Skip to content

Commit 51408bd

Browse files
committed
expanded readme
1 parent ec9b0dd commit 51408bd

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

README.md

+76-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,86 @@
33
</p>
44

55
---
6-
76
# Aldict
87

98
[![tests](https://img.shields.io/github/actions/workflow/status/kaliv0/aldict/ci.yml)](https://github.com/kaliv0/aldict/actions/workflows/ci.yml)
109
![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
1110
[![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
1211
[![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
1312

14-
Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
13+
Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
14+
15+
---
16+
## How to use
17+
18+
- add_alias
19+
<br>(pass <i>key</i> as first parameter and <i>alias(es)</i> as variadic params)
20+
```python
21+
ad = AliasDict({"a": 1, "b": 2})
22+
ad.add_alias("a", "aa")
23+
ad.add_alias("b", "bb", "Bbb")
24+
assert ad["a"] == ad["aa"] == 1
25+
assert ad["b"] == ad["bb"] == ad["Bbb"] == 2
26+
```
27+
- remove_alias
28+
<br>(pass <i>alias(es)</i> to be removed as variadic parameters)
29+
```python
30+
ad.remove_alias("aa")
31+
ad.remove_alias("bb", "Bbb")
32+
assert len(ad.aliases()) == 0
33+
```
34+
- clear_aliases
35+
<br>(remove all <i>aliases</i> at once)
36+
```python
37+
ad.clear_aliases()
38+
assert len(ad.aliases()) == 0
39+
```
40+
- update alias
41+
<br>(point <i>alias</i> to different <i>key</i>)
42+
```python
43+
ad = AliasDict({"a": 1, "b": 2})
44+
ad.add_alias("a", "ab")
45+
assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 1)]
46+
47+
ad.add_alias("b", "ab")
48+
assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 2)]
49+
```
50+
- read all aliases
51+
```python
52+
ad = AliasDict({"a": 1, "b": 2})
53+
ad.add_alias("a", "aa")
54+
ad.add_alias("b", "bb", "B")
55+
ad.add_alias("a", "ab", "A")
56+
assert list(ad.aliases()) == ['aa', 'bb', 'B', 'ab', 'A']
57+
```
58+
- aliased_keys
59+
<br>(read <i>keys</i> with corresponding <i>alias(es)</i>)
60+
```python
61+
assert dict(ad.aliased_keys()) == {'a': ['aa', 'ab', 'A'], 'b': ['bb', 'B']}
62+
```
63+
- read dictviews
64+
<br>(<i>dict.keys()</i> and <i>dict.items()</i> include <i>aliased</i> versions)
65+
```python
66+
ad = AliasDict({"x": 10, "y": 20})
67+
ad.add_alias("x", "Xx")
68+
ad.add_alias("y", "Yy", "xyz")
69+
70+
ad.keys()
71+
ad.values()
72+
ad.items()
73+
```
74+
```shell
75+
dict_keys(['x', 'y', 'Xx', 'Yy', 'xyz'])
76+
dict_values([10, 20])
77+
dict_items([('x', 10), ('y', 20), ('Xx', 10), ('Yy', 20), ('xyz', 20)])
78+
```
79+
- remove key and aliases
80+
```python
81+
ad.pop("y")
82+
assert list(ad.items()) == [('x', 10), ('Xx', 10)]
83+
```
84+
- origin_keys
85+
<br>(get original <i>keys</i> only)
86+
```python
87+
assert list(ad.origin_keys()) == ['x', 'y']
88+
```

aldict/alias_dict.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def remove_alias(self, *aliases):
2222
for alias in aliases:
2323
try:
2424
self._alias_dict.__delitem__(alias)
25-
except KeyError:
26-
raise AliasError(alias)
25+
except KeyError as e:
26+
raise AliasError(alias) from e
2727

2828
def clear_aliases(self):
2929
self._alias_dict.clear()
@@ -52,8 +52,8 @@ def items(self):
5252
def __missing__(self, key):
5353
try:
5454
return super().__getitem__(self._alias_dict[key])
55-
except AttributeError:
56-
raise KeyError(key)
55+
except AttributeError as e:
56+
raise KeyError(key) from e
5757

5858
def __setitem__(self, key, value):
5959
try:
@@ -77,6 +77,9 @@ def __iter__(self):
7777
for item in self.keys():
7878
yield item
7979

80+
def __len__(self):
81+
return len(self.keys())
82+
8083
def __repr__(self):
8184
return f"AliasDict({self.items()})"
8285

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "aldict"
7-
version = "0.1.0"
7+
version = "1.0.0"
88
readme = "README.md"
99
authors = [{ name = "Kaloyan Ivanov", email = "kaloyan.ivanov88@gmail.com" }]
1010
description = "Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values"

tests/test_alias_dict.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ def test_eq():
189189
assert ad_2 != ad_3
190190

191191

192-
def test_dict_len_doesnt_include_aliases(alias_dict):
192+
def test_dict_len_includes_aliases(alias_dict):
193193
assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
194-
assert len(alias_dict) == 3
194+
assert len(alias_dict) == 4
195195

196196

197197
def test_popitem(alias_dict):

0 commit comments

Comments
 (0)