def from_dict(cls, d): ''' Like Identity.from_dict, but will cast the class type based on the format. i.e. if the format is hdf, return an HdfPartitionIdentity :param d: :return: ''' from partition import identity_class_from_format_name ic = identity_class_from_format_name(d.get('format', 'db')) name = ic._name_class(**d) if 'id' in d and 'revision' in d: # The vid should be constructed from the id and the revision on = (ObjectNumber.parse(d['id']).rev(d['revision'])) elif 'vid' in d: on = ObjectNumber.parse(d['vid']) else: raise ValueError("Must have id and revision, or vid") try: return ic(name, on) except TypeError as e: raise TypeError("Failed to make identity from \n{}\n: {}".format(d, e.message))
def new_subclass(cls, name, object_number): from partition import identity_class_from_format_name, name_class_from_format_name nc = name_class_from_format_name(name.format) ic = identity_class_from_format_name(name.format) nname = nc(**name.dict) return ic(nname, object_number)
def as_partition(self, partition=0, **kwargs): '''Return a new PartitionIdentity based on this Identity :param partition: Integer partition number for PartitionObjectNumber :param kwargs: ''' from partition import identity_class_from_format_name assert type(self._name) == Name, "Wrong type: {}".format(type(self._name)) assert type(self._on) == DatasetNumber, "Wrong type: {}".format(type(self._on)) name = self._name.as_partition(**kwargs) on = self._on.as_partition(partition) ic = identity_class_from_format_name(name.format) return ic(name,on)