[Python-ideas] namedlist() or Record type
anatoly techtonik
techtonik at gmail.com
Wed Jul 24 09:50:51 CEST 2013
Rationale: There are many 2D data with table layout where order is
important and it is hard to work with them in Python.
The use case:
1. Get data row from a table
2. Change row column by name
3. Save data back
For example, termios.tcgetattr() returns list [iflag, oflag, cflag,
lflag, ispeed, ospeed, cc]. I need to modify some bits in lflag value
and save this list back. In my code it looks like:
newattr[3] &= ~termios.ICANON
I want to get rid of magic number 3 without defining additional
variables just for names. I thought that namedtuple can help here, but
it is read-only and seems not serializeable.
What I want is:
>>> data = [1, 'john', 23434]
>>> named = Record(data, ['id', 'name', 'number'])
>>> named.id = 3
>>> named.name
'john'
>>> named
[3, 'john', 23434]
>>> named.naem
AttributeError(...)
>>> named.dict()
{'id': 3, 'name': john, 'number': 23434}
>>> named.json()
...
I hacked OrderedDict to accept list as param and allow attribute
access. It doesn't behave as named list - you still need to call
.values() to get list back, and indexed access doesn't work as well.
http://bugs.python.org/file31026/DictRecord.py
--
anatoly t.
More information about the Python-ideas
mailing list