Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32794 > unrolled thread
| Started by | Wincent <ronggui.huang@gmail.com> |
|---|---|
| First post | 2012-11-05 19:32 -0800 |
| Last post | 2012-11-06 00:06 -0500 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
problem with eval and time Wincent <ronggui.huang@gmail.com> - 2012-11-05 19:32 -0800
Re: problem with eval and time alex23 <wuwei23@gmail.com> - 2012-11-05 20:22 -0800
Re: problem with eval and time Wincent <ronggui.huang@gmail.com> - 2012-11-05 20:29 -0800
Re: problem with eval and time Chris Angelico <rosuav@gmail.com> - 2012-11-06 15:38 +1100
Re: problem with eval and time Dave Angel <d@davea.name> - 2012-11-05 23:42 -0500
Re: problem with eval and time Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-06 00:06 -0500
| From | Wincent <ronggui.huang@gmail.com> |
|---|---|
| Date | 2012-11-05 19:32 -0800 |
| Subject | problem with eval and time |
| Message-ID | <932c348a-2670-44f0-a38b-4fddae577a0e@googlegroups.com> |
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. >>> import time >>> tstr = str(time.localtime()) >>> eval(tstr) 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 '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' Wincent
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2012-11-05 20:22 -0800 |
| Message-ID | <43fc26d7-50f4-470a-9d02-3b3e27271b63@nl3g2000pbc.googlegroups.com> |
| In reply to | #32794 |
On Nov 6, 1:32 pm, Wincent <ronggui.hu...@gmail.com> wrote: > 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. > > >>> import time > >>> tstr = str(time.localtime()) > >>> eval(tstr) > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Wincent <ronggui.huang@gmail.com> |
|---|---|
| Date | 2012-11-05 20:29 -0800 |
| Message-ID | <0963c058-5ba2-474b-8a18-6d3decb5889c@googlegroups.com> |
| In reply to | #32796 |
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 On Tuesday, November 6, 2012 12:22:44 PM UTC+8, alex23 wrote: > On Nov 6, 1:32 pm, Wincent <ronggui.hu...@gmail.com> wrote: > > > 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. > > > > > > >>> import time > > > >>> tstr = str(time.localtime()) > > > >>> eval(tstr) > > > > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-11-06 15:38 +1100 |
| Message-ID | <mailman.3307.1352176730.27098.python-list@python.org> |
| In reply to | #32797 |
On Tue, Nov 6, 2012 at 3:29 PM, Wincent <ronggui.huang@gmail.com> wrote: > 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.time() 1352176547.787 >>> time.gmtime(1352176547.787) 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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-11-05 23:42 -0500 |
| Message-ID | <mailman.3308.1352176985.27098.python-list@python.org> |
| In reply to | #32797 |
On 11/05/2012 11:29 PM, Wincent wrote: (Please don't top-post. it messes everything up. And your use of Google-groups is making everything double-spaced) > 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. > Then restate your problem in terms of describing these strings. Apparently you don't get them from str(time.localtime()), but from some redis methodology. To convert most reasonable strings back to a date/time, you can probably use strftime. But you certainly cannot reasonably use eval. You're lucky it didn't work, so you didn't leave it that way. Eval on data stored in some database? You gotta be kidding. <snip all the stuff that followed your message, since you obviously didn't care about it> -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2012-11-06 00:06 -0500 |
| Message-ID | <mailman.3309.1352178382.27098.python-list@python.org> |
| In reply to | #32794 |
On Mon, 5 Nov 2012 19:32:41 -0800 (PST), Wincent
<ronggui.huang@gmail.com> declaimed the following in
gmane.comp.python.general:
> 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"...
> >>> import time
> >>> tstr = str(time.localtime())
Did you look at what you are doing there?
>>> import time
>>> time.localtime()
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)
>>> str(time.localtime())
'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.
> >>> eval(tstr)
> 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((2012, 11, 5, 23, 47, 32, 0, 310, 0))
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.mktime((2012, 11, 5, 23, 47, 32, 0, 310, 0))
1352177252.0
>>> t2 = time.localtime()
>>> t2
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)
>>> time.mktime(t2)
1352178232.0
>>>
But really, what do you mean by "representation of time"?
>>> time.asctime()
'Tue Nov 06 00:05:39 2012'
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web