Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105098 > unrolled thread
| Started by | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| First post | 2016-03-17 15:29 +0000 |
| Last post | 2016-03-18 22:47 +1100 |
| Articles | 20 on this page of 43 — 14 participants |
Back to article view | Back to comp.lang.python
sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 15:29 +0000
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 15:40 +0000
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 17:48 +0200
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 15:59 +0000
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 18:07 +0200
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:15 +0000
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 17:47 +0200
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:06 +0000
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 18:30 +0200
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:32 +0000
Re: sobering observation, python vs. perl srinivas devaki <mr.eightnoteight@gmail.com> - 2016-03-17 21:18 +0530
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:15 +0000
Re: sobering observation, python vs. perl Tim Chase <python.list@tim.thechases.com> - 2016-03-17 10:52 -0500
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:08 +0000
Re: sobering observation, python vs. perl Ethan Furman <ethan@stoneleaf.us> - 2016-03-17 09:21 -0700
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:36 +0000
Re: sobering observation, python vs. perl Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-17 17:09 +0000
Re: sobering observation, python vs. perl Ethan Furman <ethan@stoneleaf.us> - 2016-03-17 10:26 -0700
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 17:35 +0000
Re: sobering observation, python vs. perl Ethan Furman <ethan@stoneleaf.us> - 2016-03-17 11:21 -0700
DSLs in perl and python (Was sobering observation) Rustom Mody <rustompmody@gmail.com> - 2016-03-17 10:47 -0700
Re: DSLs in perl and python (Was sobering observation) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-17 22:22 +0000
Re: DSLs in perl and python (Was sobering observation) MRAB <python@mrabarnett.plus.com> - 2016-03-17 22:43 +0000
Re: DSLs in perl and python (Was sobering observation) Rustom Mody <rustompmody@gmail.com> - 2016-03-18 05:57 -0700
Re: DSLs in perl and python (Was sobering observation) Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-18 15:18 +0200
Re: DSLs in perl and python (Was sobering observation) Peter Otten <__peter__@web.de> - 2016-03-18 14:22 +0100
Re: DSLs in perl and python (Was sobering observation) Rustom Mody <rustompmody@gmail.com> - 2016-03-18 19:07 -0700
DSL design (was DSLs in perl and python) Rustom Mody <rustompmody@gmail.com> - 2016-03-29 06:28 -0700
Re: DSL design (was DSLs in perl and python) Chris Angelico <rosuav@gmail.com> - 2016-03-30 00:41 +1100
Re: DSL design (was DSLs in perl and python) Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-29 16:45 +0300
Finding methods, was Re: DSL design (was DSLs in perl and python) Peter Otten <__peter__@web.de> - 2016-03-29 15:51 +0200
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 18:34 +0200
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 16:42 +0000
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 19:08 +0200
Re: sobering observation, python vs. perl "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-17 17:25 +0000
Re: sobering observation, python vs. perl BartC <bc@freeuk.com> - 2016-03-17 17:53 +0000
Re: sobering observation, python vs. perl Rustom Mody <rustompmody@gmail.com> - 2016-03-17 10:59 -0700
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 20:53 +0200
Re: sobering observation, python vs. perl BartC <bc@freeuk.com> - 2016-03-17 19:06 +0000
Re: sobering observation, python vs. perl Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 21:11 +0200
Re: sobering observation, python vs. perl Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-17 20:47 +0000
Re: sobering observation, python vs. perl Peter Otten <__peter__@web.de> - 2016-03-18 10:26 +0100
Re: sobering observation, python vs. perl Steven D'Aprano <steve@pearwood.info> - 2016-03-18 22:47 +1100
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-17 10:47 -0700 |
| Subject | DSLs in perl and python (Was sobering observation) |
| Message-ID | <7f139f92-3957-40e5-be75-ad19b9401617@googlegroups.com> |
| In reply to | #105117 |
On Thursday, March 17, 2016 at 10:09:27 PM UTC+5:30, Charles T. Smith wrote:
> or something else ... you're in a "defend python at all costs!" mode.
Interesting that I see this now.
I was showing some finance-friends how to to convert one of the tables in
https://www.nyse.com/publicdocs/nyse/markets/amex-options/ArcaDirectAPISpecVersion4_1.pdf
[printed page 9 pdf pg 12 "Equities Symbology" ]
Since they were much more comfortable with perl than with python
I wrote the first version in perl (which I am not comfortable with!):
You try these by calling at command line with one argument
such as
IBM.A#
The perl and python versions are not matching in output behavior
but thats not really relevant; what we want is the exact recognition of that table (currently only a subset that I understand!)
--------------perl version
#!/usr/bin/perl
$string = $ARGV[0];
$string =~ /([A-Z]*)([.+-])?([A-Z]?)(#?)/;
%seriesmap = ('.' => "Plain",
'-' => "Preferred",
'+' => "Warrant"
);
print "(";
print "$1" . ", ";
print maybeize($2 , $seriesmap{$2}) . ", ";
print maybeize($3 , $3) . ", ";
print ((($4 =~ /#/) ? "True" : "False") . ")\n");
sub maybeize ($inp, $out) {
my ($inp, $out) = @_;
return (($inp eq "") ? "None" : ("Maybe." . $out));
}
----------- python version 1-----------------------
#!/usr/bin/python
# Using named captures and verbose (ie multiline readable) regexps
# Idea being that the regexp captures most of the DSL semantics
# ATM the DSL is no pretty (Thats another matter :-)
from sys import argv
import re
seriesmap = {'.':"Plain", '-':"Preferred", '+':"Warrant"}
string = argv[1]
rexp = r"""(?x) # Verbose re
# DSL for describing symbology
(?P<scrip> [A-Z]*) # The base scrip
(?P<serchar> [.+-])? # Series type char
(?P<series> [A-Z])? # Series
(?P<issuedc> [#])? # issued char indicator
"""
rec = re.compile(rexp)
m = rec.match(string)
if m:
g = m.group
# DSL -> Python
scrip, serchar, series, issuedc = g('scrip'), g('serchar'), g('series'), g('issuedc')
# Postprocessing
sertype = seriesmap[serchar]
issued = issuedc == '#'
print "scrip: %s\nsertype: %s\nseries: %s\nissued: %s" % (scrip, sertype, series, issued)
else:
print "match failed"
Finally separating the dsl into a separate file from the python driver:
--------- python driver
#!/usr/bin/python
# Symbology into a separated into file: "symbologydsl.txt"
# As of now file name hardwired and needs to be in same directory as this script
from sys import argv
import re
try:
# TODO Following needs to be added to dsl file
# (?=^.{1,}$) # There better be something >=1
dslfile = "symbologydsl.txt"
rexp = open(dslfile, "rU").read()
string = argv[1]
except IOError as i:
if i.errno == 2: # File not found
print ("No dsl file")
else:
print ("IOError: %s" % i.str)
exit()
except IndexError:
print ("No arg given?")
exit()
seriesmap = {'.':"Plain", '-':"Preferred", '+':"Warrant"}
m = re.search(rexp, string, re.VERBOSE)
if m:
g = m.group
# DSL -> Python
scrip, serchar, series, issuedc = g('scrip'), g('serchar'), g('series'), g('issuedc')
# Postprocessing: series, scrip are left as is
sertype = seriesmap[serchar] if serchar else "No Series Type"
issued = issuedc == '#'
print("scrip:\t%s\ntype:\t%s\nseries:\t%s\nissued:\t%s" %
(scrip, sertype, series, issued))
else:
print ("match failed")
----------- dsl file: symbologydsl.txt--------
# DSL (instantiation) for describing NYSE symbology
^
(?P<scrip> [A-Z]*) # The base scrip
(?P<serchar> [.+-])? # Series type char
(?P<series> [A-Z])? # Series
(?P<issuedc> [#])? # issued char indicator
$ # Thats all (there should be!)
------------------------------------
So now my questions:
How do you in perl make regexps readable like python's VERBOSE?
Can you do better in perl?
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-17 22:22 +0000 |
| Subject | Re: DSLs in perl and python (Was sobering observation) |
| Message-ID | <mailman.289.1458253391.12893.python-list@python.org> |
| In reply to | #105128 |
On 17/03/2016 17:47, Rustom Mody wrote: > On Thursday, March 17, 2016 at 10:09:27 PM UTC+5:30, Charles T. Smith wrote: >> or something else ... you're in a "defend python at all costs!" mode. > So now my questions: > > How do you in perl make regexps readable like python's VERBOSE? > Can you do better in perl? > I neither know, nor do I care. Why not ask on a perl list? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-03-17 22:43 +0000 |
| Subject | Re: DSLs in perl and python (Was sobering observation) |
| Message-ID | <mailman.294.1458254815.12893.python-list@python.org> |
| In reply to | #105128 |
On 2016-03-17 22:22, Mark Lawrence wrote: > On 17/03/2016 17:47, Rustom Mody wrote: >> On Thursday, March 17, 2016 at 10:09:27 PM UTC+5:30, Charles T. Smith wrote: >>> or something else ... you're in a "defend python at all costs!" mode. >> So now my questions: >> >> How do you in perl make regexps readable like python's VERBOSE? >> Can you do better in perl? >> > > I neither know, nor do I care. Why not ask on a perl list? > Stick an "x" on the end of the regex: /something/x or s/old/new/x.
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-18 05:57 -0700 |
| Subject | Re: DSLs in perl and python (Was sobering observation) |
| Message-ID | <b3b4b5a9-65e2-4598-8bba-1f4427f9abec@googlegroups.com> |
| In reply to | #105166 |
On Friday, March 18, 2016 at 4:17:06 AM UTC+5:30, MRAB wrote:
> Stick an "x" on the end of the regex: /something/x or s/old/new/x.
Thanks!
Is there somewhere a regexp 'introspection' API/capability available?
ie if the re looks like
rexp = r"""
# DSL (instantiation) for describing NYSE symbology
^
(?P<scrip> [A-Z]*) # The base scrip
(?P<serchar> [.+-])? # Series type char
(?P<series> [A-Z])? # Series
(?P<issuedc> [#])? # issued char indicator
$ # Thats all (there should be!)
"""
I would like to know that the named-groups are
{scrip, serchar, series, issued}
without doing match/search etc
That way then the same DSL could be used for quite different regexps
IOW I would like to generalize the code:
g = m.group
scrip, serchar, series, issuedc = g('scrip'), g('serchar'), g('series'), g('issuedc')
The scrip, serchar, series etc need not be local vars;
a dict is fine/preferable
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jussi.piitulainen@helsinki.fi> |
|---|---|
| Date | 2016-03-18 15:18 +0200 |
| Subject | Re: DSLs in perl and python (Was sobering observation) |
| Message-ID | <lf5mvpv9b5z.fsf@ling.helsinki.fi> |
| In reply to | #105209 |
Rustom Mody writes:
> On Friday, March 18, 2016 at 4:17:06 AM UTC+5:30, MRAB wrote:
>> Stick an "x" on the end of the regex: /something/x or s/old/new/x.
>
> Thanks!
>
> Is there somewhere a regexp 'introspection' API/capability available?
>
> ie if the re looks like
> rexp = r"""
> # DSL (instantiation) for describing NYSE symbology
> ^
> (?P<scrip> [A-Z]*) # The base scrip
> (?P<serchar> [.+-])? # Series type char
> (?P<series> [A-Z])? # Series
> (?P<issuedc> [#])? # issued char indicator
> $ # Thats all (there should be!)
> """
>
> I would like to know that the named-groups are
> {scrip, serchar, series, issued}
> without doing match/search etc
>
> That way then the same DSL could be used for quite different regexps
>
> IOW I would like to generalize the code:
>
> g = m.group
> scrip, serchar, series, issuedc = g('scrip'), g('serchar'), g('series'), g('issuedc')
>
> The scrip, serchar, series etc need not be local vars;
> a dict is fine/preferable
Maybe the .groupindex in the object that you get when you compile the
regex. Found it with dir(pat), help(pat) was not so helpful. Haven't
looked for other documentation.
>>> pat = re.compile(r'...(?P<foo>xxx)...(?P<bar>yyy)...')
>>> pat.groups
2
>>> pat.groupindex
{'bar': 2, 'foo': 1}
>>> pat.pattern
'...(?P<foo>xxx)...(?P<bar>yyy)...'
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-03-18 14:22 +0100 |
| Subject | Re: DSLs in perl and python (Was sobering observation) |
| Message-ID | <mailman.315.1458307357.12893.python-list@python.org> |
| In reply to | #105209 |
Rustom Mody wrote:
> On Friday, March 18, 2016 at 4:17:06 AM UTC+5:30, MRAB wrote:
>> Stick an "x" on the end of the regex: /something/x or s/old/new/x.
>
> Thanks!
>
> Is there somewhere a regexp 'introspection' API/capability available?
>
> ie if the re looks like
> rexp = r"""
> # DSL (instantiation) for describing NYSE symbology
> ^
> (?P<scrip> [A-Z]*) # The base scrip
> (?P<serchar> [.+-])? # Series type char
> (?P<series> [A-Z])? # Series
> (?P<issuedc> [#])? # issued char indicator
> $ # Thats all (there should be!)
> """
>
> I would like to know that the named-groups are
> {scrip, serchar, series, issued}
> without doing match/search etc
Is that a Perl or a Python question? If the latter:
>>> r = re.compile(rexp, re.VERBOSE)
>>> r.groupindex
{'serchar': 2, 'issuedc': 4, 'scrip': 1, 'series': 3}
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-18 19:07 -0700 |
| Subject | Re: DSLs in perl and python (Was sobering observation) |
| Message-ID | <21d252f4-c6a8-4241-88e4-c5f492ebb835@googlegroups.com> |
| In reply to | #105211 |
On Friday, March 18, 2016 at 6:52:53 PM UTC+5:30, Peter Otten wrote:
> Rustom Mody wrote:
>
> > On Friday, March 18, 2016 at 4:17:06 AM UTC+5:30, MRAB wrote:
> >> Stick an "x" on the end of the regex: /something/x or s/old/new/x.
> >
> > Thanks!
> >
> > Is there somewhere a regexp 'introspection' API/capability available?
> >
> > ie if the re looks like
> > rexp = r"""
> > # DSL (instantiation) for describing NYSE symbology
> > ^
> > (?P<scrip> [A-Z]*) # The base scrip
> > (?P<serchar> [.+-])? # Series type char
> > (?P<series> [A-Z])? # Series
> > (?P<issuedc> [#])? # issued char indicator
> > $ # Thats all (there should be!)
> > """
> >
> > I would like to know that the named-groups are
> > {scrip, serchar, series, issued}
> > without doing match/search etc
>
> Is that a Perl or a Python question? If the latter:
>
> >>> r = re.compile(rexp, re.VERBOSE)
> >>> r.groupindex
> {'serchar': 2, 'issuedc': 4, 'scrip': 1, 'series': 3}
Neat Thanks (and Jussi)
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-29 06:28 -0700 |
| Subject | DSL design (was DSLs in perl and python) |
| Message-ID | <d3fa99e0-cf9d-4876-9fa3-a5c7803e9f54@googlegroups.com> |
| In reply to | #105249 |
On Saturday, March 19, 2016 at 7:37:52 AM UTC+5:30, Rustom Mody wrote:
> On Friday, March 18, 2016 at 6:52:53 PM UTC+5:30, Peter Otten wrote:
> > Rustom Mody wrote:
> >
> > > On Friday, March 18, 2016 at 4:17:06 AM UTC+5:30, MRAB wrote:
> > >> Stick an "x" on the end of the regex: /something/x or s/old/new/x.
> > >
> > > Thanks!
> > >
> > > Is there somewhere a regexp 'introspection' API/capability available?
> > >
> > > ie if the re looks like
> > > rexp = r"""
> > > # DSL (instantiation) for describing NYSE symbology
> > > ^
> > > (?P<scrip> [A-Z]*) # The base scrip
> > > (?P<serchar> [.+-])? # Series type char
> > > (?P<series> [A-Z])? # Series
> > > (?P<issuedc> [#])? # issued char indicator
> > > $ # Thats all (there should be!)
> > > """
> > >
> > > I would like to know that the named-groups are
> > > {scrip, serchar, series, issued}
> > > without doing match/search etc
> >
> > Is that a Perl or a Python question? If the latter:
> >
> > >>> r = re.compile(rexp, re.VERBOSE)
> > >>> r.groupindex
> > {'serchar': 2, 'issuedc': 4, 'scrip': 1, 'series': 3}
>
> Neat Thanks (and Jussi)
Thanks once again to Peter and Jussi.
With that groupindex pointer, this tiny dsl for re's is here
https://github.com/rusimody/redsl
[How BTW did you folks go about unearth that groupindex?? Dont see it in docs]
Comments welcome.
If people have comments on the python thats fine -- specifically if something
does not work, is unclear etc.
I am however more specifically interested in the DSL-design --
Can one clean/spruce that up?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-30 00:41 +1100 |
| Subject | Re: DSL design (was DSLs in perl and python) |
| Message-ID | <mailman.151.1459258884.28225.python-list@python.org> |
| In reply to | #105982 |
On Wed, Mar 30, 2016 at 12:28 AM, Rustom Mody <rustompmody@gmail.com> wrote: > Thanks once again to Peter and Jussi. > With that groupindex pointer, this tiny dsl for re's is here > https://github.com/rusimody/redsl > [How BTW did you folks go about unearth that groupindex?? Dont see it in docs] Depending on your version: https://docs.python.org/2/library/re.html#re.RegexObject.groupindex https://docs.python.org/3/library/re.html#re.regex.groupindex You can find that by searching the docs for "?P", the syntax used in the RE itself. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jussi.piitulainen@helsinki.fi> |
|---|---|
| Date | 2016-03-29 16:45 +0300 |
| Subject | Re: DSL design (was DSLs in perl and python) |
| Message-ID | <lf5oa9xxurz.fsf@ling.helsinki.fi> |
| In reply to | #105982 |
Rustom Mody writes:
> [How BTW did you folks go about unearth that groupindex?? Dont see it
> in docs]
The following reveal its existence:
dir(re.compile(""))
help(re.compile(""))
But help only lists it as one of "data descriptors defined here" (in
Python 3.4.3). I think I just guessed from its name that it might be
relevant, and trying it out didn't immediately contradict the guess :)
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-03-29 15:51 +0200 |
| Subject | Finding methods, was Re: DSL design (was DSLs in perl and python) |
| Message-ID | <mailman.153.1459259524.28225.python-list@python.org> |
| In reply to | #105982 |
Rustom Mody wrote: > [How BTW did you folks go about unearth that groupindex?? Dont see it in > [docs] While it's there https://docs.python.org/dev/library/re.html#re.regex.groupindex my personal search algorithm for this category of questions is mostly "I saw something like that before", then dir(some_obj) to find the candidates, then try the candidates in order of likelihood. If that fails use the source. While you learn things you weren't looking for the disadvantage of that approach is that you sometimes miss the "obvious, first hit given by the search engine" answer.
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2016-03-17 18:34 +0200 |
| Message-ID | <87twk5qd1d.fsf@elektro.pacujo.net> |
| In reply to | #105108 |
"Charles T. Smith" <cts.private.yahoo@gmail.com>: > well, I don't want to forgo REs in order to have python's numbers be > better.... <URL: http://stackoverflow.com/questions/12793562/text-processing-pytho n-vs-perl-performance> Marko
[toc] | [prev] | [next] | [standalone]
| From | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2016-03-17 16:42 +0000 |
| Message-ID | <ncempn$eav$4@dont-email.me> |
| In reply to | #105115 |
On Thu, 17 Mar 2016 18:34:06 +0200, Marko Rauhamaa wrote: > n-vs-perl-performance Okay, that was interesting. Actually, I saw a study some years ago that concluded that python could be both slower and faster than perl, but that perl had much less deviation than python. I took that and accepted it, but was surprised now that in exactly the field of application that I've traditionally used perl, it really is better, er... faster. Furthermore, the really nice thing about python is its OO, but I've really neglected looking into that with perl's OO capabilities.
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2016-03-17 19:08 +0200 |
| Message-ID | <87h9g5qbf9.fsf@elektro.pacujo.net> |
| In reply to | #105118 |
"Charles T. Smith" <cts.private.yahoo@gmail.com>:
> Actually, I saw a study some years ago that concluded that python
> could be both slower and faster than perl, but that perl had much less
> deviation than python. I took that and accepted it, but was surprised
> now that in exactly the field of application that I've traditionally
> used perl, it really is better, er... faster.
>
> Furthermore, the really nice thing about python is its OO, but I've
> really neglected looking into that with perl's OO capabilities.
I haven't had such log processing needs as you, nor has it come down to
performance in such a way. Do use the best tool for the job.
(When it comes to freely formatted logs, gleaning information from them
is somewhat of a lost cause. I've done my best to move to rigorously
formatted logs that are much more amenable to post processing.)
Perl might be strong on its home turf, but I am a minimalist and
reductionist -- Perl was intentionally designed to be a maximalist,
imitating the principles of natural languages. Python has concise,
crystal-clear semantics that are convenient to work with.
Compare Perl (<URL: http://www.perlmonks.org/?node_id=98357>):
my $str = "I have a dream";
my $find = "have";
my $replace = "had";
$find = quotemeta $find; # escape regex metachars if present
$str =~ s/$find/$replace/g;
print $str;
with Python:
print("I have a dream".replace("have", "had"))
Marko
[toc] | [prev] | [next] | [standalone]
| From | "Charles T. Smith" <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2016-03-17 17:25 +0000 |
| Message-ID | <ncep9g$ore$1@dont-email.me> |
| In reply to | #105119 |
On Thu, 17 Mar 2016 19:08:58 +0200, Marko Rauhamaa wrote:
> "Charles T. Smith" <cts.private.yahoo@gmail.com>:
>
>
> Compare Perl (<URL: http://www.perlmonks.org/?node_id=98357>):
>
> my $str = "I have a dream";
> my $find = "have";
> my $replace = "had";
> $find = quotemeta $find; # escape regex metachars if present
> $str =~ s/$find/$replace/g;
> print $str;
>
> with Python:
>
> print("I have a dream".replace("have", "had"))
>
>
> Marko
Uh... that perl is way over my head. I admit though, that perl's
powerful substitute command is also clumsy. The best I can do
right now is:
$v = "I have a dream\n";
$v =~ s/have/had/;
print $v
One of the ugliest things about perl are the "silly" type
prefixes ($, @, %). But in a python project I'm doing now,
I realized an important advantage that they bring...
I want to be able to initialize msgs to communicate with
C. Ideally, I'd to just specify the path to an equivalent
python instance but all intermediate instances have to
already exist - python does not have autovivication.
I implemented it but only up until the leaf node - because
python doesn't know their types. Perl can do that, because the
prefix tells it the type.
But, don't get me wrong, coding in python is a JOY!
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-03-17 17:53 +0000 |
| Message-ID | <nceqpg$3l9$1@dont-email.me> |
| In reply to | #105125 |
On 17/03/2016 17:25, Charles T. Smith wrote:
> On Thu, 17 Mar 2016 19:08:58 +0200, Marko Rauhamaa wrote:
>> my $str = "I have a dream";
>> my $find = "have";
>> my $replace = "had";
>> $find = quotemeta $find; # escape regex metachars if present
>> $str =~ s/$find/$replace/g;
>> print $str;
>>
>> with Python:
>>
>> print("I have a dream".replace("have", "had"))
> Uh... that perl is way over my head. I admit though, that perl's
> powerful substitute command is also clumsy. The best I can do
> right now is:
>
> $v = "I have a dream\n";
> $v =~ s/have/had/;
> print $v
I was going to suggest just using a function. But never having coded in
Perl before, I wasn't expecting something this ugly:
sub replacewith{
$s = $_[0];
$t = $_[1];
$u = $_[2];
$s =~ s/$t/$u/;
return $s;
}
Although once done, the original task now looks a proper language:
print (replacewith("I have a dream","have","had"));
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-17 10:59 -0700 |
| Message-ID | <c07493d3-5e9c-4c1a-87e0-891ae0f9e9f7@googlegroups.com> |
| In reply to | #105130 |
On Thursday, March 17, 2016 at 11:24:00 PM UTC+5:30, BartC wrote:
> On 17/03/2016 17:25, Charles T. Smith wrote:
> > On Thu, 17 Mar 2016 19:08:58 +0200, Marko Rauhamaa wrote:
>
> >> my $str = "I have a dream";
> >> my $find = "have";
> >> my $replace = "had";
> >> $find = quotemeta $find; # escape regex metachars if present
> >> $str =~ s/$find/$replace/g;
> >> print $str;
> >>
> >> with Python:
> >>
> >> print("I have a dream".replace("have", "had"))
>
> > Uh... that perl is way over my head. I admit though, that perl's
> > powerful substitute command is also clumsy. The best I can do
> > right now is:
> >
> > $v = "I have a dream\n";
> > $v =~ s/have/had/;
> > print $v
>
> I was going to suggest just using a function. But never having coded in
> Perl before, I wasn't expecting something this ugly:
>
> sub replacewith{
> $s = $_[0];
> $t = $_[1];
> $u = $_[2];
I think [untested] you can shorten those 3 lines to:
($s, $t, $u) = @_ ;
> $s =~ s/$t/$u/;
> return $s;
> }
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2016-03-17 20:53 +0200 |
| Message-ID | <878u1hq6ln.fsf@elektro.pacujo.net> |
| In reply to | #105130 |
BartC <bc@freeuk.com>:
> I was going to suggest just using a function. But never having coded in
> Perl before, I wasn't expecting something this ugly:
>
> sub replacewith{
> $s = $_[0];
> $t = $_[1];
> $u = $_[2];
> $s =~ s/$t/$u/;
> return $s;
> }
>
> Although once done, the original task now looks a proper language:
>
> print (replacewith("I have a dream","have","had"));
Now try your function with:
print (replacewith("I have a dream",".","had"));
Marko
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-03-17 19:06 +0000 |
| Message-ID | <ncev1j$k9l$1@dont-email.me> |
| In reply to | #105137 |
On 17/03/2016 18:53, Marko Rauhamaa wrote:
> BartC <bc@freeuk.com>:
>> sub replacewith{
>> $s = $_[0];
>> $t = $_[1];
>> $u = $_[2];
>> $s =~ s/$t/$u/;
>> return $s;
>> }
>>
>> Although once done, the original task now looks a proper language:
>>
>> print (replacewith("I have a dream","have","had"));
>
> Now try your function with:
>
> print (replacewith("I have a dream",".","had"));
Yeah, it needs your quotemeta line (whatever that does). But the call is
unaffected as the clutter is in the function.
--
bartc
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2016-03-17 21:11 +0200 |
| Message-ID | <874mc4rkaw.fsf@elektro.pacujo.net> |
| In reply to | #105139 |
BartC <bc@freeuk.com>:
> On 17/03/2016 18:53, Marko Rauhamaa wrote:
>> BartC <bc@freeuk.com>:
>
>>> sub replacewith{
>>> $s = $_[0];
>>> $t = $_[1];
>>> $u = $_[2];
>>> $s =~ s/$t/$u/;
>>> return $s;
>>> }
>>>
>>> Although once done, the original task now looks a proper language:
>>>
>>> print (replacewith("I have a dream","have","had"));
>>
>> Now try your function with:
>>
>> print (replacewith("I have a dream",".","had"));
>
> Yeah, it needs your quotemeta line (whatever that does). But the call
> is unaffected as the clutter is in the function.
Well, you fell in the trap. Most perl programmers would fall in it. Same
with bash programmers, including myself.
That's why I'm wondering if Python could come to the rescue and offer a
solid alternative to bash. You have to go out of your way to get into
accidental quoting/escaping problems in Python.
Marko
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web