Help with regex needed

Yuri Slobodyanyuk yuri.slobodyanyuk at gmail.com
Tue Apr 12 21:11:55 CEST 2011


Thanks for the insight, while this code will run once a week and
optimization isn't really a must here, it is
still  a good idea not to leave half-baked code behind me, especially given
that it will be running on this server  for the next  13 years ;)
I have one doubt though . Doesn't using the list comprehension here increase
number of loops per the same string ?
for nnn in [x.split() for x in hhh]:
My vision here is that after doing the comprehension it would look:
for nnn in [1st_field,2nd_field,3rd_filed,...,nth_filed]:
... and therefore would do number of loops equal to number of fields while
we really need just one ?

Thanks
Yuri


On Tue, Apr 12, 2011 at 3:50 PM, D'Arcy J.M. Cain <darcy at druid.net> wrote:

> On Tue, 12 Apr 2011 15:06:25 +0300
> Yuri Slobodyanyuk <yuri.slobodyanyuk at gmail.com> wrote:
> > Thanks everybody , and especially Chris - i used split and it took me 15
> > mins to make it work :)
>
> That's great.  One thing though...
>
> > for nnn in hhh:
> >     if nnn.split()[2] == str(time_tuple[1]).strip(' \t\n\r')   and
> > nnn.split()[4]  == str(time_tuple[0]).strip(' \t\n\r') and
>  nnn.split()[3]
> > == str(time_tuple[2]).strip(' \t\n\r')   :
>
> You are running split() on the same string three times and running
> strip on the same time tuple each time through the loop.  I know that
> you shouldn't optimize before testing for bottlenecks but this is just
> egrecious as well as making it more difficult to read.  Consider this.
>
> year = str(time_tuple[0]) # strip() not really needed here
> mon = str(time_tuple[1])
> day = str(time_tuple[2])
>
> for nnn in [x.split() for x in hhh]:
>    if nnn[2] == mon and nnn[3] = day and nnn[4] = year:
>
> If strip() were needed you could leave off the argument.  The default
> is to strip all whitespace from both ends.  In fact, read up on locales
> to see why it is a good idea to omit the argument.
>
> --
> D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
> http://www.druid.net/darcy/                |  and a sheep voting on
> +1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
>



-- 
Taking challenges one by one.
http://yurisk.info
http://ccie-security-blog.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110412/089a458a/attachment.html>


More information about the Python-list mailing list