Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
named tuples
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
type(tuple_name).__name__
Reply
#3
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
vars()
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
so i need to parse something like version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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']
>>>
Reply
#7
(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']
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
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}
Reply
#10
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020