# HG changeset patch # User Goffi # Date 1474823785 -7200 # Node ID c8e561a5b2b69aa993cee8597a7c3307abc05f71 # Parent 3a0a7e5cef49012ffeeeed3984cdcc1c32b9c578 core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one diff -r 3a0a7e5cef49 -r c8e561a5b2b6 src/tools/common/data_format.py --- a/src/tools/common/data_format.py Sun Sep 25 16:07:44 2016 +0200 +++ b/src/tools/common/data_format.py Sun Sep 25 19:16:25 2016 +0200 @@ -31,7 +31,7 @@ iteration stop at first missing increment Empty values are possible @param name(unicode): name of the key - @param mb_data (dict): dictionary with the serialised list + @param dict_(dict): dictionary with the serialised list @param pop(bool): if True, remove the value from dict @return iter: iterate through the deserialised list """ @@ -79,3 +79,24 @@ raise exceptions.ConflictError dict_[key] = value return dict + +def getSubDict(name, dict_, sep=u'_'): + """get a sub dictionary from a serialised dictionary + + look for keys starting with name, and create a dict with it + eg.: if "key" is looked for, {'html': 1, 'key_toto': 2, 'key_titi': 3} will return: + {None: 1, toto: 2, titi: 3} + @param name(unicode): name of the key + @param dict_(dict): dictionary with the serialised list + @param sep(unicode): separator used between name and subkey + @return iter: iterate through the deserialised items + """ + for k,v in dict_.iteritems(): + if k.startswith(name): + if k == name: + yield None, v + else: + if k[len(name)] != sep: + continue + else: + yield k[len(name)+1:], v