Represent a reference in the catalogue of a workspace.
Reference is a dictionary with a name. Its keys are only alphanumerics characters spaced with underscores and its values are all sppasRefAttribute objects.
Represent a reference in the catalogue of a workspace.
Reference is a dictionary with a name. Its keys are only alphanumerics characters spaced with underscores and its values are all sppasRefAttribute objects.
Constructor of the sppasCatReference class.
def __init__(self, identifier):
"""Constructor of the sppasCatReference class.
:param identifier: (str) identifier for the object, the name of the reference
"""
super(sppasCatReference, self).__init__(identifier)
self.__attributs = list()
self.__type = annots.types[0]
self.subjoined = None
Return the attribute matching the given identifier or None.
def att(self, identifier):
"""Return the attribute matching the given identifier or None.
:param identifier: (str) Id of a sppasRefAttribute
:return: sppasRefAttribute or None if the identifier does not match
any attribute of this reference.
"""
su = sppasUnicode(identifier)
identifier = su.unicode()
for a in self.__attributs:
if a.get_id() == identifier:
return a
return None
Append an attribute into the reference.
AttributeIdValueError
def add(self, identifier, value=None, att_type=None, descr=None):
"""Append an attribute into the reference.
:param identifier: (str) Id of a sppasRefAttribute
:param value: (any type)
:param att_type: (str) One of 'str', 'bool', 'int', 'float'. Default is 'str'.
:param descr: (str) A text to describe the attribute
:raise: AttributeIdValueError
"""
self.append(sppasRefAttribute(identifier, value, att_type, descr))
Append an attribute into a reference.
def append(self, att):
"""Append an attribute into a reference.
:param att: (sppasRefAttribute)
"""
if isinstance(att, sppasRefAttribute) is False:
raise sppasTypeError(att, 'sppasRefAttribute')
if att in self:
raise FileAddValueError(att.get_id())
self.__attributs.append(att)
Delete an attribute of this reference.
def pop(self, identifier):
"""Delete an attribute of this reference.
:param identifier: (str, sppasRefAttribute) the attribute or its id to delete
"""
if identifier in self:
if isinstance(identifier, sppasRefAttribute) is False:
identifier = self.att(identifier)
self.__attributs.remove(identifier)
else:
raise FileRemoveValueError(identifier)
Set the current state to a new one.
(sppasTypeError)
def set_state(self, state):
"""Set the current state to a new one.
:param state: (State)
:raises (sppasTypeError)
"""
if isinstance(state, int):
self._state = state
else:
raise sppasTypeError(state, 'States')
Returns the type of the Reference.
def get_type(self):
"""Returns the type of the Reference."""
return self.__type
Set the type of the Reference within the authorized ones.
sppasIndexError, sppasTypeError
def set_type(self, ann_type):
"""Set the type of the Reference within the authorized ones.
:param ann_type: (int) One of the annots.types
:raise: sppasIndexError, sppasTypeError
"""
if ann_type in annots.types:
self.__type = ann_type
else:
try:
ref_index = int(ann_type)
if ref_index in range(0, len(annots.types)):
self.__type = annots.types[ref_index]
else:
raise sppasIndexError(ref_index)
except:
raise sppasTypeError(ann_type, annots.types)
def __len__(self):
return len(self.__attributs)
def __str__(self):
return '{:s}: {!s:s}'.format(self.id, self.__attributs)
def __repr__(self):
return '{:s}: {!s:s}'.format(self.id, self.__attributs)
def __format__(self, fmt):
return str(self).__format__(fmt)
def __iter__(self):
for att in self.__attributs:
yield att
Return true if self contains the given attribute/identifier.
def __contains__(self, att):
"""Return true if self contains the given attribute/identifier.
:param att: (str or sppasRefAttribute)
"""
if isinstance(att, sppasRefAttribute) is False:
try:
att = sppasRefAttribute(att)
except:
return False
for a in self.__attributs:
if a.get_id() == att.get_id():
return True
return False