From: Ivan V. i B. <iv...@ca...> - 2005-01-27 09:17:16
|
On Wed, Jan 26, 2005 at 03:43:19PM +0100, Norbert Nemec wrote: > Am Mittwoch, 26. Januar 2005 10:52 schrieb Ivan Vilata i Balaguer: > > Having said that, I keep the opinion that node creation, removal and > > renaming should belong in Group methods and nowhere else. Bye! >=20 > For renaming and removal, I agree with you. For creation however, I don't= =2E=20 > Making node creation a group method makes the whole system non-extensible= =2E=20 > The Group class has to contain a createSomething method for every kind of= =20 > Node that it might contain. Currently, we already have Array, EArray, Tab= le,=20 > Group and VLArray. In the long run, the number of possible Nodes will=20 > probably increase even more. Actually, it would make sense to allow the u= ser=20 > to define their own kinds of Nodes to extend PyTables. [...] Actually, it is possible for Group to use _f_create* methods and have no knowledge of which kinds of nodes it supports. One could register and deregister new Node classes into Group in this way: class MyNewLeaf(Leaf): ... class definition ... Group._f_registerNode(MyNewLeaf) The _f_create* method would be created on-the-fly. This can actually be done. Here is the code for an hypothetical implementation: class Group(Node): # ... # @classmethod def _f_registerNode(cls, nodeClass): nodeClassName =3D nodeClass.__name__ if not issubclass(nodeClass, Node): raise TypeError("registered class is not a subclass of Node: %s" % (nodeClassName,)) # Define the _create<Class> method. methodName =3D '_f_create%s' % (nodeClassName,) def createNode(self, name, **nodeArgs): node =3D nodeClass(name, **nodeArgs) self._g_bind(name, node) createNode.__name__ =3D methodName # Add the new method to the class. setattr(cls, methodName, createNode) _f_registerNode =3D classmethod(_f_registerNode) # @classmethod def _f_deregisterNode(cls, nodeclass): nodeClassName =3D nodeClass.__name__ methodName =3D '_f_create%s' % (nodeClassName,) if not hasattr(cls, methodName): raise ValueError("class is not registered: %s" % (nodeClassName,)) delattr(cls, methodName) _f_deregisterNode =3D classmethod(_f_deregisterNode) # ... Group._f_registerNode(Group) The only restriction to Node subclasses is that its __init__ method supports at least one argument (which already makes sense for =ABtitle=BB). Else, a **kwargs argument can be used and checked for no actual arguments. I think this implementation is reasonably simple and places very little burden on the developer (namely, calling Group._f_registerNode). It should make Group completely independent of Node subclasses. Regards, Ivan --=20 Ivan Vilata i Balaguer >qo< http://www.carabos.com/ C=E1rabos Coop. V. V V Enjoy Data "" |