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.
named tuples
|
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:20 AM
(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: which is the kind of "parsing" i was referring to.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
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.
May-03-2017, 08:54 AM
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.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
May-05-2017, 02:45 AM
is time.struct_time a named tuple?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
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(), andYou 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
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
May-05-2017, 05:06 PM
why look beyond the built-in?
def zingo(): return 1, 2, 3 print('zingo: {}'.format(zingo()))results:
...to get something that produces the correct output. the format method does not. adding the zingo function has no effect in that case.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
May-06-2017, 02:00 PM
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. |
|