objectified elements should be comparable

Bug #1462317 reported by Régis Desgroppes on 2015-06-05
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
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_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
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)
## -----

scoder (scoder) wrote :

I guess the semantics are not entirely obvious here, since there might be special cases where the ObjectifiedDataElement 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.

Changed in lxml:
importance: Undecided → Wishlist
status: New → Triaged
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers