2010-02-03 6 views
10

कई अतिरिक्त @properties के साथ नामित किए जा सकते हैं या subclassed कैसे?
कुछ के लिए, कोई भी नीचे दिए गए पाठ को लिख सकता है; लेकिन कई, हैं इसलिए मैं जनरेटर या संपत्ति कारखाने की तलाश में हूं। एक तरीका _fields से टेक्स्ट जेनरेट करना होगा और इसे निष्पादित करना होगा; दूसरा रनटाइम पर एक ही प्रभाव के साथ एक add_fields होगा।
(मेरे @props पंक्तियों और क्षेत्रों पाने के लिए एक डेटाबेस में कई तालिकाओं में फैले, ताकि rec.pnamepersontable[rec.personid].pname है कर रहे हैं; लेकिन namedtuples-साथ स्मार्ट क्षेत्रों अन्य होता भी उपयोग करता है।)कई @properties के साथ पाइथन नामकरण का विस्तार?

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

क्या आपने वहां अपने स्वयं के प्रश्न का उत्तर नहीं दिया? –

+0

मुझे प्रश्न समझ में नहीं आता है। शायद आप गुणों को टुपल में दिखाना चाहते हैं? यदि आप चाहते हैं तो गेटिटम को ओवरराइट करें। – Pepijn

+1

मैं भी उलझन में हूँ। ऐसा लगता है कि आप जिस प्रभाव के बारे में पूछ रहे हैं उसे पाने के लिए आपको वास्तव में क्या करना चाहिए। आपकी क्या समस्या हैं? – Omnifarious

उत्तर

13

अपने प्रश्न

का जवाब कैसे namedtuples बढ़ाया जा सकता है या अतिरिक्त @properties साथ subclassed?

है: बिल्कुल वैसे ही आप इसे कर रहे हैं! आप क्या त्रुटि प्राप्त कर रहे हैं? एक साधारण मामला देखने के लिए,

>>> class x(collections.namedtuple('y', 'a b c')): 
... @property 
... def d(self): return 23 
... 
>>> a=x(1, 2, 3) 
>>> a.d 
23 
>>> 
2

इसके बारे में कैसे? ऊपर की तरह अजगर पाठ में इस बारी है, और यह exec:

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

यहाँ एक दृष्टिकोण, एक छोटे से भाषा है।
(टेक्स्ट-टू-टेक्स्ट का विस्तार करना आसान है, और — का परीक्षण करना आसान है, आप इंटरमीडिएट टेक्स्ट देख सकते हैं।)
मुझे यकीन है कि अगर ऐसा नहीं है तो कृपया ऐसे ही हैं, लिंक कृपया?

# 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 
संबंधित मुद्दे