problem with eval and time

W

Wincent

Dear all, I would like to convert tstr to representation of time, but encounter the following error. Is there a simple way to get what I want? Thanks.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'

Wincent
 
A

alex23

Dear all, I would like to convert tstr to representation
of time, but encounter the following error. Is there a
simple way to get what I want? Thanks.


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
TypeError: structseq() takes at most 2 arguments (9 given)>>> sys.version

The problem is that the repr of `time.struct_time` isn't its
constructor, so you won't be able to do this without parsing the
string, I believe.

What are you trying to achieve here? You already have a
time.struct_time object, why turn it into a string if what you want is
the object?

If you're wanting to pass time values around as strings, maybe
`time.strptime` will be more useful.
 
W

Wincent

Thanks.

I fetch data from social networking sites and want to mark the time of access. I store all the information in a redis database, which converts everything into strings and I need to convert those strings back to original python objects when analyzing the data.

Best Regards
 
C

Chris Angelico

Thanks.

I fetch data from social networking sites and want to mark the time of access. I store all the information in a redis database, which converts everything into strings and I need to convert those strings back to original python objects when analyzing the data.

The easiest way, imho, is to store Unix times - simply the number of
seconds since 1970, as an integer or float. That can easily and safely
be turned into a string and back (floats might lose a little accuracy,
depending on how you do it, but the difference will be a small
fraction of a second).
time.struct_time(tm_year=2012, tm_mon=11, tm_mday=6, tm_hour=4,
tm_min=35, tm_sec=47, tm_wday=1, tm_yday=311, tm_isdst=0)

Easy and unambiguous. Also compact, which may or may not be a selling point..

ChrisA
 
D

Dennis Lee Bieber

Dear all, I would like to convert tstr to representation of time, but encounter the following error. Is there a simple way to get what I want? Thanks.
Well... you don't show an example of "what I want"...

Did you look at what you are doing there?
time.struct_time(tm_year=2012, tm_mon=11, tm_mday=5, tm_hour=23,
tm_min=38, tm_sec=16, tm_wday=0, tm_yday=310, tm_isdst=0)'time.struct_time(tm_year=2012, tm_mon=11, tm_mday=5, tm_hour=23,
tm_min=40, tm_sec=22, tm_wday=0, tm_yday=310, tm_isdst=0)'
You've created a struct_time object, then turned that into a string
representation.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
TypeError: structseq() takes at most 2 arguments (9 given)

Now you are trying to evaluate that string representation. But the
constructor form uses a /tuple/ of values...
time.struct_time(tm_year=2012, tm_mon=11, tm_mday=5, tm_hour=23,
tm_min=47, tm_sec=32, tm_wday=0, tm_yday=310, tm_isdst=0)
.... not a bunch of position/keyword arguments.

I'd consider it a wart -- commonly the representation is valid for
reconstructing the data...
time.struct_time(tm_year=2012, tm_mon=11, tm_mday=6, tm_hour=0,
tm_min=3, tm_sec=52, tm_wday=1, tm_yday=311, tm_isdst=0)
But really, what do you mean by "representation of time"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top