Mercurial > libervia-backend
changeset 2079:c8e561a5b2b6
core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 25 Sep 2016 19:16:25 +0200 |
parents | 3a0a7e5cef49 |
children | 3626b2813158 |
files | src/tools/common/data_format.py |
diffstat | 1 files changed, 22 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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