[lxml-dev] Architecture/best practice question.

Hi everybody, a bit of a general architecture/best practice question. Say you want to keep in sync an ElementTree with a separate tree structure, one that is parallel but does not have the exact same nodes and yet needs to be informed and updated whenever a change in the ElementTree occurs. ElementTree supports custom elements and I guess it wouldn't be too difficult to override the standard methods of an element to do something before or after any change. -However-, I understand that ElementProxies cannot store instance-level data as the instances are not persistent and are garbage collected more or less as soon as they are no longer referenced somewhere. So, what I'm wondering is, how do I tell a method of a custom element what object in the parallel structure to inform whenever a change arises? I guess one way would be to store at -class level- (or where else?) a dictionary mapping custom ElementProxies instances to nodes of the parallel structure. In so doing whenever a custom method is executed it can get hold of the parallel structure. Is that a reasonable way to do it or are there better ones? Manu

Sorry folks, I'm posting this message again just in case it slipped under everybody's radar. Please let me know if my question isn't clear enough or if there are problems with any of its premises. ---------- Forwarded message ---------- From: Emanuele D'Arrigo <manu3d@gmail.com> Date: 26 February 2010 13:13 Subject: Architecture/best practice question. To: lxml-dev@codespeak.net Hi everybody, a bit of a general architecture/best practice question. Say you want to keep in sync an ElementTree with a separate tree structure, one that is parallel but does not have the exact same nodes and yet needs to be informed and updated whenever a change in the ElementTree occurs. ElementTree supports custom elements and I guess it wouldn't be too difficult to override the standard methods of an element to do something before or after any change. -However-, I understand that ElementProxies cannot store instance-level data as the instances are not persistent and are garbage collected more or less as soon as they are no longer referenced somewhere. So, what I'm wondering is, how do I tell a method of a custom element what object in the parallel structure to inform whenever a change arises? I guess one way would be to store at -class level- (or where else?) a dictionary mapping custom ElementProxies instances to nodes of the parallel structure. In so doing whenever a custom method is executed it can get hold of the parallel structure. Is that a reasonable way to do it or are there better ones? Manu

Hi,
Just an idea: If it's ok for you to actually modify the referencing tree what about storing a unique xpath to the referenced node of the other tree, e.g. in an XML attribute? Holger -- Sicherer, schneller und einfacher. Die aktuellen Internet-Browser - jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/chbrowser

Emanuele D'Arrigo, 26.02.2010 14:13:
I don't feel like having enough information to give good advice. I'm not aware of any ready-made solution here, so the 'best practices' certainly lack statistical evidence, but you can try to use the generated XPath of the Element as a key, or keep the Element proxies alive, as you already suggested (but I expect that to be more cumbersome). http://codespeak.net/lxml/xpathxslt.html#generating-xpath-expressions Stefan

Sorry folks, I'm posting this message again just in case it slipped under everybody's radar. Please let me know if my question isn't clear enough or if there are problems with any of its premises. ---------- Forwarded message ---------- From: Emanuele D'Arrigo <manu3d@gmail.com> Date: 26 February 2010 13:13 Subject: Architecture/best practice question. To: lxml-dev@codespeak.net Hi everybody, a bit of a general architecture/best practice question. Say you want to keep in sync an ElementTree with a separate tree structure, one that is parallel but does not have the exact same nodes and yet needs to be informed and updated whenever a change in the ElementTree occurs. ElementTree supports custom elements and I guess it wouldn't be too difficult to override the standard methods of an element to do something before or after any change. -However-, I understand that ElementProxies cannot store instance-level data as the instances are not persistent and are garbage collected more or less as soon as they are no longer referenced somewhere. So, what I'm wondering is, how do I tell a method of a custom element what object in the parallel structure to inform whenever a change arises? I guess one way would be to store at -class level- (or where else?) a dictionary mapping custom ElementProxies instances to nodes of the parallel structure. In so doing whenever a custom method is executed it can get hold of the parallel structure. Is that a reasonable way to do it or are there better ones? Manu

Hi,
Just an idea: If it's ok for you to actually modify the referencing tree what about storing a unique xpath to the referenced node of the other tree, e.g. in an XML attribute? Holger -- Sicherer, schneller und einfacher. Die aktuellen Internet-Browser - jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/chbrowser

Emanuele D'Arrigo, 26.02.2010 14:13:
I don't feel like having enough information to give good advice. I'm not aware of any ready-made solution here, so the 'best practices' certainly lack statistical evidence, but you can try to use the generated XPath of the Element as a key, or keep the Element proxies alive, as you already suggested (but I expect that to be more cumbersome). http://codespeak.net/lxml/xpathxslt.html#generating-xpath-expressions Stefan
participants (3)
-
Emanuele D'Arrigo
-
jholg@gmx.de
-
Stefan Behnel