SPPAS 4.22

https://sppas.org/

Module sppas.src.wkps

Class sppasRefAttribute

Description

Represent any attribute with an id, a value, and a description.

Constructor

Constructor of sppasRefAttribute.

Parameters
  • identifier: (str) The identifier of the attribute
  • value: (str) String representing the value of the attribute
  • atttype: (*str*) One of the VALUETYPES
  • descr: (str) A string to describe what the attribute is
Raises

AttributeIdValueError

View Source
def __init__(self, identifier, value=None, att_type=None, descr=None):
    """Constructor of sppasRefAttribute.

    :param identifier: (str) The identifier of the attribute
    :param value: (str) String representing the value of the attribute
    :param att_type: (str) One of the VALUE_TYPES
    :param descr: (str) A string to describe what the attribute is
    :raises: AttributeIdValueError

    """
    self.__id = ''
    self.__set_id(identifier)
    self.__value = None
    self.set_value(value)
    self.__valuetype = 'str'
    self.set_value_type(att_type)
    self.__descr = None
    self.set_description(descr)

Public functions

validate

Return True if the given identifier matches the requirements.

An id should contain between 2 and 20 ASCII-characters only, i.e. letters a-z, letters A-Z and numbers 0-9.

Parameters
  • identifier: (str) Key to be validated
Returns
  • (bool)
View Source
@staticmethod
def validate(identifier):
    """Return True if the given identifier matches the requirements.

        An id should contain between 2 and 20 ASCII-characters only, i.e.
        letters a-z, letters A-Z and numbers 0-9.

        :param identifier: (str) Key to be validated
        :returns: (bool)

        """
    if 1 < len(identifier) < 21:
        return True
    return False

get_id

Return the identifier of the attribute.

View Source
def get_id(self):
    """Return the identifier of the attribute."""
    return self.__id

get_value

Return the current non-typed value.

Returns
  • (str)
View Source
def get_value(self):
    """Return the current non-typed value.

        :returns: (str)

        """
    if self.__value is None:
        return ''
    return self.__value

set_value

Set a new value.

Parameters
  • value: (str)
View Source
def set_value(self, value):
    """Set a new value.

        :param value: (str)

        """
    if value is None:
        self.__value = None
    else:
        su = sppasUnicode(value)
        self.__value = su.to_strip()

get_value_type

Return the current type of the value.

Returns
  • (str) Either: "str", "int", "float", "bool".
View Source
def get_value_type(self):
    """Return the current type of the value.

        :returns: (str) Either: "str", "int", "float", "bool".

        """
    return self.__valuetype if self.__valuetype is not None else 'str'

set_value_type

Set a new type for the current value.

Parameters
  • type_name: (str) the new type name
Raises

sppasTypeError

View Source
def set_value_type(self, type_name):
    """Set a new type for the current value.

        :param type_name: (str) the new type name
        :raises: sppasTypeError

        """
    if type_name in sppasRefAttribute.VALUE_TYPES:
        self.__valuetype = type_name
        try:
            self.get_typed_value()
        except AttributeTypeValueError:
            self.__valuetype = 'str'
            raise
    elif type_name is None:
        self.__valuetype = 'str'
    else:
        raise sppasTypeError(type_name, ' '.join(sppasRefAttribute.VALUE_TYPES))

get_typed_value

Return the current typed value.

Returns
  • (any type) the current typed value.
View Source
def get_typed_value(self):
    """Return the current typed value.

        :returns: (any type) the current typed value.

        """
    if self.__valuetype is not None or self.__valuetype != 'str':
        try:
            if self.__valuetype == 'int':
                return int(self.__value)
            elif self.__valuetype == 'float':
                return float(self.__value)
            elif self.__valuetype == 'bool':
                return self.__value.lower() == 'true'
        except ValueError:
            raise AttributeTypeValueError(self.__value, self.__valuetype)
        except TypeError:
            raise AttributeTypeValueError(self.__value, self.__valuetype)
    return self.__value

get_description

Return current description of the attribute.

Returns
  • (str)
View Source
def get_description(self):
    """Return current description of the attribute.

        :returns: (str)

        """
    if self.__descr is None:
        return ''
    return self.__descr

set_description

Set a new description of the attribute.

Parameters
  • description: (str)
View Source
def set_description(self, description):
    """Set a new description of the attribute.

        :param description: (str)

        """
    if description is None:
        self.__descr = None
    else:
        su = sppasUnicode(description)
        self.__descr = su.to_strip()

Protected functions

__set_id

View Source
def __set_id(self, identifier):
    su = sppasUnicode(identifier)
    identifier = su.unicode()
    if sppasRefAttribute.validate(identifier) is False:
        raise AttributeIdValueError(identifier)
    self.__id = identifier

Overloads

__str__

View Source
def __str__(self):
    return '{:s}, {:s}, {:s}'.format(self.__id, self.get_value(), self.get_description())

__repr__

View Source
def __repr__(self):
    return '{:s}, {:s}, {:s}'.format(self.__id, self.get_value(), self.get_description())

__format__

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

__hash__

View Source
def __hash__(self):
    return hash((self.__id, self.get_typed_value(), self.__descr))

__eq__

View Source
def __eq__(self, other):
    if other is None:
        return False
    if isinstance(other, sppasRefAttribute) is False:
        return False
    if self.__id != other.get_id():
        return False
    if self.get_typed_value() != other.get_typed_value():
        return False
    if self.get_description() != other.get_description():
        return False
    return True