Skip to content

kaliv0/aldict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Alter Ego


Aldict

tests Python 3.x PyPI License Downloads

Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values


How to use

  • add_alias
    (pass key as first parameter and alias(es) as variadic params)
ad = AliasDict({"a": 1, "b": 2})
ad.add_alias("a", "aa")
ad.add_alias("b", "bb", "Bbb")
assert ad["a"] == ad["aa"] == 1
assert ad["b"] == ad["bb"] == ad["Bbb"] == 2
  • remove_alias
    (pass alias(es) to be removed as variadic parameters)
ad.remove_alias("aa")
ad.remove_alias("bb", "Bbb")
assert len(ad.aliases()) == 0
  • clear_aliases
    (remove all aliases at once)
ad.clear_aliases()
assert len(ad.aliases()) == 0
  • update alias
    (point alias to different key)
ad = AliasDict({"a": 1, "b": 2})
ad.add_alias("a", "ab")
assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 1)]

ad.add_alias("b", "ab")
assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 2)]
  • read all aliases
ad = AliasDict({"a": 1, "b": 2})
ad.add_alias("a", "aa")
ad.add_alias("b", "bb", "B")
ad.add_alias("a", "ab", "A")
assert list(ad.aliases()) == ['aa', 'bb', 'B', 'ab', 'A']
  • aliased_keys
    (read keys with corresponding alias(es))
assert dict(ad.aliased_keys()) == {'a': ['aa', 'ab', 'A'], 'b': ['bb', 'B']}
  • read dictviews
    (dict.keys() and dict.items() include aliased versions)
ad = AliasDict({"x": 10, "y": 20})
ad.add_alias("x", "Xx")
ad.add_alias("y", "Yy", "xyz")

ad.keys()
ad.values()
ad.items()
dict_keys(['x', 'y', 'Xx', 'Yy', 'xyz'])
dict_values([10, 20])
dict_items([('x', 10), ('y', 20), ('Xx', 10), ('Yy', 20), ('xyz', 20)])
  • remove key and aliases
ad.pop("y")
assert list(ad.items()) == [('x', 10), ('Xx', 10)]
  • origin_keys
    (get original keys only)
assert list(ad.origin_keys()) == ['x', 'y']
  • origin_len
    (get original dict length without aliases)
ad = AliasDict({"a": 1, "b": 2})
ad.add_alias("a", "aa")
assert list(ad.keys()) == ["a", "b", "aa"]
assert len(ad) == 3
assert ad.origin_len() == 2

Releases

No releases published

Packages

No packages published

Languages