Python Forum
named tuples - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: named tuples (/thread-3153.html)

Pages: 1 2 3


named tuples - Skaperen - May-02-2017

so i got a named tuple.  how can code get a list of just the names (much like getting the keys of a dict)?  the built-in dir() function (gets a list of attribute names of an object) does get the names mixed in with other attributes.  the names of a named tuple can be used as attributes so it is no surprise that a list of attributes would include them.

in my function to convert and present an object as python source code, how should i convert/form a named tuple as a source code expression that can re-create a like named tuple object?


RE: named tuples - Larz60+ - May-02-2017

type(tuple_name).__name__


RE: named tuples - Skaperen - May-02-2017

(May-02-2017, 04:55 AM)Larz60+ Wrote: type(tuple_name).__name__

this just gives me the original name of the object, not the list of keys.

type(sys.version_info).__name__ -> 'version_info' but i am wanting whatever(sys.version_info) -> ('major', 'minor', 'micro', 'releaselevel', 'serial') or something like that (a list, a set, a dict, or whatever).  please note that sys.version_info is just one possible example.  a different (any) named tuple may be involved, so don't try to focus on that one case.



you know how

d = some dictionary
[x for x in d]
gives a list of keys in the dictionary.  doing that with a named tuple gives a list of the values.  there is no .keys() for a named tuple and and dir() of the name tuple includes other attributes.  and even if you can remove the other attributes from dir(), the order is sorted, not the original order (tuples have an order).

i just know someone is going to say "dictionaries and tuples are different".  i know that.  that's why i am asking.

well... much of the real reason i am asking is to figure out how to code for them in a revision of my print_object() function in my printobject module.  i want it to produce an expression that can be embedded in python source code.


RE: named tuples - volcano63 - May-02-2017

vars()


RE: named tuples - Skaperen - May-02-2017

so i need to parse something like version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0).


RE: named tuples - Mekire - May-02-2017

Why is everything always such a fight... you don't need to parse anything.
>>> Point = namedtuple('Point', ['x', 'y'])
>>> a = Point(4,5)
>>> a
Point(x=4, y=5)
>>> a.x
4
>>> a.y
5
>>> vars(a)
OrderedDict([('x', 4), ('y', 5)])
>>> vars(a).keys()
['x', 'y']
>>>



RE: named tuples - volcano63 - May-02-2017

(May-02-2017, 09:42 AM)Mekire Wrote:
>>>
>>> vars(a).keys()
['x', 'y']
>>>
in Python 2.7 it will work that way, in 3.x
In [19]: vars(p).keys()
Out[19]: KeysView(OrderedDict([('x', 4), ('y', 5)]))

In [20]: list(vars(p).keys())
Out[20]: ['x', 'y']



RE: named tuples - Skaperen - May-03-2017

i'm hoping it will be simple.  but i am getting errors that seem to suggest i am off course.  you guys are acting like you how to do what i want to know how but are assuming i know some critical piece of info that i do not know.

Output:
lt1/forums /home/forums 10> 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 >>> a = sys.version_info >>> a sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0) >>> vars(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: vars() argument must have __dict__ attribute >>>
at some point i want to get [ 'major', 'minor', 'micro', 'releaselevel', 'serial ] or something from which i can make that.


RE: named tuples - Mekire - May-03-2017

Well, sys.version_info is not apparently a named tuple. Did you have a reason to believe it was?

It is quite easy to get the info out if you need it:
>>> import sys
>>> fields = ["major", "minor", "micro", "releaselevel", "serial"]
>>> dict(zip(fields, sys.version_info))
{'micro': 11, 'major': 2, 'releaselevel': 'final', 'serial': 0, 'minor': 7}



RE: named tuples - Skaperen - May-03-2017

(May-03-2017, 07:25 AM)Mekire Wrote: Well, sys.version_info is not apparently a named tuple.  Did you have a reason to believe it was?

i thought it was because it looked like it was.

so, what is it and what does the source code to make it look like?

my goal is to add missing object types to print_object(), at least those that come with python.