Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
named tuples
#21
i do understand how to format the output once i understand what output i actually need to produce and understand the information used to produce it, and how to get it.  the "how to get it" could be done with repr(), by either using that result as-is (not these two cases) or parsing it apart to extract information (such as a list of names of items).  the "how to produce it" needs the understanding of what source code is needed to produce it.  in the case of sys.version_info this may be unknowable as it appears there is no way to construct that in the form of a literal, as print_object() produces.  in the case of time.struct_time, working source can be produced.  i do understand what and how.  first, the source must determine that it is this case.  that could be repr(type(object)).split("'")[1]=='time.struct_time'. then time.struct_time(object[:6]) could reconstruct it.  so the produced code might be (an example with arbitrary contents):

time.struct_time((2017,5,7,3,59,29,6,127,0))
i'll leave it as a user exercise to make code that produces that (be sure it only does it for that type and with the values it has).

see also:
Output:
lt1/forums /home/forums 3> 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 >>> time.gmtime() time.struct_time(tm_year=2017, tm_mon=5, tm_mday=7, tm_hour=3, tm_min=59, tm_sec=29, tm_wday=6, tm_yday=127, tm_isdst=0) >>> time.struct_time((2017,5,7,3,59,29,6,127,0)) time.struct_time(tm_year=2017, tm_mon=5, tm_mday=7, tm_hour=3, tm_min=59, tm_sec=29, tm_wday=6, tm_yday=127, tm_isdst=0) >>> lt1/forums /home/forums 4> 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 >>> time.gmtime() time.struct_time(tm_year=2017, tm_mon=5, tm_mday=7, tm_hour=4, tm_min=0, tm_sec=44, tm_wday=6, tm_yday=127, tm_isdst=0) >>> time.struct_time((2017,5,7,4,0,44,6,127,0)) time.struct_time(tm_year=2017, tm_mon=5, tm_mday=7, tm_hour=4, tm_min=0, tm_sec=44, tm_wday=6, tm_yday=127, tm_isdst=0) >>> lt1/forums /home/forums 5>[/python] but, [i]sys.version_info[/i] does not work like this. [output]lt1/forums /home/forums 7> 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 >>> sys.version_info sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0) >>> sys.version_info((3,5,2,'final',0)) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: 'sys.version_info' object is not callable >>> lt1/forums /home/forums 8>
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