Mercurial > libervia-backend
diff sat/tools/common/data_format.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 8cacf7929f3c |
line wrap: on
line diff
--- a/sat/tools/common/data_format.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/tools/common/data_format.py Wed Jun 27 20:14:46 2018 +0200 @@ -18,10 +18,11 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. """ tools common to backend and frontends """ -# FIXME: json may be more appropriate than manual serialising like done here +# FIXME: json may be more appropriate than manual serialising like done here from sat.core import exceptions + def dict2iter(name, dict_, pop=False): """iterate into a list serialised in a dict @@ -36,24 +37,25 @@ @return iter: iterate through the deserialised list """ if pop: - get=lambda d,k: d.pop(k) + get = lambda d, k: d.pop(k) else: - get=lambda d,k: d[k] + get = lambda d, k: d[k] try: - yield get(dict_,name) + yield get(dict_, name) except KeyError: return else: idx = 1 while True: try: - yield get(dict_,u'{}#{}'.format(name, idx)) + yield get(dict_, u"{}#{}".format(name, idx)) except KeyError: return else: idx += 1 + def dict2iterdict(name, dict_, extra_keys, pop=False): """like dict2iter but yield dictionaries @@ -61,17 +63,20 @@ e.g. dict2iterdict(comments, mb_data, ('node', 'service')) will yield dicts like: {u'comments': u'value1', u'node': u'value2', u'service': u'value3'} """ - # FIXME: this format seem overcomplicated, it may be more appropriate to use json here + # FIXME: this format seem overcomplicated, it may be more appropriate to use json here if pop: - get=lambda d,k: d.pop(k) + get = lambda d, k: d.pop(k) else: - get=lambda d,k: d[k] + get = lambda d, k: d[k] for idx, main_value in enumerate(dict2iter(name, dict_, pop=pop)): ret = {name: main_value} for k in extra_keys: - ret[k] = get(dict_, u'{}{}_{}'.format(name, (u'#' + unicode(idx)) if idx else u'', k)) + ret[k] = get( + dict_, u"{}{}_{}".format(name, (u"#" + unicode(idx)) if idx else u"", k) + ) yield ret + def iter2dict(name, iter_, dict_=None, check_conflict=True): """Fill a dict with values from an iterable @@ -92,13 +97,14 @@ if idx == 0: key = name else: - key = u'{}#{}'.format(name, idx) + key = u"{}#{}".format(name, idx) if check_conflict and key in dict_: raise exceptions.ConflictError dict_[key] = value return dict -def getSubDict(name, dict_, sep=u'_'): + +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 @@ -109,7 +115,7 @@ @param sep(unicode): separator used between name and subkey @return iter: iterate through the deserialised items """ - for k,v in dict_.iteritems(): + for k, v in dict_.iteritems(): if k.startswith(name): if k == name: yield None, v @@ -117,4 +123,4 @@ if k[len(name)] != sep: continue else: - yield k[len(name)+1:], v + yield k[len(name) + 1 :], v