I regularly deal with problems where the children of a node are interconnected and only "make sense" together. I often need to verify and enforce this, using _{pre,post}_{attach,detach}_children(children). However, it is somewhat cumbersome to dis-allow children to be attached or detached individually using, for example, Node.parent = None (which would invalidate the tree at least temporarily, until the last child has been removed).
While I can work around this using a locking mechanism (for example, 1. set a locking variable in _pre_detach_children(children); 2. assert that variable in _pre_detach(parent); and 3. unset the variable in _post_detach_children(children)), it would be much cleaner if a user of anytree could override the default implementation of Node.parent to raise an exception.
For this, one would merely need an additional, thin layer of indirection with a protected internal parent property, say, Node._parent which is used for all internal for attaching and detaching nodes; Node.parent in its default implementation would just call Node._parent.
Any thoughts (or pointers to existing functionality I have missed), maybe @c0fec0de ?
I regularly deal with problems where the children of a node are interconnected and only "make sense" together. I often need to verify and enforce this, using
_{pre,post}_{attach,detach}_children(children). However, it is somewhat cumbersome to dis-allow children to be attached or detached individually using, for example,Node.parent = None(which would invalidate the tree at least temporarily, until the last child has been removed).While I can work around this using a locking mechanism (for example, 1. set a locking variable in
_pre_detach_children(children); 2. assert that variable in_pre_detach(parent); and 3. unset the variable in_post_detach_children(children)), it would be much cleaner if a user ofanytreecould override the default implementation ofNode.parentto raise an exception.For this, one would merely need an additional, thin layer of indirection with a protected internal parent property, say,
Node._parentwhich is used for all internal for attaching and detaching nodes;Node.parentin its default implementation would just callNode._parent.Any thoughts (or pointers to existing functionality I have missed), maybe @c0fec0de ?