2010-02-03 29 views
10

如何将namedtuples扩展或使用许多其他@properties进行子类化?
对于少数人,可以只写下面的文字;但有很多, 所以我正在寻找一个发电机或物业工厂。 一种方法是从_fields生成文本并执行它; 另一个是在运行时具有相同效果的add_fields。
(我@props是让行和字段 在数据库中分散在多个表, 使rec.pnamepersontable[rec.personid].pname; 但namedtuples与 - 智能领域将有其他用途了。)用多个@properties扩展名为tuple的Python吗?

""" 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 
+2

难道你不是在那里回答自己的问题吗? – 2010-02-03 15:36:19

+0

我不明白这个问题。也许你想要属性显示在元组中?如果你想要的话,覆盖getitem。 – Pepijn 2010-02-03 15:42:28

+1

我也很困惑。你似乎正在做你应该得到你所问的效果。你有什么问题? – Omnifarious 2010-02-03 15:51:59

回答

13

回答你的问题

如何namedtuples延长或子类额外@properties

是:正是你这样做的方式!你遇到了什么错误?看到一个更简单的情况,

>>> class x(collections.namedtuple('y', 'a b c')): 
... @property 
... def d(self): return 23 
... 
>>> a=x(1, 2, 3) 
>>> a.d 
23 
>>> 
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) 
0

这里有一种方法,一点点语言: 把它变成像上面那样的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 
相关问题