def __init__(self, uid=None, **kw): if self.unique: uid = self.__class__.__name__ elif uid is None: uid = next_available_uid() self._saved = {} self._history = {} self.uid = uid self.settings = {} self.class_name = getattr(self.__class__, 'role', self.__class__.__name__) self._cd = 0 # / CREATED / DELETED self._can_be_deleted_with_undo = True self._skip_this = False # temporary "ghost" objects can use this flag to avoid being stored
def copy(self): """ Make a new object of same type and copy its attributes. object class can define dont_copy, list of attribute names that shouldn't be copied ( either because they refer to peers or objects above, or because they are handled manually.). Attributes starting with '_' are always ignored, and the copied object is assigned a new key. :return: """ if self.__class__.unique: print('cannot copy unique object') return None new_obj = self.__class__() new_obj.uid = next_available_uid() dont_copy = self.__class__.dont_copy for key, value in vars(self).items(): if (not key.startswith('_')) and key not in dont_copy: if hasattr(value, 'copy'): new_value = value.copy() else: new_value = copy.copy(value) setattr(new_obj, key, new_value) return new_obj