objectified elements should be comparable
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Triaged
|
Wishlist
|
Unassigned |
Bug Description
Hi,
Currently, objectified element comparison operators are not defined, therefore the default ones apply (i.e. based on identities/memory addresses).
Given the purpose of objectify, "The main idea is to hide the usage of XML behind normal Python objects, sometimes referred to as data-binding. It allows you to use XML as if you were dealing with a normal Python object hierarchy.", we were expecting that deep object comparison was implemented.
One rationale is that we've been extensively using object (sub)tree comparison for testing purposes.
Something very simple would do the job, like (Python syntax):
def key_of(element):
try:
return (element.tag, element.attrib, element.__dict__) #as well as any other relevant member from an object point of view
except AttributeError: #not an element
return id(instance)
class ...Element
....
def __cmp__(self, other):
return cmp(key_of(self), key_of(other))
....
def __eq__(self, other):
return key_of(self) == key_of(other)
....
Thanks for considering this feature request.
Br,
Regis
## -- PLEASE PROVIDE THE FOLLOWING INFORMATION: --
Python : sys.version_
lxml.etree : (3, 3, 3, 0)
libxml used : (2, 9, 1)
libxml compiled : (2, 9, 1)
libxslt used : (1, 1, 28)
libxslt compiled : (1, 1, 28)
## -----
I guess the semantics are not entirely obvious here, since there might be special cases where the ObjectifiedData Element class and the underlying data type mismatch in behaviour. But I would certainly consider a pull request that implements this and that proves with tests that a safe and desirable thing to do.