Skip to content

Commit

Permalink
Merge pull request #37 from simberaj/measure-persist-docs
Browse files Browse the repository at this point in the history
Documentation for measure and persist modules
  • Loading branch information
simberaj authored Nov 7, 2020
2 parents 50861b7 + 9375160 commit a862c68
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Votelib API reference

api_docs/api_evaluate
api_docs/api_convert
api_docs/api_measure
api_docs/api_candidate
api_docs/api_vote
api_docs/api_component


api_docs/api_persist
5 changes: 5 additions & 0 deletions docs/api_docs/api_measure.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Election result measurements
---------------------------------------------------------

.. automodule:: votelib.measure
:members:
6 changes: 6 additions & 0 deletions docs/api_docs/api_persist.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Persistence of election evaluators and other objects
---------------------------------------------------------

.. autofunction:: votelib.persist.from_dict

.. autofunction:: votelib.persist.to_dict
13 changes: 13 additions & 0 deletions votelib/persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def simple_serialization(class_: type) -> type:
to the class's constructor parameter names. Therefore, this decorator
is only useful when the class stores all its original parameters
unchanged (or in any other form acceptable to its constructor).
:param class_: The class to add the method to.
'''
if hasattr(class_, 'serialize_params'):
param_names = class_.serialize_params
Expand Down Expand Up @@ -135,6 +137,10 @@ def get_object(identifier: str) -> Any:


def from_dict(value: Dict[str, Any]) -> Any:
"""Parse an election evaluator object from a JSON-like dictionary.
:param value: A dictionary created by :func:`to_dict`.
"""
if not isinstance(value, dict):
raise ValueError('invalid votelib object def: dict expected,'
f'got {value!r}')
Expand All @@ -148,6 +154,13 @@ def from_dict(value: Dict[str, Any]) -> Any:


def to_dict(obj: Any) -> Dict[str, Any]:
"""Serialize an election evaluator object to a JSON-ready dictionary.
:param obj: An election evaluator object or similar. It should provide
a `to_dict()` method (all the standard evaluators, converters and the
like from Votelib should have it, courtesy of the simple_serialization
decorator).
"""
return serialize_value(obj)


Expand Down

0 comments on commit a862c68

Please sign in to comment.