SPPAS 4.22

https://sppas.org/

Module sppas.src.wkps

Class sppasCatReference

Description

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

Constructor of the sppasCatReference class.

Parameters
  • identifier: (str) identifier for the object, the name of the reference
View Source
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

Public functions

att

Return the attribute matching the given identifier or None.

Parameters
  • identifier: (str) Id of a sppasRefAttribute
Returns
  • sppasRefAttribute or None if the identifier does not match any attribute of this reference.
View Source
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

add

Append an attribute into the reference.

Parameters
  • identifier: (str) Id of a sppasRefAttribute
  • value: (any type)
  • att_type: (str) One of 'str', 'bool', 'int', 'float'. Default is 'str'.
  • descr: (str) A text to describe the attribute
Raises

AttributeIdValueError

View Source
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

Append an attribute into a reference.

Parameters
  • att: (sppasRefAttribute)
View Source
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)

pop

Delete an attribute of this reference.

Parameters
  • identifier: (str, sppasRefAttribute) the attribute or its id to delete
View Source
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_state

Set the current state to a new one.

Parameters
  • state: (State)
Raises

(sppasTypeError)

View Source
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')

get_type

Returns the type of the Reference.

View Source
def get_type(self):
    """Returns the type of the Reference."""
    return self.__type

set_type

Set the type of the Reference within the authorized ones.

Parameters
  • ann_type: (int) One of the annots.types
Raises

sppasIndexError, sppasTypeError

View Source
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)

Overloads

__len__

View Source
def __len__(self):
    return len(self.__attributs)

__str__

View Source
def __str__(self):
    return '{:s}: {!s:s}'.format(self.id, self.__attributs)

__repr__

View Source
def __repr__(self):
    return '{:s}: {!s:s}'.format(self.id, self.__attributs)

__format__

View Source
def __format__(self, fmt):
    return str(self).__format__(fmt)

__iter__

View Source
def __iter__(self):
    for att in self.__attributs:
        yield att

__contains__

Return true if self contains the given attribute/identifier.

Parameters
  • att: (str or sppasRefAttribute)
View Source
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