From af463354728b7f8d8e0b627d8ba7513befbd1dd5 Mon Sep 17 00:00:00 2001 From: Tom Schierenbeck Date: Tue, 23 Apr 2024 08:43:15 +0200 Subject: [PATCH] New exercise for self assessment --- src/random_events/events.py | 12 ++++++++++++ src/random_events/variables.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/random_events/events.py b/src/random_events/events.py index 5166319..5dd86a6 100644 --- a/src/random_events/events.py +++ b/src/random_events/events.py @@ -462,6 +462,12 @@ def get_variables_where_assignment_is_different(self, other: Self) -> List[Varia """ return [variable for variable in self.keys() if self[variable] != other[variable]] + def to_typst(self) -> str: + """ + Convert the event to a typst string. + """ + return " times ".join(f"{variable.name}_({variable.assignment_to_typst(value)})" for variable, value in + self.items()) class EncodedEvent(Event): @@ -770,5 +776,11 @@ def _from_json(cls, data: Dict[str, Any]) -> Self: events = [Event.from_json(event) for event in data["events"]] return cls(events) + def to_typst(self) -> str: + """ + Convert the event to a typst string. + """ + return " union ".join(f"({event.to_typst()})" for event in self.events) + EventType = Union[Event, EncodedEvent, ComplexEvent] diff --git a/src/random_events/variables.py b/src/random_events/variables.py index 3991513..5b55550 100644 --- a/src/random_events/variables.py +++ b/src/random_events/variables.py @@ -155,6 +155,12 @@ def assignment_from_json(self, data: Any) -> AssignmentType: def encoded_domain(self): return self.encode_many(self.domain) + def assignment_to_typst(self, assignment: AssignmentType) -> str: + """ + Convert an assignment to typst string. + """ + raise NotImplementedError + class Continuous(Variable): """ @@ -195,6 +201,9 @@ def assignment_to_json(self, assignment: portion.Interval) -> Any: def assignment_from_json(self, data: Any) -> portion.Interval: return portion.from_data(data) + def assignment_to_typst(self, assignment: AssignmentType) -> str: + return " union ".join([interval.__str__() for interval in assignment]) + class Discrete(Variable): """ @@ -266,6 +275,9 @@ def assignment_to_json(self, assignment: Tuple) -> Tuple: def assignment_from_json(self, data: Any) -> AssignmentType: return tuple(data) + def assignment_to_typst(self, assignment: AssignmentType) -> str: + return "{" + ", ".join([str(element) for element in assignment]) + "}" + class Symbolic(Discrete): """