WhintPy 1.1

https://sourceforge.net/projects/whintpy/

Module whintpy.filters

Class FilteredSet

Description

Manager for a list of filtered data.

Manage a list of unique objects, similar to a set but with list-like append and remove methods. It supports union (|) and intersection (&) operations, deep copying, and equality checks.

Example
 >>> fs = FilteredSet()
 >>> fs.append("item1")
 >>> fs.append("item2")
 >>> print(len(fs))
 > 2
 >>> fs2 = FilteredSet()
 >>> fs2.append("item2")
 >>> fs2.append("item3")
 >>> union_fs = fs | fs2
 >>> print(len(union_fs))
 > 3
 >>> intersection_fs = fs & fs2
 >>> print(len(intersection_fs))
 > 1

Constructor

Create a FilteredSet instance.

View Source
def __init__(self):
    """Create a FilteredSet instance."""
    self._data_set = list()

Public functions

append

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

Parameters
  • data: (object)
Returns
  • (bool) Added or not
View Source
def append(self, data) -> bool:
    """Append a data in the data set, with the given value.

        :param data: (object)
        :return: (bool) Added or not

        """
    if data not in self._data_set:
        self._data_set.append(data)
        return True
    return False

remove

Remove the data of the data set.

Parameters
  • data: (object)
Returns
  • (bool) Removed or not
View Source
def remove(self, data) -> bool:
    """Remove the data of the data set.

        :param data: (object)
        :return: (bool) Removed or not

        """
    if data in self._data_set:
        self._data_set.remove(data)
        return True
    return False

copy

Make a deep copy of self.

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

Overloads

__iter__

View Source
def __iter__(self):
    for data in self._data_set:
        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. they share the same content.

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

__str__

View Source
def __str__(self):
    return str(self._data_set)

__or__

Implements the '|' operator between 2 data sets.

Usually, the '|' is a bitwise comparator. It is overridden, so it does the union operation between two filtered sets.

Returns
  • (FilteredSet) Union of two filtered sets.
Parameters
  • other
View Source
def __or__(self, other):
    """Implements the '|' operator between 2 data sets.

        Usually, the '|' is a bitwise comparator. It is overridden, so it
        does the union operation between two filtered sets.

        :return: (FilteredSet) Union of two filtered sets.

        """
    _d = self.copy()
    for data in other:
        _d.append(data)
    return _d

__and__

Implements the '&' operator between 2 data sets.

Usually, the '&' is a bitwise comparator. It is overridden, so it does the intersection operation between two filtered sets.

Returns
  • (FilteredSet) Intersection of two filtered sets.
Parameters
  • other
View Source
def __and__(self, other):
    """Implements the '&' operator between 2 data sets.

        Usually, the '&' is a bitwise comparator. It is overridden, so it
        does the intersection operation between two filtered sets.

        :return: (FilteredSet) Intersection of two filtered sets.

        """
    _d = FilteredSet()
    for data in self._data_set:
        if data in other:
            _d.append(data)
    return _d