SPPAS 4.22

https://sppas.org/

Module sppas.src.wkps

Class FileBase

Description

Represent any type of data with an identifier and a state.

Constructor

Constructor of a FileBase.

Data structure to store an identifier (str) and a state (States).

Parameters
  • identifier: (str) Any un-modifiable string.
Raises

ValueError if the identifier is not valid.

View Source
def __init__(self, identifier):
    """Constructor of a FileBase.

    Data structure to store an identifier (str) and a state (States).

    :param identifier: (str) Any un-modifiable string.
    :raises: ValueError if the identifier is not valid.

    """
    self.__id = FileBase.validate_id(identifier)
    self._state = States().UNUSED

Public functions

validate_id

Return the given identifier if it matches the requirements.

An identifier should contain at least 2 characters.

Parameters
  • identifier: (str) Key to be validated
Raises

ValueError

Returns
  • (unicode)
View Source
@staticmethod
def validate_id(identifier):
    """Return the given identifier if it matches the requirements.

        An identifier should contain at least 2 characters.

        :param identifier: (str) Key to be validated
        :raises: ValueError
        :returns: (unicode)

        """
    su = sppasUnicode(identifier)
    ide = su.unicode().strip()
    if len(ide) < 1:
        raise FileIdValueError
    return su.unicode()

get_id

Return the identifier (str).

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

get_state

Return the state (States).

View Source
def get_state(self):
    """Return the state (States)."""
    return self._state

set_state

Set a state (to be overridden).

Parameters
  • value: (States) The state value to assign
Returns
  • (bool or list)
View Source
def set_state(self, value):
    """Set a state (to be overridden).

        :param value: (States) The state value to assign
        :returns: (bool or list)

        """
    raise NotImplementedError

match

Return True if this instance matches all or any of the functions.

Functions are defined in a comparator. They return a boolean. The type of the value depends on the function. The logical not is used to reverse the result of the function.

Parameters
  • functions: list of(function, value, logical_not)
  • logic_bool: (str) Apply a logical "and" or a logical "or" between the functions.
Returns
  • (bool)
View Source
def match(self, functions, logic_bool='and'):
    """Return True if this instance matches all or any of the functions.

        Functions are defined in a comparator. They return a boolean.
        The type of the value depends on the function.
        The logical not is used to reverse the result of the function.

        :param functions: list of (function, value, logical_not)
        :param logic_bool: (str) Apply a logical "and" or a logical "or" between the functions.
        :returns: (bool)

        """
    matches = list()
    for func, value, logical_not in functions:
        if logical_not is True:
            matches.append(not func(self, value))
        else:
            matches.append(func(self, value))
    if logic_bool == 'and':
        is_matching = all(matches)
    else:
        is_matching = any(matches)
    return is_matching

Overloads

__format__

Allow to show the class at a given format.

Parameters
  • fmt: (str) the wanted format of string
Returns
  • (str)
View Source
def __format__(self, fmt):
    """Allow to show the class at a given format.

        :param fmt: (str) the wanted format of string
        :returns: (str)

        """
    return str(self).__format__(fmt)

__str__

The string conversion of the object.

Returns
  • (str)
View Source
def __str__(self):
    """The string conversion of the object.

        :returns: (str)

        """
    return '{!s:s}'.format(self.__id)

__repr__

String conversion when called by print.

Returns
  • (str) Printed representation of the object.
View Source
def __repr__(self):
    """String conversion when called by print.

        :returns: (str) Printed representation of the object.

        """
    return 'File: {!s:s}'.format(self.__id)

__eq__

Allows to compare self with other by using "==".

Compare the identifier, but not the state.

Parameters
  • other: (FileName, str)
View Source
def __eq__(self, other):
    """Allows to compare self with other by using "==".

        Compare the identifier, but not the state.

        :param other: (FileName, str)

        """
    if other is not None:
        if isinstance(other, FileBase):
            return self.id == other.id
        else:
            return self.id == other
    return False

__ne__

Allows to compare self with other by using "!=".

Compare the identifier, but not the state.

Parameters
  • other: (FileName, str)
View Source
def __ne__(self, other):
    """Allows to compare self with other by using "!=".

        Compare the identifier, but not the state.

        :param other: (FileName, str)

        """
    if other is not None:
        return not self == other
    return False

__gt__

Allows to compare self with other by using ">".

Can be used, for example, to sort a list of instances alphabetically.

Parameters
  • other: (FileName, str)
View Source
def __gt__(self, other):
    """Allows to compare self with other by using ">".

        Can be used, for example, to sort a list of instances alphabetically.

        :param other: (FileName, str)

        """
    if other is not None:
        return self.id > other.id
    return False

__lt__

Allows to compare self with other by using "<".

Can be used, for example, to sort a list of instances alphabetically.

Parameters
  • other: (FileName, str)
View Source
def __lt__(self, other):
    """Allows to compare self with other by using "<".

        Can be used, for example, to sort a list of instances alphabetically.

        :param other: (FileName, str)

        """
    if other is not None:
        return self.id < other.id
    return False

__hash__

View Source
def __hash__(self):
    return hash((self.get_state(), self.get_id()))