def test_rearrange(): named_sndict_a = StructuredNestedDict( dict_a, levels=3, level_names=["a", "b", "c"]) assert list_equal( named_sndict_a.rearrange([1, 0]).flatten_keys(2, named=False), [('key1_1', 'key1', 'key1_1_1'), ('key1_1', 'key1', 'key1_1_2'), ('key2_1', 'key2', 'key2_1_1'), ('key2_1', 'key2', 'key2_1_2'), ('key2_2', 'key2', 'key2_2_1')], ) assert named_sndict_a.rearrange([1, 0]).level_names \ == ("b", "a", "c") assert named_sndict_a.rearrange([0, 1]).level_names \ == ("a", "b", "c") assert list_equal( named_sndict_a\ .rearrange(["c", "b", "a"])\ .flatten_keys(2, named=False), [('key1_1_1', 'key1_1', 'key1'), ('key1_1_2', 'key1_1', 'key1'), ('key2_1_1', 'key2_1', 'key2'), ('key2_1_2', 'key2_1', 'key2'), ('key2_2_1', 'key2_2', 'key2')], ) assert named_sndict_a.rearrange(["c", "b", "a"]).level_names \ == ("c", "b", "a") unnamed_sndict_a = StructuredNestedDict( dict_a, levels=3) assert unnamed_sndict_a.rearrange([2, 1, 0]).levels == 3 assert unnamed_sndict_a.rearrange([2, 1, 0])\ .flatten_keys(2, named=False)[0] == ('key1_1_1', 'key1_1', 'key1')
def test_flatten_values(): assert list_equal( NestedDict(dict_a).flatten_values(), ['val1', 'val2_1', 'val2_2', 'val3_1_1', 'val3_1_2', 'val3_2_1'], ) flattened_one_level = NestedDict(dict_a).flatten_values(max_depth=1) assert flattened_one_level[0] == 'val1' assert isinstance(flattened_one_level[1], col.OrderedDict) assert isinstance(flattened_one_level[2], col.OrderedDict) assert list_equal( NestedDict(empty_dict).flatten_values(), list(), )
def test_flatten_keys(): assert list_equal( NestedDict(dict_a).flatten_keys(), [('key1', ), ('key2', 'key2_1'), ('key2', 'key2_2'), ('key3', 'key3_1', 'key3_1_1'), ('key3', 'key3_1', 'key3_1_2'), ('key3', 'key3_2', 'key3_2_1')]) assert list_equal( NestedDict(dict_a).flatten_keys(max_depth=1), [('key1', ), ('key2', ), ('key3', )], ) assert list_equal( NestedDict(empty_dict).flatten_keys(), list(), )
def test_map_values(): named_sndict_a = StructuredNestedDict( dict_a, levels=3, level_names=["a", "b", "c"]) assert list_equal( named_sndict_a.map_values(str.upper).flatten_values(), ['VAL1_1_1', 'VAL1_1_2', 'VAL2_1_1', 'VAL2_1_2', 'VAL2_2_1'] )
def test_sort_keys(): named_sndict_a = StructuredNestedDict( dict_a, levels=3, level_names=["a", "b", "c"]) assert list_equal( named_sndict_a.sort_keys(reverse=True).keys(), ['key3', 'key2', 'key1'], )
def test_filter_values(): sndict_c = StructuredNestedDict(dict_c, levels=3) assert list_equal( sndict_c.filter_values(lambda _: _[-1] == "2") .flatten_keys(1, named=False), [('key1', 'keyX_1'), ('key2', 'keyX_1'), ('key2', 'keyX_2')], )
def test_unique_keys(): sndict_b = StructuredNestedDict(dict_b, levels=3) assert list_equal( map(tuple, sndict_b.unique_keys()), [('key1', 'key2', 'key3'), ('keyX_1',), ('keyX_X_1', 'keyX_X_2', 'keyX_X_3', 'keyX_X_4', 'keyX_X_5')], )
def test__delitem__(): named_sndict_a = StructuredNestedDict( dict_a, levels=3, level_names=["a", "b", "c"]) del named_sndict_a["key2"] assert list_equal( named_sndict_a.flatten_values(), ['val1_1_1', 'val1_1_2'], ) assert named_sndict_a.dim == (2, 1, 2)
def test_iterflatten(): sndict = StructuredNestedDict(dict_a, levels=3) assert list_equal( col.OrderedDict(sndict.iterflatten(1)).keys(), [('key1', 'key1_1'), ('key2', 'key2_1'), ('key2', 'key2_2')] ) assert list_equal( col.OrderedDict(sndict.iterflatten(2)).values(), ['val1_1_1', 'val1_1_2', 'val2_1_1', 'val2_1_2', 'val2_2_1'], ) assert list_equal( col.OrderedDict(sndict.iterflatten(-2)).keys(), col.OrderedDict(sndict.iterflatten(1)).keys(), ) assert list_equal( col.OrderedDict(sndict.iterflatten(-1)).keys(), col.OrderedDict(sndict.iterflatten(2)).keys(), )
def test_stratify(): named_sndict_a = StructuredNestedDict( dict_a, levels=3, level_names=["a", "b", "c"]) flattened_stratified_named_sndict_a = named_sndict_a\ .flatten(1, named=True).stratify(1) assert list_equal( flattened_stratified_named_sndict_a.level_names, ["a", "b", "c"] ) assert flattened_stratified_named_sndict_a.dim == (2, 3, 5) assert flattened_stratified_named_sndict_a
def test_filter_key(): sndict_a = StructuredNestedDict(dict_a, levels=3) sndict_c = StructuredNestedDict(dict_c, levels=3) assert list_equal( sndict_a.filter_key(["key1"]).keys(), ['key1'] ) assert list_equal( sndict_a.filter_key([["key1", "key3"]]).keys(), ['key1', 'key3'], ) assert list_equal( sndict_a.filter_key([["key1", "key3"]], drop_empty=True).keys(), ['key1'], ) assert list_equal( sndict_a.filter_key([slice(None), "key1_1"]).flatten_values(), ['val1_1_1', 'val1_1_2'], ) assert list_equal( sndict_a.filter_key({"level1": "key1_1"}).flatten_values(), ['val1_1_1', 'val1_1_2'], ) assert list_equal( sndict_c.filter_key({"level1": "keyX_1"}).flatten_values(), ['val1_1_1', 'val1_1_2', 'val2_1_1', 'val2_1_2'], )
def test_flatten(): named_sndict_a = StructuredNestedDict( dict_a, levels=3, level_names=["a", "b", "c"]) assert named_sndict_a.flatten(0).dim == named_sndict_a.dim assert named_sndict_a.flatten(1).dim == (3, 5) assert list_equal( map(tuple, named_sndict_a.flatten(1).keys()), [('key1', 'key1_1'), ('key2', 'key2_1'), ('key2', 'key2_2')], ) assert named_sndict_a.flatten(1).level_names == ("a___b", "c") with pytest.raises(LevelError): named_sndict_a.flatten(3) assert len(named_sndict_a.flatten_keys()) == named_sndict_a.dim[-1]
def test_convert(): converted_b = NestedDict(dict_a).convert() assert isinstance(converted_b, NestedDict) assert isinstance(list(converted_b.values())[1], NestedDict) assert isinstance( list(list(converted_b.values())[2].values())[0], NestedDict) converted_b = NestedDict(dict_a).convert("odict") assert isinstance(converted_b, col.OrderedDict) assert isinstance(list(converted_b.values())[1], col.OrderedDict) assert isinstance( list(list(converted_b.values())[2].values())[0], col.OrderedDict) assert isinstance(NestedDict(empty_dict).convert(), NestedDict) assert list_equal( NestedDict.from_flat(dict_data_b).sort_nested_keys().flatten_keys(), [('key_a', 'key_a_a'), ('key_a', 'key_a_b'), ('key_b', )], )
def test_ix(): sndict_a = StructuredNestedDict(dict_a, levels=3) assert sndict_a.ix['key1', 'key1_1', 'key1_1_1'] == "val1_1_1" try: _ = sndict_a.ix['keyX', 'keyX_1', 'keyX_1_1'] raise RuntimeError except KeyError: pass sndict_a.ix['keyX', 'keyX_1', 'keyX_1_1'] = "valX_1_1" assert sndict_a.ix['keyX', 'keyX_1', 'keyX_1_1'] == "valX_1_1" sndict_c = StructuredNestedDict(dict_c, levels=3) sndict_c.ix[:, "keyX_1", :] = "NEW" assert list_equal( sndict_c.flatten_values(), ['NEW', 'NEW', 'NEW', 'NEW', 'val2_2_1', 'val2_2_2', 'val2_2_3', 'val2_2_4', 'val2_2_5'], )
def test_filter_values(): assert list_equal( NestedDict.from_flat(dict_data_b).filter_values( lambda _: _[-1] == "b").flatten_keys(), [('key_b', ), ('key_a', 'key_a_b')], )
def test_sort_values(): assert list_equal( StructuredNestedDict({"a": "z", "b": "y", "c": "x"}) .sort_values().values(), ["x", "y", "z"], )
def test_from_flat(): assert list_equal( NestedDict.from_flat(dict_data_b).keys(), ['key_b', 'key_a'], )