summaryrefslogtreecommitdiffstats
path: root/src/act4e_solutions
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2023-10-10 11:26:46 +0200
committerNao Pross <np@0hm.ch>2023-10-10 11:26:46 +0200
commitd208bf8102bea5212027efc71e51b4dbddfda0f9 (patch)
tree0e0126f7fcd6a1f25ad030ac1235dfe5d9e6fe14 /src/act4e_solutions
parentPass TestFiniteMakeSetProduct (diff)
downloadact4e-d208bf8102bea5212027efc71e51b4dbddfda0f9.tar.gz
act4e-d208bf8102bea5212027efc71e51b4dbddfda0f9.zip
Implement loading of set products in SolFiniteSetRepresentation
Diffstat (limited to 'src/act4e_solutions')
-rw-r--r--src/act4e_solutions/sets_representation.py34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/act4e_solutions/sets_representation.py b/src/act4e_solutions/sets_representation.py
index 64d958a..32931da 100644
--- a/src/act4e_solutions/sets_representation.py
+++ b/src/act4e_solutions/sets_representation.py
@@ -1,6 +1,7 @@
from typing import Any, Collection, Iterator, List, TypeVar, cast
import act4e_interfaces as I
+from .sets_product import MyFiniteSetProduct
E = TypeVar("E")
@@ -18,7 +19,7 @@ class MyFiniteSet(I.FiniteSet[E]):
def elements(self) -> Iterator[E]:
return iter(self._elements)
- def save(self, h: I.IOHelper, x: E) -> I.ConcreteRepr:
+ def save(self, h: I.IOHelper, x: E) -> List[E]:
return cast(I.ConcreteRepr, x)
def load(self, h: I.IOHelper, o: I.ConcreteRepr):
@@ -30,14 +31,33 @@ class SolFiniteSetRepresentation(I.FiniteSetRepresentation):
if not isinstance(data, dict):
raise I.InvalidFormat()
- if not "elements" in data:
- raise I.InvalidFormat()
+ if "elements" in data:
+ if not isinstance(data["elements"], list):
+ raise I.InvalidFormat()
- if not isinstance(data["elements"], list):
- raise I.InvalidFormat()
+ elements = data["elements"]
+ return MyFiniteSet(elements)
+
+ elif "product" in data:
+ if not isinstance(data["product"], list):
+ raise I.InvalidFormat()
+
+ components = []
+ for comp in data["product"]:
+ if not isinstance(comp, dict):
+ raise I.InvalidFormat()
- elements = data["elements"]
- return MyFiniteSet(elements)
+ if not "elements" in comp:
+ raise I.InvalidFormat()
+
+ if not isinstance(comp["elements"], list):
+ raise I.InvalidFormat()
+
+ components.append(MyFiniteSet(comp["elements"]))
+ return MyFiniteSetProduct(components)
+
+ else:
+ raise I.InvalidFormat()
def save(self, h: I.IOHelper, f: I.FiniteSet[Any]) -> I.FiniteSet_desc:
all_elements = [f.save(h, e) for e in f.elements()]