Python wins again!
Alan Green
web.mail at lycos.com
Tue Oct 30 18:55:00 EST 2001
A colleague of mine - who wishes to remain anonymous to the world at
large - just sent me the following. Waaaayyy cool!
> Just a little of tale of why I enjoy having Python as a tool.
>
> I had a time, represented in the standard Unix
> seconds-since-epoch format.
> I wanted to know what the date of this time was, and for some
> reason couldn't do this in my head.
>
> Now, I knew that the Python time module could do this, but I
> couldn't for the life of me remember which method I needed.
>
> Enter my hack (done at command prompt)
>
> >>> import time
> >>> a = 941598773
> >>> for f in dir(time):
> ... str = "time.%s(%d)" %(f,a)
> ... try:
> ... print eval(str)
> ... except:
> ... pass
> ...
> Wed Nov 03 13:12:53 1999
> (1999, 11, 3, 3, 12, 53, 2, 307, 0)
> (1999, 11, 3, 13, 12, 53, 2, 307, 0)
> >>>
>
> A small problem is that, while I have the answer, I still
> don't know what method it is. I should have written
>
> print f, eval(str)
>
> instead. But, as it stands, it was quicker to do this than to
> consult the module reference.
>
He also adds:
> Seriously, which of these would you choose?
> >>> dir(time)
> ['__doc__', '__name__', 'accept2dyear', 'altzone', 'asctime',
> 'clock', 'ctime', 'daylight', 'gmtime', 'localtime',
> 'mktime', 'sleep', 'strftime', 'time', 'timezone', 'tzname']
For a laugh, just re-read that list and take a guess.
> The answer is ctime.
Alan.
More information about the Python-list
mailing list