ladybug-tools / honeybee-core

:honeybee: honeybee core library
https://www.ladybug.tools/honeybee-core/docs/
GNU Affero General Public License v3.0
14 stars 14 forks source link

HB Obj Duplicate Properties to allow for more than .energy and .radiance #495

Closed ed-p-may closed 2 years ago

ed-p-may commented 2 years ago

When calling .duplicate() on an HB Object (Room, etc) during __copy__(), the object's properties also get duplicated using the _duplicate_extension_attr method, called by the 'new' object, with the original object's properties passed in as the argument.

    def __copy__(self):
        new_r = Room(self.identifier, tuple(face.duplicate() for face in self._faces))
        ...
        new_r._properties._duplicate_extension_attr(self._properties) #<--- 
        return new_r

To duplicate the property object, within the _duplicate_extension_attr() the code then steps through the self._extension_attributes. However, this assumes that the property object names to duplicate already exist on the 'new' object (self) which is true for .energy and .radiance, but would not be true for any custom user-determined property objects other than .radiance and .energy. (Unless the parent classes have been modified / sub-classed and the new custom properties added ahead of time.)

    def _duplicate_extension_attr(self, original_properties):
        for atr in self._extension_attributes: # <------
            var = getattr(original_properties, atr)
            if not hasattr(var, 'duplicate'):
                continue
            try:
                setattr(self, '_' + atr, var.duplicate(self.host))
            except Exception as e:
                ...

By revising self._extension_attributes to original_properties._extension_attributes, the HB Object's duplicate() can now work for any properties object present on the HB-Obj. This allows for properties other than .radiance and .energy. to be added (assuming it passes the hasattr(..., ,'duplicate') guard).

Note that I think this will only result in a duplication of a user-added property object which is private ("_" prefix) and no duplication of any public getter? I'm not sure if that is ok or not? It seems that for user-added properties, having them always private is ok? Or if properties are supposed to be 'registering' with the host class somehow prior to any of this duplication code, this may be irrelevant, as then they would already be present on the self in the above example?

chriswmackey commented 2 years ago

I know we agreed on Basecamp that there's a better way of setting up the .ph extension so that we do not need this. So I'm closing this one.