May-03-2017, 08:02 AM
Well, there doesn't seem to be a way to get the names of the fields from it directly, but they are always in the same order so you can build a dict easily as I showed in the previous post.
(May-03-2017, 08:02 AM)Mekire Wrote: [ -> ]Well, there doesn't seem to be a way to get the names of the fields from it directly, but they are always in the same order so you can build a dict easily as I showed in the previous post.
Output:lt1/forums /home/forums 12> py3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> [x.split('=')[0] for x in repr(sys.version_info).split('(')[1].split(')')[0].split(', ')]
['major', 'minor', 'micro', 'releaselevel', 'serial']
>>>
which is the kind of "parsing" i was referring to.sys.version_info
object. So if you have such an object you just zip the values with the known "keys".>>> import sys >>> fields = ["major", "minor", "micro", "releaselevel", "serial"] >>> dict(zip(fields, sys.version_info)) {'micro': 11, 'major': 2, 'releaselevel': 'final', 'serial': 0, 'minor': 7}You don't need to parse out the keys if you already know what they are...
Output:lt1/forums /home/forums 1> py2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print(repr(time.gmtime()))
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=5, tm_hour=2, tm_min=43, tm_sec=5, tm_wday=4, tm_yday=125, tm_isdst=0)
>>>
lt1/forums /home/forums 2> py3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print(repr(time.gmtime()))
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=5, tm_hour=2, tm_min=43, tm_sec=28, tm_wday=4, tm_yday=125, tm_isdst=0)
>>>
lt1/forums /home/forums 3>
Quote:The time value as returned by gmtime(), localtime(), and strptime(), andYou can get full field descriptions in an interactive session by:
accepted by asctime(), mktime() and strftime(). May be considered as a
sequence of 9 integers.
Note that several fields' values are not the same as those defined by
the C language standard for struct tm. For example, the value of the
field tm_year is the actual year, not year - 1900. See individual
fields' descriptions for details.
instance(,tuple)
test so i can apply repr() to them. my goal is to output reconstructive source code. there is a way (call the class with a list of values) to reconstruct time.struct_time (so i need to test for this and handle it as a special case) but not sys.version_info. so they are not alike types.def zingo(): return 1, 2, 3 print('zingo: {}'.format(zingo()))results:
Output:zingo: (1, 2, 3)