def merge(self, other): """ Merge the cross sections of two collections. Notes ----- 1. This can only merge if one hasn't been assigned at all, because it doesn't try to figure out how to account for overlapping cross sections. 2. Update the current library (self) with values from the other library if all attributes in the library except ones in `attributesToIgnore` are None. 3. Libraries are already merged if all attributes in the other library are None (This is nothing to merge!). """ attributesToIgnore = ["source", HIGHORDER_SCATTER] if all(v is None for k, v in self.__dict__.items() if k not in attributesToIgnore): self.__dict__.update(other.__dict__) # See note 2 elif all(v is None for k, v in other.__dict__.items() if k not in attributesToIgnore): pass # See note 3 else: overlappingAttrs = set(k for k, v in self.__dict__.items() if v is not None and k != "source") overlappingAttrs &= set(k for k, v in other.__dict__.items() if v is not None and k != "source") raise exceptions.XSLibraryError( "Cannot merge {} and {}.\n Cross sections overlap in " "attributes: {}.".format(self.source, other.source, ", ".join(overlappingAttrs))) raise exceptions.XSLibraryError( "Cannot merge from and from \n Cross sections overlap in " "attributes:.")
def _mergeAttributes(this, other, attrName): """Function for merging XSNuclide attributes. Notes ----- This function checks to see that the attribute has only been assigned for a single instance, and then uses uses the one that has been assigned. Returns ------- The proper value for the attribute. """ attr1 = getattr(this, attrName) attr2 = getattr(other, attrName) if attr1 is not None and attr2 is not None: raise exceptions.XSLibraryError( "Cannot merge {} and {}, the attribute `{}` has been assigned on both" "instances.".format(this, other, attrName)) return attr1 if attr1 is not None else attr2
def __setitem__(self, key, value): if key in self._orderedNuclideLabels: raise exceptions.XSLibraryError("{} already contains {}".format( self, key)) value.container = self self._orderedNuclideLabels.append(key)