如何用多个@property扩展Python namedtuple?
如何给命名元组(namedtuple)添加很多额外的属性呢?
如果只是几个属性,可以直接写代码;但如果有很多属性,我就想找个生成器或者属性工厂来帮忙。
一种方法是从 _fields
生成代码,然后执行它;另一种方法是在运行时用一个叫 add_fields
的函数来实现同样的效果。
(我的属性是用来获取数据库中分散在多个表里的行和字段,所以 rec.pname
实际上是 persontable[rec.personid].pname
;不过带有智能字段的命名元组也可以有其他用途。)
""" extend namedtuple with many @properties ? """
from collections import namedtuple
Person = namedtuple( "Person", "pname paddr" ) # ...
persontable = [
Person( "Smith", "NY" ),
Person( "Jones", "IL" )
]
class Top( namedtuple( "Top_", "topid amount personid" )):
""" @property
.person -> persontable[personid]
.pname -> person.pname ...
"""
__slots__ = ()
@property
def person(self):
return persontable[self.personid]
# def add_fields( self, Top.person, Person._fields ) with the same effect as these ?
@property
def pname(self):
return self.person.pname
@property
def paddr(self):
return self.person.paddr
# ... many more
rec = Top( 0, 42, 1 )
print rec.person, rec.pname, rec.paddr
3 个回答
-2
这里有一种方法,就是用一种简单的语言:把这个转换成像上面那样的Python代码,然后执行它。
(把文本转换成文本是很简单的,而且也容易测试——你可以查看中间的文本。)
我相信还有类似的,虽然不那么简单的,能分享一些链接吗?
# example of a little language for describing multi-table databases 3feb
# why ?
# less clutter, toprec.pname -> persontable[toprec.personid].pname
# describe in one place: easier to understand, easier to change
Top:
topid amount personid
person: persontable[self.personid] + Person
# toprec.person = persontable[self.personid]
# pname = person.pname
# locid = person.locid
# todo: chaining, toprec.city -> toprec.person.loc.city
Person:
personid pname locid
loc: loctable[self.locid] + Loc
Loc:
locid zipcode province city
2
这个怎么样?
class Top( namedtuple( "Top_", "topid amount personid" )):
""" @property
.person -> persontable[personid]
.pname -> person.pname ...
"""
__slots__ = ()
@property
def person(self):
return persontable[self.personid]
def __getattr__(self,attr):
if attr in Person._fields:
return getattr(self.person, attr)
raise AttributeError("no such attribute '%s'" % attr)
19
你问的问题的答案是:
如何给命名元组添加或扩展额外的
@properties
呢?
答案就是:你现在的做法完全正确!你遇到了什么错误呢?为了让你更容易理解,
>>> class x(collections.namedtuple('y', 'a b c')):
... @property
... def d(self): return 23
...
>>> a=x(1, 2, 3)
>>> a.d
23
>>>