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 '&'.
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:
It implements the operators '|' and '&'.
Create a sppasBaseSet instance.
def __init__(self):
"""Create a sppasBaseSet instance."""
self._data_set = collections.OrderedDict()
Return the string value corresponding to a data.
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 a data in the data set, with the given value.
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 the data of the data set.
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]
Make a deep copy of self.
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
def __iter__(self):
for data in list(self._data_set.keys()):
yield data
def __len__(self):
return len(self._data_set)
def __contains__(self, data):
return data in self._data_set
Check if data sets are equals, i.e. share the same data.
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
Implements the '|' operator between 2 data sets.
The operator '|' does the intersection operation.
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
Implements the '&' operator between 2 data sets.
The operator '&' does the union operation.
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