From a5fc1a648a4fcf768c3bf7a9432ce21bd63e47ff Mon Sep 17 00:00:00 2001 From: Tom Schierenbeck Date: Tue, 11 Jun 2024 11:36:20 +0200 Subject: [PATCH] Added singleton checking methods. SimpleSet now inherits from int. --- src/random_events/__init__.py | 2 +- src/random_events/interval.py | 14 +++++++++++--- src/random_events/set.py | 7 ++++++- test/test_set.py | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/random_events/__init__.py b/src/random_events/__init__.py index b7a5531..da4039b 100644 --- a/src/random_events/__init__.py +++ b/src/random_events/__init__.py @@ -1 +1 @@ -__version__ = '3.0.1' +__version__ = '3.0.2' diff --git a/src/random_events/interval.py b/src/random_events/interval.py index cab0419..413d106 100644 --- a/src/random_events/interval.py +++ b/src/random_events/interval.py @@ -76,6 +76,12 @@ def is_empty(self) -> bool: return self.lower > self.upper or ( self.lower == self.upper and (self.left == Bound.OPEN or self.right == Bound.OPEN)) + def is_singleton(self) -> bool: + """ + :return: True if the interval is a singleton (contains only one value), False otherwise. + """ + return self.lower == self.upper and self.left == Bound.CLOSED and self.right == Bound.CLOSED + def intersection_with(self, other: Self) -> Self: # create new limits for the intersection @@ -199,9 +205,11 @@ def new_empty_set(self) -> Self: def complement_if_empty(self) -> Self: return Interval([SimpleInterval(float('-inf'), float('inf'), Bound.OPEN, Bound.OPEN)]) - -# Type definitions - + def is_singleton(self): + """ + :return: True if the interval is a singleton (contains only one value), False otherwise. + """ + return len(self.simple_sets) == 1 and self.simple_sets[0].is_singleton() def open(left: float, right: float) -> Interval: diff --git a/src/random_events/set.py b/src/random_events/set.py index 5e902c5..791a220 100644 --- a/src/random_events/set.py +++ b/src/random_events/set.py @@ -7,16 +7,21 @@ from .sigma_algebra import * -class SetElement(AbstractSimpleSet, enum.Enum): +class SetElement(AbstractSimpleSet, int, enum.Enum): """ Base class for enums that are used as elements in a set. Classes that inherit from this class have to define an attribute called EMPTY_SET. + It is advisable to define EMPTY_SET as -1 to correctly work with indices. + The empty set of the class is used to access all other elements of the class. """ @property @abstractmethod def EMPTY_SET(self): + """ + :return: The empty set of the class. + """ raise NotImplementedError("The EMPTY_SET attribute has to be defined.") @property diff --git a/test/test_set.py b/test/test_set.py index 421604a..a2073cf 100644 --- a/test/test_set.py +++ b/test/test_set.py @@ -6,7 +6,7 @@ class TestEnum(SetElement): - EMPTY_SET = 0 + EMPTY_SET = -1 A = 1 B = 2 C = 4