core (tools/common): added getSubDict method in data_format to get sub dictionary from serialised one
This commit is contained in:
parent
63b1c9af14
commit
3337750def
1 changed files with 22 additions and 1 deletions
|
|
@ -31,7 +31,7 @@ def dict2iter(name, dict_, pop=False):
|
|||
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 @@ def iter2dict(name, iter_, dict_=None, check_conflict=True):
|
|||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue