Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-migas committed Jun 28, 2024
1 parent 5f48cd5 commit 638bd21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/koyo/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def default(o):
return float(o)
elif isinstance(o, np.ndarray):
return o.tolist()
elif isinstance(o, np.bool_):
return bool(o)
elif isinstance(o, Path):
return str(o)
raise TypeError("Could not convert {} of type {}".format(*o), type(o))
Expand Down
9 changes: 7 additions & 2 deletions src/koyo/json_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import typing as ty
from pathlib import Path
import warnings

from koyo.json import read_json_data, write_json_data
from koyo.typing import PathLike
from json import JSONDecodeError


class JSONCache:
Expand Down Expand Up @@ -96,7 +98,7 @@ def as_str(self, sep="; ", exclude: ty.Optional[ty.Tuple[str, ...]] = None) -> s
if exclude is None:
exclude = ()
if self.exists():
data = read_json_data(self.path)
data = self.read()
ret = ""
n_stop = len(data) - 1
for i, (k, v) in enumerate(data.items()):
Expand All @@ -122,7 +124,10 @@ def print_summary(self, name: str = "", pre: str = "\t", sep: str = "\n"):
def read(self) -> ty.Dict:
"""Read data."""
if self.exists():
return read_json_data(self.path)
try:
return read_json_data(self.path)
except JSONDecodeError:
warnings.warn(f"Failed to read JSON file: {self.path}")
return {}

def write(self, data: ty.Dict):
Expand Down

0 comments on commit 638bd21

Please sign in to comment.