From bf3ee16971358a5d08cb276e8bb193af234030a0 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 2 Jul 2018 18:18:16 +0100 Subject: [PATCH] added allow_other_keys for plugin.decoders also improved doctest for apply_unitschema --- docs/source/readme.rst | 3 +++ jsonextended/__init__.py | 2 +- jsonextended/edict.py | 2 ++ jsonextended/plugins.py | 6 +++++- jsonextended/units/core.py | 20 ++++++++------------ 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/docs/source/readme.rst b/docs/source/readme.rst index 4e18d85..65d0303 100644 --- a/docs/source/readme.rst +++ b/docs/source/readme.rst @@ -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 diff --git a/jsonextended/__init__.py b/jsonextended/__init__.py index 2a1d8ee..becbb42 100755 --- a/jsonextended/__init__.py +++ b/jsonextended/__init__.py @@ -91,7 +91,7 @@ """ -__version__ = '0.7.6' +__version__ = '0.7.7' from jsonextended import ejson, units, utils, edict, plugins diff --git a/jsonextended/edict.py b/jsonextended/edict.py index 7cd9f47..4b22919 100755 --- a/jsonextended/edict.py +++ b/jsonextended/edict.py @@ -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 -------- diff --git a/jsonextended/plugins.py b/jsonextended/plugins.py index 25c2349..42f4c00 100755 --- a/jsonextended/plugins.py +++ b/jsonextended/plugins.py @@ -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 diff --git a/jsonextended/units/core.py b/jsonextended/units/core.py index 0ac22f7..ad4197b 100755 --- a/jsonextended/units/core.py +++ b/jsonextended/units/core.py @@ -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': , - 'meta': None, - 'other': {'y': }, - 'x': , - 'y': } + >>> 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: