Python vs. Perl, which is better to learn?
Andrew Dalke
dalke at dalkescientific.com
Wed May 1 05:39:52 EDT 2002
Terry Hancock:
>Thanks for the example -- I think there is some definite
>merit to this. It occurs to me that you might be both more
>clear and more compact (and equally pythonic?) to say:
>
>digit = '[0-9]'
>date = '\(' + 4*digit + '/' + 2*digit + '/' + 2*digit + '\)'
>
>for the regular expression, so you don't have to count
>the digits.
This comes from my Martel project
(http://www.dalkescientific.com/Martel)
>>> from Martel import Time
>>> Time.make_pattern("%(YYYY)/%(MM)/%(DD)")
'(?P<year?type=long>[0-9]{4})\\/(?P<month?type=numeric>(0[1-9]|1[012]))\\/(?
P<day?
type=numeric>(0[1-9]|[12][0-9]|3[01]))'
>>> print Martel.select_names(Time.make_expression("%(YYYY)/%(MM)/%(DD)"),
())
[\d]{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[12][\d]|3[01])
>>>
The first one uses an extension to regular expressions to allow XML-style
key/value pairs on the match names. The second one makes an regexp parse
tree I can manipulate -- in this case to remove all named groups.
This solution is even nicer in some sense because it's easy to
read and it doesn't allow
1234/56/78
as a legal year.
>My Perl teacher described Perl as being "like English",
>full of idioms and synonyms and ambiguities resolved
>by context -- an organic, naturally evolved solution
>to programming problems. If so, Python is probably like
>Esperanto -- artificial, clean, regular, and easy to
>learn, though perhaps not as creatively expressive in
>itself.
Oh my, not this argument again. Before people respond,
there's been discussions on the English/Esperanto comparison
before. Please check the archives.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list