sobering observation, python vs. perl

Tim Chase python.list at tim.thechases.com
Thu Mar 17 11:52:30 EDT 2016


On 2016-03-17 15:29, Charles T. Smith wrote:
> isready = re.compile ("(.*) is ready")
> relreq = re.compile (".*release_req")
> for fn in sys.argv[1:]:                                 # logfile
> name tn = None
>     with open (fn) as fd:
>         for line in fd:
>             #match = re.match ("(.*) is ready", line)
>             match = isready.match (line)
>             if match:
>                 tn = match.group(1)
>             #match = re.match (".*release_req", line)
>             match = relreq.match (line)
>             if match:

Note that this "match" and "if" get executed for every line

>                 #print "%s: %s" % (tn, line),
>                 print tn
> 
> vs.
> 
> while (<>) {
>     if (/(.*) is ready/) {
>         $tn = $1;
>     }
>     elsif (/release_req/) {

Note this else ^

>         print "$tn\n";
>     }
> }

Also, you might just test for string-presence on that second one

So what happens if your code looks something like

isready = re.compile ("(.*) is ready")
for fn in sys.argv[1:]: # logfile name
    tn = None
    with open (fn) as fd:
        for line in fd:
            match = isready.match (line)
            if match:
                tn = match.group(1)
            elif "release_req" in line:
                print tn

Not saying this will make a great deal of difference, but these two
items jumped out at me.  I'd even be tempted to just use string
manipulations for the isready aspect as well.  Something like
(untested)

IS_READY = " is ready"
REL_REQ = "release_req"
for n in sys.argv[1:]:
  tn = None
  with open(fn) as fd):
    for line in fd:
      try:
        index = line.rindex(IS_READY)
      except ValueError:
        if REL_REQ in line:
          print tn
      else:
        tn = line[:index]

-tkc






More information about the Python-list mailing list