https://sppas.org/

SPPAS 4.16

Module sppas.src.anndata

Class sppasCtrlVocab

Description

Generic representation of a controlled vocabulary.

A controlled Vocabulary is a set of tags. It is used to restrict the use

of tags in a label: only the accepted tags can be set to a label.

A controlled vocabulary is made of an identifier name, a description and

a list of pairs tag/description.

Constructor

Create a new sppasCtrlVocab instance.

Parameters

  • name: (str) Identifier name of the controlled vocabulary
  • description: (str)
def __init__(self, name, description=''):
    """Create a new sppasCtrlVocab instance.

    :param name: (str) Identifier name of the controlled vocabulary
    :param description: (str)

    """
    super(sppasCtrlVocab, self).__init__()
    su = sppasUnicode(str(name))
    self.__name = su.clear_whitespace()
    self.__desc = ''
    if len(description) > 0:
        self.set_description(description)
    self.__entries = OrderedDict()

Public functions

get_name

Return the name of the controlled vocabulary.

def get_name(self):
    """Return the name of the controlled vocabulary."""
    return self.__name
get_description

Return the unicode str of the description of the ctrl vocab.

def get_description(self):
    """Return the unicode str of the description of the ctrl vocab."""
    return self.__desc
get_tag_description

Return the unicode string of the description of an entry.

Parameters

  • tag: (sppasTag) the tag to get the description.

Returns

  • (str)
def get_tag_description(self, tag):
    """Return the unicode string of the description of an entry.

        :param tag: (sppasTag) the tag to get the description.
        :returns: (str)

        """
    if self.contains(tag) is False:
        raise CtrlVocabContainsError(tag)
    return self.__entries[tag]
set_description

Set the description of the controlled vocabulary.

Parameters

  • description: (str)
def set_description(self, description=''):
    """Set the description of the controlled vocabulary.

        :param description: (str)

        """
    su = sppasUnicode(description)
    self.__desc = su.to_strip()
contains

Test if a tag is in the controlled vocabulary.

Attention: Do not check the instance but the data content of the tag.

Parameters

  • tag: (sppasTag) the tag to check.

Returns

  • Boolean
def contains(self, tag):
    """Test if a tag is in the controlled vocabulary.

        Attention: Do not check the instance but the data content of the tag.

        :param tag: (sppasTag) the tag to check.
        :returns: Boolean

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    for entry in self.__entries:
        if tag == entry:
            return True
    return False
validate_tag

Check if the given tag can be added to the ctrl vocabulary.

Parameters

  • tag: (sppasTag) the tag to check.

Returns

  • Boolean
def validate_tag(self, tag):
    """Check if the given tag can be added to the ctrl vocabulary.

        :param tag: (sppasTag) the tag to check.
        :returns: Boolean

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    current_type = tag.get_type()
    for k in self.__entries:
        current_type = k.get_type()
        break
    if tag.get_type() != current_type:
        raise AnnDataTypeError(tag, 'sppasTag:' + current_type)
add

Add a tag to the controlled vocab.

Parameters

  • tag: (sppasTag): the tag to add.
  • description: (str)

Returns

  • Boolean
def add(self, tag, description=''):
    """Add a tag to the controlled vocab.

        :param tag: (sppasTag): the tag to add.
        :param description: (str)
        :returns: Boolean

        """
    self.validate_tag(tag)
    if self.contains(tag) is True:
        return False
    su = sppasUnicode(description)
    self.__entries[tag] = su.to_strip()
    return True
remove

Remove a tag of the controlled vocab.

Parameters

  • tag: (sppasTag) the tag to remove.

Returns

  • Boolean
def remove(self, tag):
    """Remove a tag of the controlled vocab.

        :param tag: (sppasTag) the tag to remove.
        :returns: Boolean

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if self.contains(tag) is False:
        return False
    del self.__entries[tag]
    return True
set_tag_description

Set the unicode string of the description of an entry.

Parameters

  • tag: (sppasTag) the tag to get the description.
  • description: (str)

Returns

  • (str)
def set_tag_description(self, tag, description):
    """Set the unicode string of the description of an entry.

        :param tag: (sppasTag) the tag to get the description.
        :param description: (str)
        :returns: (str)

        """
    if self.contains(tag) is False:
        raise CtrlVocabContainsError(tag)
    su = sppasUnicode(description)
    self.__entries[tag] = su.to_strip()

Overloads

__format__
def __format__(self, fmt):
    return str(self).__format__(fmt)
__iter__
def __iter__(self):
    for a in self.__entries:
        yield a
__len__
def __len__(self):
    return len(self.__entries)
__repr__
def __repr__(self):
    return 'CtrlVocab: id={:s} name={:s} description={:s}'.format(self.get_meta('id'), self.__name, self.__desc)
__eq__

Test if other is strictly identical to self (even the id).

Parameters
  • other
def __eq__(self, other):
    """Test if other is strictly identical to self (even the id)."""
    if isinstance(other, sppasCtrlVocab) is False:
        return False
    for meta_key in self.get_meta_keys():
        if self.get_meta(meta_key) != other.get_meta(meta_key):
            return False
    for meta_key in other.get_meta_keys():
        if self.get_meta(meta_key) != other.get_meta(meta_key):
            return False
    if self.__name != other.get_name():
        return False
    if self.__desc != other.get_description():
        return False
    for entry in self:
        if other.contains(entry) is False:
            return False
        if self.get_tag_description(entry) != other.get_tag_description(entry):
            return False
    return len(self) == len(other)
__ne__
def __ne__(self, other):
    return not self == other