Skip to content

Commit

Permalink
added allow_other_keys for plugin.decoders
Browse files Browse the repository at this point in the history
also improved doctest for apply_unitschema
  • Loading branch information
chrisjsewell committed Jul 2, 2018
1 parent 31a6395 commit bf3ee16
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions docs/source/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ Interface specifications
- *dict\_signature* attribute, a tuple denoting the keys which the
dictionary must have, e.g. dict\_signature=('a','b') decodes
{'a':1,'b':2}
- optionally, the attribute *allow\_other\_keys = True* can be set,
to allow the dictionary to have more keys than the dict\_signature and still be decoded,
e.g. dict\_signature=('a','b') would also decode {'a':1,'b':2,'c': 3}
- *from\_...* method(s), which takes a dict object as parameter. The
``plugins.decode`` function will use the method denoted by the
intype parameter, e.g. if intype='json', then *from\_json* will be
Expand Down
2 changes: 1 addition & 1 deletion jsonextended/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"""

__version__ = '0.7.6'
__version__ = '0.7.7'

from jsonextended import ejson, units, utils, edict, plugins

Expand Down
2 changes: 2 additions & 0 deletions jsonextended/edict.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,12 @@ def remove_keys(d, keys=None, use_wildcards=True,
Parameters
----------
keys: list
use_wildcards : bool
if true, can use * (matches everything) and ? (matches any single character)
list_of_dicts: bool
treat list of dicts as additional branches
deepcopy: bool
Examples
--------
Expand Down
6 changes: 5 additions & 1 deletion jsonextended/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ def decode(dct, intype='json', raise_error=False):
"""
for decoder in get_plugins('decoders').values():
if sorted(list(decoder.dict_signature)) == sorted(dct.keys()) and hasattr(decoder, 'from_{}'.format(intype)):
if set(list(decoder.dict_signature)).issubset(dct.keys()) and hasattr(decoder, 'from_{}'.format(intype)) \
and getattr(decoder, 'allow_other_keys', False):
return getattr(decoder, 'from_{}'.format(intype))(dct)
break
elif sorted(list(decoder.dict_signature)) == sorted(dct.keys()) and hasattr(decoder, 'from_{}'.format(intype)):
return getattr(decoder, 'from_{}'.format(intype))(dct)
break

Expand Down
20 changes: 8 additions & 12 deletions jsonextended/units/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,16 @@ def apply_unitschema(data, uschema, as_quantity=True,
>>> newschema = {'energy':'kJ','other':{'y':'nm'},'y':'m'}
>>> new_data = apply_unitschema(data_units,newschema)
>>> pprint(new_data)
{'energy': <Quantity(1.60217653e-22, 'kilojoule')>,
'meta': None,
'other': {'y': <Quantity([ 4.00000000e+09 5.00000000e+09], 'nanometer')>},
'x': <Quantity([1 2], 'nanometer')>,
'y': <Quantity([ 0.04 0.05], 'meter')>}
>>> str(new_data["energy"])
'1.60217653e-22 kilojoule'
>>> new_data["other"]["y"].magnitude.round(3).tolist(), str(new_data["other"]["y"].units)
([4000000000.0, 5000000000.0], 'nanometer')
>>> old_data = apply_unitschema(new_data,uschema,as_quantity=False)
>>> pprint(old_data)
{'energy': 1.0,
'meta': None,
'other': {'y': array([ 4., 5.])},
'x': array([1, 2]),
'y': array([ 4., 5.])}
>>> old_data["energy"]
1.0
>>> old_data["other"]["y"].round(3).tolist()
[4.0, 5.0]
"""
try:
Expand Down

0 comments on commit bf3ee16

Please # to comment.