Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions serializable/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ def list_to_serializable_repr(x):

def to_dict(obj):
"""
If value wasn't isn't a primitive scalar or collection then it needs to
either implement to_dict (instances of Serializable) or has member
If value isn't a primitive scalar or collection then it needs to
either implement to_dict (instances of Serializable) or have member
data matching each required arg of __init__.
"""
if isinstance(obj, dict):
Expand Down
15 changes: 6 additions & 9 deletions serializable/serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@

class Serializable:
"""
Base class for all PyEnsembl objects which provides default
Base class for user-defined objects which provides default
methods such as to_json, from_json, __reduce__, and from_dict

Relies on the following condition:
(1) a user-defined to_dict method
(2) the keys of to_dict() must match the arguments to __init__
Relies on the keys of to_dict() matching the arguments to __init__.
"""

def __str__(self):
Expand Down Expand Up @@ -78,7 +76,7 @@ def _update_kwargs(cls, kwargs):
Rename any old keyword arguments to preserve backwards compatibility
"""
# check every class in the inheritance chain for its own
# definition of _KEYWORD_ALIASES
# definition of _SERIALIZABLE_KEYWORD_ALIASES
for klass in cls.mro():
keyword_rename_dict = getattr(klass, "_SERIALIZABLE_KEYWORD_ALIASES", {})
for old_name, new_name in keyword_rename_dict.items():
Expand All @@ -99,7 +97,7 @@ def from_dict(cls, state_dict):

def to_json(self):
"""
Returns a string containing a JSON representation of this Genome.
Returns a string containing a JSON representation of this object.
"""
return to_json(self)

Expand All @@ -112,16 +110,15 @@ def from_json(cls, json_string):

def write_json_file(self, path):
"""
Serialize this VariantCollection to a JSON representation and write it
out to a text file.
Serialize this object to JSON and write it to the text file at `path`.
"""
with open(path, "w") as f:
f.write(self.to_json())

@classmethod
def read_json_file(cls, path):
"""
Construct a VariantCollection from a JSON file.
Construct an instance of this class from the JSON file at `path`.
"""
with open(path) as f:
json_string = f.read()
Expand Down
2 changes: 1 addition & 1 deletion serializable/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.1"
__version__ = "1.1.2"


def print_version():
Expand Down
6 changes: 6 additions & 0 deletions tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ def test_object_to_json():

def test_object_pickle():
eq_(instance, pickle.loads(pickle.dumps(instance)))


def test_object_json_file(tmp_path):
path = tmp_path / "instance.json"
instance.write_json_file(path)
eq_(instance, A.read_json_file(path))
Loading