Skip to content
Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A derived class must either:
- have a member data matching the name of each argument to `__init__`
- provide a user-defined `to_dict()` method which returns a dictionary whose keys match the arguments to `__init__`

If you change the keyword arguments to a class which derives from `Serializable` but would like to be able to deserialize older JSON representations then you can define a class-level dictionary called `_KEYWORD_ALIASES` which maps old keywords to new names (or `None` if a keyword was removed).
If you change the keyword arguments to a class which derives from `Serializable` but would like to be able to deserialize older JSON representations then you can define a class-level dictionary called `_SERIALIZABLE_KEYWORD_ALIASES` which maps old keywords to new names (or `None` if a keyword was removed).

## `DataclassSerializable` for `@dataclass` subclasses

Expand All @@ -42,6 +42,6 @@ The on-wire JSON format is identical to `Serializable`, so mixed codebases inter

## Limitations

- Serializable objects must inherit from `Serializable`, be tuples or namedtuples, be serializble primitive types such as dict, list, int, float, or str.
- Serializable objects must inherit from `Serializable`, be tuples or namedtuples, be serializable primitive types such as dict, list, int, float, or str.

- The serialized representation of objects relies on reserved keywords (such as `"__name__"`, and `"__class__"`), so dictionaries are expected to not contain any keys which begin with two underscores.
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
22 changes: 8 additions & 14 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
methods such as to_json, from_json, __reduce__, and from_dict
Base class 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,16 @@ 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 a JSON representation and write it out to a
text file.
"""
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 from a JSON file.
"""
with open(path) as f:
json_string = f.read()
Expand All @@ -132,8 +130,4 @@ def __reduce__(self):
Overriding this method directs the default pickler to reconstruct
this object using our from_dict method.
"""

# I wish I could just return (self.from_dict, (self.to_dict(),) but
# Python 2 won't pickle the class method from_dict so instead have to
# use globally defined functions.
return (from_serializable_repr, (to_serializable_repr(self),))
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.0"
__version__ = "1.1.1"


def print_version():
Expand Down
Loading