SPPAS 4.22

https://sppas.org/

Module sppas.src.structs

Class sppasBaseSet

Description

Manager for a set of data.

Mainly used with the data that are the result of the filter system.

A sppasBaseSet() manages a dictionary with:

  • key: an object
  • value: a list of strings

It implements the operators '|' and '&'.

Constructor

Create a sppasBaseSet instance.

View Source
def __init__(self):
    """Create a sppasBaseSet instance."""
    self._data_set = collections.OrderedDict()

Public functions

get_value

Return the string value corresponding to a data.

Parameters
  • data: (object)
Returns
  • (list of str) the string value to associate to the data.
View Source
def get_value(self, data):
    """Return the string value corresponding to a data.

        :param data: (object)
        :returns: (list of str) the string value to associate to the data.

        """
    return self._data_set.get(data, None)

append

Append a data in the data set, with the given value.

Parameters
  • data: (object)
  • value: (list of str) List of any string.
View Source
def append(self, data, value):
    """Append a data in the data set, with the given value.

        :param data: (object)
        :param value: (list of str) List of any string.

        """
    if value is None:
        raise sppasTypeError(value, 'list')
    if isinstance(value, list) is False:
        raise sppasTypeError(value, 'list')
    if data in self._data_set:
        old_value_list = self._data_set[data]
        self._data_set[data] = list(set(old_value_list + value))
    else:
        self._data_set[data] = value

remove

Remove the data of the data set.

Parameters
  • data: (object)
View Source
def remove(self, data):
    """Remove the data of the data set.

        :param data: (object)

        """
    if data in self._data_set:
        del self._data_set[data]

copy

Make a deep copy of self.

View Source
def copy(self):
    """Make a deep copy of self."""
    d = sppasBaseSet()
    for data, value in self._data_set.items():
        d.append(data, value)
    return d

Overloads

__iter__

View Source
def __iter__(self):
    for data in list(self._data_set.keys()):
        yield data

__len__

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

__contains__

View Source
def __contains__(self, data):
    return data in self._data_set

__eq__

Check if data sets are equals, i.e. share the same data.

Parameters
  • other
View Source
def __eq__(self, other):
    """Check if data sets are equals, i.e. share the same data."""
    if len(self) != len(other):
        return False
    for key, value in self._data_set.items():
        if key not in other:
            return False
        other_value = other.get_value(key)
        if set(other_value) != set(value):
            return False
    return True

__or__

Implements the '|' operator between 2 data sets.

The operator '|' does the intersection operation.

Parameters
  • other
View Source
def __or__(self, other):
    """Implements the '|' operator between 2 data sets.

        The operator '|' does the intersection operation.

        """
    d = self.copy()
    for data in other:
        d.append(data, other.get_value(data))
    return d

__and__

Implements the '&' operator between 2 data sets.

The operator '&' does the union operation.

Parameters
  • other
View Source
def __and__(self, other):
    """Implements the '&' operator between 2 data sets.

        The operator '&' does the union operation.

        """
    d = sppasBaseSet()
    for data in self:
        if data in other:
            d.append(data, self.get_value(data))
            d.append(data, other.get_value(data))
    return d