-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 03/19/2014 07:49 AM, Anurag wrote:
> I faced the similar problem Traversal fails as inherited class from > OOBTree doesn't persist __name__, __parent__. Similarly you can't have > other attributes also as they will not be persisted. Classes > inheriting from PersistentMapping don't have this problem and you can > have other attributes also in them apart from key/value stored in > mapping. You are correct: the '__getstate__' / '__setstate__' implementations for BTrees don't handle instance attributes outside the tree data structures. > I have defined my class to circumvent the problem ( may be for other > attributes similar approach can be taken up) class > ItemContainer(OOBTree.BTree): def __init__(self,name,parent): > super(ItemContainer,self).__init__() > > self.__name__=name self.__parent__=parent > > def additem(self,item): self[item.__name__] = item > item.__parent__=self @property def __name__(self): return > self["name"] > > @__name__.setter def __name__(self, value): self["name"] = value > > @property def __parent__(self): return self["parent"] > > @__parent__.setter def __parent__(self, value): self["parent"] = > value > > Please let me know if there is a problem in this approach. A couple of issues: - - Storing the '__name__' and '__parent__' values in the tree itself puts them in the same namespace as the items, which is probably not ideal (what if a user wants to create an item named 'parent'?) - - It won't work at all for the trees which expect constrained values (ints / floats / longs). Such trees are probably not in scope for Pyramid's normal traversal use cases, though. A better solution is to wrap the tree in another object which handles storing those attributes, and proxies item access to the tree (this is the approach taken by repoze.folder and SubstanceD). Note that overriding '__getstate__' / '__setstate__' to save the attributes is problematic because the "inner" tree nodes won't ever have them. It would *work* (assuming you worked out the details) but would be inefficient for large trees. Tres. - -- =================================================================== Tres Seaver +1 540-429-0999 [email protected] Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlMpqbMACgkQ+gerLs4ltQ5QzACeMsIGNfn2yIHjrtozPoH8YKtk gPoAoMUHHFM2PubwcgdWEbQl9F728MX/ =uWIN -----END PGP SIGNATURE----- -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pylons-discuss. For more options, visit https://groups.google.com/d/optout.
