Skip to content

Commit

Permalink
Update json_cache.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-migas committed Dec 29, 2024
1 parent 8712968 commit c77fd00
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/koyo/json_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,28 @@ def print_summary(self, name: str = "", pre: str = "\t", sep: str = "\n"):
for k, v in data.items():
print(f"{pre}{k}: {v}", sep=sep)

@staticmethod
def format_value(v) -> ty.Any:
"""Format value."""
if isinstance(v, str):
if v.lower() == "true":
return True
elif v.lower() == "false":
return False
elif v.isnumeric():
return int(v)
try:
return float(v)
except ValueError:
return v
return v

def read(self) -> ty.Dict:
"""Read data."""

def _convert(v):
if isinstance(v, str):
if v.lower() == "true":
return True
elif v.lower() == "false":
return False
elif v.isnumeric():
return int(v)
try:
return float(v)
except ValueError:
return v
return v

if self.exists():
try:
data = read_json_data(self.path)
data = {k: _convert(v) for k, v in data.items()}
data = {k: self.format_value(v) for k, v in data.items()}
return data
except JSONDecodeError:
warnings.warn(f"Failed to read JSON file: {self.path}")
Expand Down

0 comments on commit c77fd00

Please sign in to comment.