namedtuple

Adding docstrings to namedtuples in Python?

Hello, Is it possible to add a documentation string to a namedtuple in an easy manner? I tried from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) """ A point in 2D space """ # Yet another test """ A(nother) point in 2D space """ Point2 = namedtuple("Point2", ["x", "y"]) print Point.__doc__ # -> "Point(x, y)...

Python: Using namedtuple._replace with a variable as a fieldname.

Can I reference a namedtuple fieldame using a variable? from collections import namedtuple import random Prize = namedtuple("Prize", ["left", "right"]) this_prize = Prize("FirstPrize", "SecondPrize") if random.random() > .5: choice = "left" else: choice = "right" #retrieve the value of "left" or "right" depending on the ch...

Python: namedtuple._replace() doesn't work as descrbed in the documentation.

I was having trouble implementing 'namedtuple._replace()', so I copied the code right off of the documentation: Point = namedtuple('Point', 'x,y') p = Point(x=11, y=22) p._replace(x=33) print p and I got: Point(x=11, y=22) instead of: Point(x=33, y=22) as is shown in the doc. I'm using Python 6.2 on Windows 7 edit: oops- tha...

Python: how to check if an object is an instance of a namedtuple?

How do I check if an object is an instance of a Named tuple? ...

Python tables <-> sqlite Rosetta stone ?

Is there a "Rosetta stone" which shows how to use Python tables of namedtuples in sqlite3 ? Say we have a namedtuple Row and a list or dict rows: Row = namedtuple( "Row", "name city x y" ) rows = [None, Row( "Jones", "Boston", 3, 4 ), Row(...) ... ] # translate the following to sqlite please -- rows[id] rows[id] = Row(...) rows.ap...

extend Python namedtuple with many @properties ?

How can namedtuples be extended or subclassed with many additional @properties ? For a few, one can just write the text below; but there are many, so I'm looking for a generator or property factory. One way would be to generate text from _fields and exec it; another would be an add_fields with the same effect at runtime. (My @props are t...

What is a nicer alternative to a namedtuples _replace?

Take this code: >>> import urlparse >>> parts = urlparse.urlparse('http://docs.python.org/library/') >>> parts = parts._replace(path='/3.0'+parts.path) parts._replace works but as it is an underscored method, it's supposed to be internal, and not used. Is there an alternative? I don't want to do: >>> parts = parts[:2] + ('/3.0'+parts...

Structure accessible by attribute name or index options

I am very new to Python, and trying to figure out how to create an object that has values that are accessible either by attribute name, or by index. For example, the way os.stat() returns a stat_result or pwd.getpwnam() returns a struct_passwd. In trying to figure it out, I've only come across C implementations of the above types. No...

What are "named tuples" in Python?

Reading the changes in Python 3.1, I found something... unexpected: The sys.version_info tuple is now a named tuple: I never heard about named tuples before, and I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). I never expected they could be indexed both ways. Thus, my qu...

Modifying a namedtuple's constructor arguments via subclassing?

I want to create a namedtuple which represents the individual flags in a short bitfield. I'm trying to subclass it so that I can unpack the bitfield before the tuple is created. However, my current attempt isn't working: class Status(collections.namedtuple("Status", "started checking start_after_check checked error paused queued loade...