Skip to content

Commit

Permalink
json methods
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbean committed Mar 19, 2021
1 parent b39bc23 commit cb41a07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
22 changes: 13 additions & 9 deletions motzkin/motzkinpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 (
Expand Down
10 changes: 9 additions & 1 deletion motzkin/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit cb41a07

Please sign in to comment.