Skip to content

Commit

Permalink
Adding basic support for a data handle that reads/writes json to/from…
Browse files Browse the repository at this point in the history
… a dict.
  • Loading branch information
drewoldag committed Dec 4, 2023
1 parent fd13285 commit cbe94c3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/rail/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import tables_io
import pickle
import json
import qp


Expand Down Expand Up @@ -333,6 +334,31 @@ def default_model_write(model, path):
pickle.dump(obj=model, file=fout, protocol=pickle.HIGHEST_PROTOCOL)


class JsonHandle(DataHandle):
"""Generic handle for reading in a whole file in json format as a dictionary.
"""

suffix = 'json'

@classmethod
def _open(cls, path, **kwargs):
"""Open and return the contents of the json file."""
return cls.read(path, **kwargs)

Check warning on line 346 in src/rail/core/data.py

View check run for this annotation

Codecov / codecov/patch

src/rail/core/data.py#L346

Added line #L346 was not covered by tests

@classmethod
def _read(cls, path, **kwargs):
"""Read the contents of the json file into a python dictionary"""
with open(path, "r") as infile:
data = json.loads(infile)
return data

Check warning on line 353 in src/rail/core/data.py

View check run for this annotation

Codecov / codecov/patch

src/rail/core/data.py#L351-L353

Added lines #L351 - L353 were not covered by tests

@classmethod
def _write(cls, data, path, **kwargs):
"""Write out the provided data dictionary as json."""
with open(path, "w") as outfile:
json.dumps(data, outfile)

Check warning on line 359 in src/rail/core/data.py

View check run for this annotation

Codecov / codecov/patch

src/rail/core/data.py#L358-L359

Added lines #L358 - L359 were not covered by tests


class ModelDict(dict):
"""
A specialized dict to keep track of individual estimation models objects: this is just a dict these additional features
Expand Down

0 comments on commit cbe94c3

Please sign in to comment.