Skip to content

Commit

Permalink
Added singleton checking methods. SimpleSet now inherits from int.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Jun 11, 2024
1 parent 84a33b2 commit a5fc1a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/random_events/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.0.1'
__version__ = '3.0.2'
14 changes: 11 additions & 3 deletions src/random_events/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion src/random_events/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class TestEnum(SetElement):
EMPTY_SET = 0
EMPTY_SET = -1
A = 1
B = 2
C = 4
Expand Down

0 comments on commit a5fc1a6

Please sign in to comment.