On Thu, Mar 07, 2002 at 12:59:31AM -0800, Bob Miller wrote:
>But compare this Perl:
>
>    /^\[(\d+)\:(\d+)\:(\d+)\]/ and ($o, $t) = ($t, $1 * 3600 + $2 * 60 + $3);
>
>with this Python:
>
>    timestamp = re.match(r'\[(\d+)\:(\d+)\:(\d+)\]', line)
>    if timestamp:
>        hr, min, sec = [int(n) for n in timestamp.groups()]
>        o, t = t, (hr * 60 + min) * 60 + sec

Python doesn't have to be that bad.  There has been talk in the past about
adding an assignment in conditional syntax such as:

   if timestamp := re.match(r'\[(\d+)\:(\d+)\:(\d+)\]', line):
      hr, min, sec = [int(n) for n in timestamp.groups()]
      o, t = t, (hr * 60 + min) * 60 + sec

However, I built a module called "filterinput" (available from the Vaults,
I believe, or I can mail it), which is called "filterinput".  It's meant to
make building filters easier.  For example:

   import filterinput
   while filterinput.readline(stripeol = 1):
      if filterinput.match(r'\s*(\S+)\s*=\s*(\S*)\s*$'):
         print '"%s" = "%s"' % filterinput.group(1, 2)
      else:
         print 'No match on line "%s"' % filterinput.filterLine

I had worked at one point to integrate it into the standard library, but
Guido felt that if you wanted to be using Perl you should be using Perl.
We agreed to disagree.  I should try pushing it again, because it really
does make things a bit easier to deal with.  I mean, a lot has changed
since I last talked to Guido about it...  I mean, he's finally allowed
augmented assignment ("+=").

I do have a design on the white-board for a filter-type module which would
implement most common types of filtering in straight C, which would give
something on the order of 100x performance increase for situations where
you're rejecting a lot of data (for example, looking through
/var/log/messages for a particular string).  I haven't built that yet...

Perhaps one of these weeks at the hackingsociety.org meeting...  :-)

Sean
-- 
 "How can just a few feet of unshielded twisted pair bring SO much joy?"
                 -- Paul Orosz on DSL, paraphrasing Homer Simpson
Sean Reifschneider, Inimitably Superfluous <[EMAIL PROTECTED]>
tummy.com - Linux Consulting since 1995. Qmail, KRUD, Firewalls, Python

Reply via email to