From cb41a071f26f0b5e6fd1df517297acb4fe79db96 Mon Sep 17 00:00:00 2001 From: Christian Bean Date: Fri, 19 Mar 2021 19:50:13 +0000 Subject: [PATCH] json methods --- motzkin/motzkinpaths.py | 22 +++++++++++++--------- motzkin/strategies.py | 10 +++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/motzkin/motzkinpaths.py b/motzkin/motzkinpaths.py index 2c46bb3..6bf2c29 100644 --- a/motzkin/motzkinpaths.py +++ b/motzkin/motzkinpaths.py @@ -216,7 +216,11 @@ def all_motzkin_paths(size): yield p def to_jsonable(self, prefix="") -> dict: - return {"prefix": prefix, "avoids": self.avoids, "contains": self.contains} + d = super().to_jsonable() + d["prefix"] = prefix + d["avoids"] = self.avoids + d["contains"] = self.contains + return d @classmethod def from_dict(cls, d: dict) -> "MotzkinPaths": @@ -328,7 +332,7 @@ def maxlen(self) -> int: ) def to_jsonable(self, prefix: str = "H") -> dict: - return MotzkinPaths.to_jsonable(self, prefix=prefix) + return super().to_jsonable(prefix=prefix) def is_empty(self) -> bool: if MotzkinPath("H") in self.avoids: @@ -501,13 +505,13 @@ def objects_of_size(self, size: int) -> Iterator[MotzkinPath]: yield path def to_jsonable(self, prefix="U") -> dict: - return { - "prefix": prefix, - "crossing_avoids": tuple(p.to_jsonable() for p in self.avoids), - "crossing_contains": tuple( - p.to_jsonable() for ps in self.contains for p in ps - ), - } + d = CombinatorialClass.to_jsonable(self) + d["prefix"] = prefix + d["avoids"] = tuple(p.to_jsonable() for p in self.avoids) + d["contains"] = tuple( + tuple(p.to_jsonable() for p in ps) for ps in self.contains + ) + return d def __repr__(self) -> str: return ( diff --git a/motzkin/strategies.py b/motzkin/strategies.py index e6b7bd7..8d22d23 100644 --- a/motzkin/strategies.py +++ b/motzkin/strategies.py @@ -92,10 +92,18 @@ def decomposition_function( def formal_step(self) -> str: return f"The paths avoid or contain {self.pattern}" + def to_jsonable(self) -> dict: + d = super().to_jsonable() + d["pattern"] = self.pattern.to_jsonable() + return d + @classmethod def from_dict(cls, d: dict) -> "PattInsertion": + patt = d.pop("pattern") return PattInsertion( - pattern=MotzkinPath.from_dict(d.pop("pattern")), + pattern=MotzkinPath.from_dict(patt) + if isinstance(patt, tuple) + else CrossingPattern.from_dict(patt), ignore_parent=d.pop("ignore_parent"), inferrable=d.pop("inferrable"), possibly_empty=d.pop("possibly_empty"),