SoilPulse / MetadataGenerator

The metadata generator guides researchers to the creation of reusable data sets in a webapp.
GNU General Public License v3.0
0 stars 0 forks source link

add comparison of project manager instances #37

Open jonaslenz opened 1 month ago

jonaslenz commented 1 month ago

Hi Honza, would it be possible to compare instances of the Project Manager? - e.g. if the state loaded from DB equals the local one?

I found this stackexchange solution for it: https://stackoverflow.com/questions/1227121/compare-object-instances-for-equality-by-their-attributes

and tried to add this to the PorjectManager class:

    def __eq__(self, other):
        if not isinstance(other, ProjectManager):
            # don't attempt to compare against unrelated types
            return NotImplemented
        comp = True
        for attribute in dir(self):
            if not getattr(self, attribute) == getattr(other, attribute):
                comp = False

        return comp

but it fails.

I further tested with more informative output, which attributes are not equal:

    def __eq__(self, other):
        if not isinstance(other, ProjectManager):
            # don't attempt to compare against unrelated types
            return NotImplemented
        comp = True
        for attribute in dir(self):
            if not getattr(self, attribute) == getattr(other, attribute):
                print("diff at " + attribute)
                comp = False
            else:
                print("  good at "+attribute)
        return comp
jandevaty commented 2 weeks ago

The main problem here lies in the attributes holding other object instances. We can easily compare attributes that are string or numbers but for other object instances it is necessary to have all of the classes to have the eq method manualy defined with appropriate cascading of the instances comparison within the ProjectManager.eq method.

So by excluding the object instance holding attributes we can easily get match on project that differs substantially in its attribute structure. And to achieve valid comparison I need to implement eq to basicly all object classes there are in project_management.py and all their subclasses.