def test_sorted_items(): aa = dict(c=1, b={}, a=[]) assert ''.join(k for k, _ in sorted_items(aa)) == 'abc' assert ''.join(k for k, _ in sorted_items(aa, key=lambda x: x)) == 'abc' assert ''.join(k for k, _ in sorted_items(aa, reverse=True)) == 'cba' remap = dict(c=0, a=1, b=2) assert ''.join(k for k, _ in sorted_items(aa, key=lambda x: remap[x])) == 'cab'
def check_sources(a, b): """ True if two dictionaries contain same objects under the same names. same, not just equivalent. """ if len(a) != len(b): return False for ((ak, av), (bk, bv)) in zip(sorted_items(a), sorted_items(b)): if ak != bk: return False if av is not bv: return False return True
def visit(ds): return mk_node(ds, {k: visit(v) for k, v in sorted_items(ds.sources)}, **kwargs)
def visit_post_order(ds, func, depth=0, name=None): for k, v in sorted_items(ds.sources): visit_post_order(v, func, depth=depth + 1, name=k) func(ds, depth=depth, name=name, **kwargs)