Python Forum

Full Version: named tuples
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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.

i really didn't understand your post.  i don't even understand this one.  you are saying it can't be done but your code does it easily?  to build a dict you need to get the keys.  how do you get the keys?   apparently we can't use vars(").  do you know of anything simpler than:

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.
I'm saying the "keys" are always the same for a 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...
Now I have a dict of those keys to their values and can do whatever I want with it.
i won't know what they (the keys) are until i figure out the object the caller provides.  i've seen another object like this before.  then i need to see what else my code might run across.
is time.struct_time a named tuple?

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>
It is very similar, but not exactly the same as the C structure tm.
There are differences such as tm_year, the value in python is the actual year, not
year -1900.

The official desctiption is:
Quote:The time value as returned by gmtime(), localtime(), and strptime(), and
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.
You can get full field descriptions in an interactive session by:
importing time
and calling help(''time.struct_time')
my concern is how to deal with these things that look like tuples in my print_object() function and other similar things in other projects.  i guess i will apply my other tests and use repr() on things that don't match up to that point.  at things like time.localtime() and sys.version_info() fail the 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.

see: https://python-forum.io/Thread-printobject-py
why look beyond the built-in?
def zingo():
   return 1, 2, 3
print('zingo: {}'.format(zingo()))
results:
Output:
zingo: (1, 2, 3)
...to get something that produces the correct output.  the format method does not.  adding the zingo function has no effect in that case.
The format statement can be controlled same as printf in C.
The arguments are almost (perhaps exact, I have never checked) as C,
s for string, f for float, etc. individual cells precision etc can all be controlled.
FYI zingo had nothing to do with previous post, other than to serve a tuple.
Pages: 1 2 3