Re: Newbie question, list comprehension

2008-06-08 Thread Mark Wooding
Johannes Bauer [EMAIL PROTECTED] wrote:

 import time
 localtime = time.localtime(1234567890)
 fmttime = %04d-%02d-%02d %02d:%02d:%02d % (localtime[0], localtime[1], 
 localtime[2], localtime[3], localtime[4], localtime[5])
 print fmttime

 fmttime = %04d-%02d-%02d %02d:%02d:%02d % ([localtime[i] for i in 
 range(0, 5)])

To reduce typing, set
 
  format = '%04d-%02d-%02d %02d:%02d:%02d'

Two problems.

  * Firstly, range(0, 5) == [0, 1, 2, 3, 4], so it's not big enough.
Python tends to do this half-open-interval thing.  Once you get used
to it, you'll find that it actually reduces the number of off-by-one
errors you make.

  * Secondly, the result of a list comprehension is a list;
(Unsurprising, really, I know.)  But the `%' operator only extracts
multiple arguments from a tuple, so you'd need to convert:

  format % tuple(localtime[i] for i in xrange(6)]

(I've replaced range by xrange, which avoids building an intermediate
list, and the first argument to range or xrange defaults to zero
anyway.)

Another poster claimed that localtime returns a tuple.  This isn't
correct: it returns a time.struct_time, which is not a tuple as you can
tell:

   '%s' % localtime
  '(2009, 2, 13, 23, 31, 30, 4, 44, 0)'

This is one of those times when Python's duck typing fails -- string
formatting really wants a tuple of arguments, and nothing else will do.

But you can slice a time.struct_time, and the result /is/ a genuine
tuple:

   type(localtime[:6])
  type 'tuple'

which is nice:

   format % localtime[:6]
  '2009-02-13 23:31:30'

But really what you wanted was probably

   time.strftime('%Y-%m-%d %H:%M:%S', localtime)
  '2009-02-13 23:31:30'

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question, list comprehension

2008-06-07 Thread Larry Bates

Johannes Bauer wrote:

Hello group,

I'm currently doing something like this:

import time
localtime = time.localtime(1234567890)
fmttime = %04d-%02d-%02d %02d:%02d:%02d % (localtime[0], localtime[1], 
localtime[2], localtime[3], localtime[4], localtime[5])

print fmttime

For the third line there is, I suppose, some awesome python magic I 
could use with list comprehensions. I tried:


fmttime = %04d-%02d-%02d %02d:%02d:%02d % ([localtime[i] for i in 
range(0, 5)])


But that didn't work:

Traceback (most recent call last):
  File ./test.py, line 8, in ?
fmttime = %04d-%02d-%02d %02d:%02d:%02d % ([localtime[i] for i in 
range(0, 5)])

TypeError: int argument required

As it appearently passed the while list [2009, 02, 14, 0, 31, 30] as the 
first parameter which is supposed to be substituted by %04d. Is there 
some other way of doing it?


Thanks a lot,
Regards,
Johannes

You should look at time.strftime.  It will do the formatting for you so you 
don't have to do it manually as you have.


-Larry
--
http://mail.python.org/mailman/listinfo/python-list


Newbie question, list comprehension

2008-06-06 Thread Johannes Bauer

Hello group,

I'm currently doing something like this:

import time
localtime = time.localtime(1234567890)
fmttime = %04d-%02d-%02d %02d:%02d:%02d % (localtime[0], localtime[1], 
localtime[2], localtime[3], localtime[4], localtime[5])

print fmttime

For the third line there is, I suppose, some awesome python magic I 
could use with list comprehensions. I tried:


fmttime = %04d-%02d-%02d %02d:%02d:%02d % ([localtime[i] for i in 
range(0, 5)])


But that didn't work:

Traceback (most recent call last):
  File ./test.py, line 8, in ?
fmttime = %04d-%02d-%02d %02d:%02d:%02d % ([localtime[i] for i in 
range(0, 5)])

TypeError: int argument required

As it appearently passed the while list [2009, 02, 14, 0, 31, 30] as the 
first parameter which is supposed to be substituted by %04d. Is there 
some other way of doing it?


Thanks a lot,
Regards,
Johannes

--
Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen. - Wolfgang Gerber
  in de.sci.electronics [EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question, list comprehension

2008-06-06 Thread Hans Nowak

Johannes Bauer wrote:

Hello group,

I'm currently doing something like this:

import time
localtime = time.localtime(1234567890)
fmttime = %04d-%02d-%02d %02d:%02d:%02d % (localtime[0], localtime[1], 
localtime[2], localtime[3], localtime[4], localtime[5])

print fmttime

For the third line there is, I suppose, some awesome python magic I 
could use with list comprehensions. I tried:


fmttime = %04d-%02d-%02d %02d:%02d:%02d % ([localtime[i] for i in 
range(0, 5)])


The % operator here wants a tuple with six arguments that are integers, not a 
list.  Try:


fmttime = %04d-%02d-%02d %02d:%02d:%02d % tuple(localtime[i] for i in 
range(6))


As it appearently passed the while list [2009, 02, 14, 0, 31, 30] as the 
first parameter which is supposed to be substituted by %04d. Is there 
some other way of doing it?


In this case, you can just use a slice, as localtime is a tuple:

fmttime = %04d-%02d-%02d %02d:%02d:%02d % localtime[:6]

Hope this helps! ^_^

--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question, list comprehension

2008-06-06 Thread Johannes Bauer

Hans Nowak schrieb:


In this case, you can just use a slice, as localtime is a tuple:

fmttime = %04d-%02d-%02d %02d:%02d:%02d % localtime[:6]

Hope this helps! ^_^


Ahh, how cool! That's *exactly* what I meant with awesome Python magic :-)

Amazing language, I have to admit.

Regards,
Johannes

--
Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen. - Wolfgang Gerber
  in de.sci.electronics [EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list