Source code for superduperdb.misc.special_dicts

import typing as t
from collections import defaultdict


[docs] class MongoStyleDict(t.Dict[str, t.Any]): """ Dictionary object mirroring how fields can be referred to and set in MongoDB. >>> d = MongoStyleDict({'a': {'b': 1}}) >>> d['a.b'] 1 t.Set deep fields directly with string keys: >>> d['a.c'] = 2 >>> d {'a': {'b': 1, 'c': 2}} Parent keys should exist in order to set subfields: >>> d['a.d.e'] = 3 Traceback (most recent call last): ... KeyError: 'd' """ def __getitem__(self, key: str) -> t.Any: if key == '_base': return self if '.' not in key: return super().__getitem__(key) else: try: return super().__getitem__(key) except KeyError: parts = key.split('.') parent = parts[0] child = '.'.join(parts[1:]) sub = MongoStyleDict(self.__getitem__(parent)) return sub[child] def __setitem__(self, key: str, value: t.Any) -> None: if '.' not in key: super().__setitem__(key, value) else: parent = key.split('.')[0] child = '.'.join(key.split('.')[1:]) try: parent_item = MongoStyleDict(self[parent]) except KeyError: parent_item = MongoStyleDict({}) parent_item[child] = value self[parent] = parent_item
[docs] class ArgumentDefaultDict(defaultdict): def __getitem__(self, item): if item not in self: self[item] = self.default_factory(item) return super().__getitem__(item)
[docs] def diff(r1, r2): """ >>> _diff({'a': 1, 'b': 2}, {'a': 2, 'b': 2}) {'a': (1, 2)} >>> _diff({'a': {'c': 3}, 'b': 2}, {'a': 2, 'b': 2}) {'a': ({'c': 3}, 2)} """ d = _diff_impl(r1, r2) out = {} for path, left, right in d: out['.'.join(path)] = (left, right) return out
def _diff_impl(r1, r2): if not isinstance(r1, dict) or not isinstance(r2, dict): if r1 == r2: return [] return [([], r1, r2)] out = [] for k in list(r1.keys()) + list(r2.keys()): if k not in r1: out.append(([k], None, r2[k])) continue if k not in r2: out.append(([k], r1[k], None)) continue out.extend([([k, *x[0]], x[1], x[2]) for x in _diff_impl(r1[k], r2[k])]) return out