aldict 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
aldict-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Kaloyan Ivanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
aldict-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,104 @@
1
+ Metadata-Version: 2.1
2
+ Name: aldict
3
+ Version: 1.0.0
4
+ Summary: Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
5
+ Author-email: Kaloyan Ivanov <kaloyan.ivanov88@gmail.com>
6
+ Project-URL: repository, https://github.com/kaliv0/aldict
7
+ Keywords: multi-key dictionary,multidict,alias-dict
8
+ Requires-Python: >=3.11
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Provides-Extra: dev
12
+ Requires-Dist: pytest>=8.3.4; extra == "dev"
13
+ Requires-Dist: ruff>=0.8.3; extra == "dev"
14
+ Requires-Dist: build>=1.2.2; extra == "dev"
15
+ Requires-Dist: twine>=6.0.1; extra == "dev"
16
+
17
+ <p align="center">
18
+ <img src="https://github.com/kaliv0/aldict/blob/main/assets/alter-ego.jpg?raw=true" width="250" alt="Alter Ego">
19
+ </p>
20
+
21
+ ---
22
+ # Aldict
23
+
24
+ [![tests](https://img.shields.io/github/actions/workflow/status/kaliv0/aldict/ci.yml)](https://github.com/kaliv0/aldict/actions/workflows/ci.yml)
25
+ ![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
26
+ [![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
27
+ [![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
28
+
29
+ Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
30
+
31
+ ---
32
+ ## How to use
33
+
34
+ - add_alias
35
+ <br>(pass <i>key</i> as first parameter and <i>alias(es)</i> as variadic params)
36
+ ```python
37
+ ad = AliasDict({"a": 1, "b": 2})
38
+ ad.add_alias("a", "aa")
39
+ ad.add_alias("b", "bb", "Bbb")
40
+ assert ad["a"] == ad["aa"] == 1
41
+ assert ad["b"] == ad["bb"] == ad["Bbb"] == 2
42
+ ```
43
+ - remove_alias
44
+ <br>(pass <i>alias(es)</i> to be removed as variadic parameters)
45
+ ```python
46
+ ad.remove_alias("aa")
47
+ ad.remove_alias("bb", "Bbb")
48
+ assert len(ad.aliases()) == 0
49
+ ```
50
+ - clear_aliases
51
+ <br>(remove all <i>aliases</i> at once)
52
+ ```python
53
+ ad.clear_aliases()
54
+ assert len(ad.aliases()) == 0
55
+ ```
56
+ - update alias
57
+ <br>(point <i>alias</i> to different <i>key</i>)
58
+ ```python
59
+ ad = AliasDict({"a": 1, "b": 2})
60
+ ad.add_alias("a", "ab")
61
+ assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 1)]
62
+
63
+ ad.add_alias("b", "ab")
64
+ assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 2)]
65
+ ```
66
+ - read all aliases
67
+ ```python
68
+ ad = AliasDict({"a": 1, "b": 2})
69
+ ad.add_alias("a", "aa")
70
+ ad.add_alias("b", "bb", "B")
71
+ ad.add_alias("a", "ab", "A")
72
+ assert list(ad.aliases()) == ['aa', 'bb', 'B', 'ab', 'A']
73
+ ```
74
+ - aliased_keys
75
+ <br>(read <i>keys</i> with corresponding <i>alias(es)</i>)
76
+ ```python
77
+ assert dict(ad.aliased_keys()) == {'a': ['aa', 'ab', 'A'], 'b': ['bb', 'B']}
78
+ ```
79
+ - read dictviews
80
+ <br>(<i>dict.keys()</i> and <i>dict.items()</i> include <i>aliased</i> versions)
81
+ ```python
82
+ ad = AliasDict({"x": 10, "y": 20})
83
+ ad.add_alias("x", "Xx")
84
+ ad.add_alias("y", "Yy", "xyz")
85
+
86
+ ad.keys()
87
+ ad.values()
88
+ ad.items()
89
+ ```
90
+ ```shell
91
+ dict_keys(['x', 'y', 'Xx', 'Yy', 'xyz'])
92
+ dict_values([10, 20])
93
+ dict_items([('x', 10), ('y', 20), ('Xx', 10), ('Yy', 20), ('xyz', 20)])
94
+ ```
95
+ - remove key and aliases
96
+ ```python
97
+ ad.pop("y")
98
+ assert list(ad.items()) == [('x', 10), ('Xx', 10)]
99
+ ```
100
+ - origin_keys
101
+ <br>(get original <i>keys</i> only)
102
+ ```python
103
+ assert list(ad.origin_keys()) == ['x', 'y']
104
+ ```
aldict-1.0.0/README.md ADDED
@@ -0,0 +1,88 @@
1
+ <p align="center">
2
+ <img src="https://github.com/kaliv0/aldict/blob/main/assets/alter-ego.jpg?raw=true" width="250" alt="Alter Ego">
3
+ </p>
4
+
5
+ ---
6
+ # Aldict
7
+
8
+ [![tests](https://img.shields.io/github/actions/workflow/status/kaliv0/aldict/ci.yml)](https://github.com/kaliv0/aldict/actions/workflows/ci.yml)
9
+ ![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
10
+ [![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
11
+ [![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
12
+
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
+ ```
@@ -0,0 +1,3 @@
1
+ from .alias_dict import AliasDict as AliasDict
2
+ from .exception import AliasError as AliasError
3
+ from .exception import AliasValueError as AliasValueError
@@ -0,0 +1,92 @@
1
+ from collections import UserDict, defaultdict
2
+
3
+ from aldict.exception import AliasError, AliasValueError
4
+
5
+
6
+ class AliasDict(UserDict):
7
+ """Custom Dict class supporting key-aliases pointing to shared values"""
8
+
9
+ def __init__(self, dict_):
10
+ self._alias_dict = {}
11
+ super().__init__(self, **dict_)
12
+
13
+ def add_alias(self, key, *aliases):
14
+ if key not in self.data.keys():
15
+ raise KeyError(key)
16
+ for alias in aliases:
17
+ if alias == key:
18
+ raise AliasValueError(f"Key and corresponding alias cannot be equal: '{key}'")
19
+ self._alias_dict[alias] = key
20
+
21
+ def remove_alias(self, *aliases):
22
+ for alias in aliases:
23
+ try:
24
+ self._alias_dict.__delitem__(alias)
25
+ except KeyError as e:
26
+ raise AliasError(alias) from e
27
+
28
+ def clear_aliases(self):
29
+ self._alias_dict.clear()
30
+
31
+ def aliases(self):
32
+ return self._alias_dict.keys()
33
+
34
+ def aliased_keys(self):
35
+ result = defaultdict(list)
36
+ for alias, key in self._alias_dict.items():
37
+ result[key].append(alias)
38
+ return result.items()
39
+
40
+ def origin_keys(self):
41
+ return self.data.keys()
42
+
43
+ def keys(self):
44
+ return dict(**self.data, **self._alias_dict).keys()
45
+
46
+ def values(self):
47
+ return self.data.values()
48
+
49
+ def items(self):
50
+ return dict(**self.data, **{k: self.data[v] for k, v in self._alias_dict.items()}).items()
51
+
52
+ def __missing__(self, key):
53
+ try:
54
+ return super().__getitem__(self._alias_dict[key])
55
+ except AttributeError as e:
56
+ raise KeyError(key) from e
57
+
58
+ def __setitem__(self, key, value):
59
+ try:
60
+ key = self._alias_dict[key]
61
+ except KeyError:
62
+ pass
63
+ super().__setitem__(key, value)
64
+
65
+ def __delitem__(self, key):
66
+ try:
67
+ self.data.__delitem__(key)
68
+ except KeyError:
69
+ # in case we try to delete alias e.g. via pop()
70
+ pass
71
+ self._alias_dict = {k: v for k, v in self._alias_dict.items() if v != key}
72
+
73
+ def __contains__(self, item):
74
+ return item in set(self.keys())
75
+
76
+ def __iter__(self):
77
+ for item in self.keys():
78
+ yield item
79
+
80
+ def __len__(self):
81
+ return len(self.keys())
82
+
83
+ def __repr__(self):
84
+ return f"AliasDict({self.items()})"
85
+
86
+ def __eq__(self, other):
87
+ if not isinstance(other, AliasDict):
88
+ raise TypeError(f"{other} is not an AliasDict")
89
+ return self.data == other.data and self._alias_dict == other._alias_dict
90
+
91
+ def __hash__(self):
92
+ return hash((self.data, self._alias_dict))
@@ -0,0 +1,6 @@
1
+ class AliasError(KeyError):
2
+ pass
3
+
4
+
5
+ class AliasValueError(ValueError):
6
+ pass
@@ -0,0 +1,104 @@
1
+ Metadata-Version: 2.1
2
+ Name: aldict
3
+ Version: 1.0.0
4
+ Summary: Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
5
+ Author-email: Kaloyan Ivanov <kaloyan.ivanov88@gmail.com>
6
+ Project-URL: repository, https://github.com/kaliv0/aldict
7
+ Keywords: multi-key dictionary,multidict,alias-dict
8
+ Requires-Python: >=3.11
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Provides-Extra: dev
12
+ Requires-Dist: pytest>=8.3.4; extra == "dev"
13
+ Requires-Dist: ruff>=0.8.3; extra == "dev"
14
+ Requires-Dist: build>=1.2.2; extra == "dev"
15
+ Requires-Dist: twine>=6.0.1; extra == "dev"
16
+
17
+ <p align="center">
18
+ <img src="https://github.com/kaliv0/aldict/blob/main/assets/alter-ego.jpg?raw=true" width="250" alt="Alter Ego">
19
+ </p>
20
+
21
+ ---
22
+ # Aldict
23
+
24
+ [![tests](https://img.shields.io/github/actions/workflow/status/kaliv0/aldict/ci.yml)](https://github.com/kaliv0/aldict/actions/workflows/ci.yml)
25
+ ![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
26
+ [![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
27
+ [![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
28
+
29
+ Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
30
+
31
+ ---
32
+ ## How to use
33
+
34
+ - add_alias
35
+ <br>(pass <i>key</i> as first parameter and <i>alias(es)</i> as variadic params)
36
+ ```python
37
+ ad = AliasDict({"a": 1, "b": 2})
38
+ ad.add_alias("a", "aa")
39
+ ad.add_alias("b", "bb", "Bbb")
40
+ assert ad["a"] == ad["aa"] == 1
41
+ assert ad["b"] == ad["bb"] == ad["Bbb"] == 2
42
+ ```
43
+ - remove_alias
44
+ <br>(pass <i>alias(es)</i> to be removed as variadic parameters)
45
+ ```python
46
+ ad.remove_alias("aa")
47
+ ad.remove_alias("bb", "Bbb")
48
+ assert len(ad.aliases()) == 0
49
+ ```
50
+ - clear_aliases
51
+ <br>(remove all <i>aliases</i> at once)
52
+ ```python
53
+ ad.clear_aliases()
54
+ assert len(ad.aliases()) == 0
55
+ ```
56
+ - update alias
57
+ <br>(point <i>alias</i> to different <i>key</i>)
58
+ ```python
59
+ ad = AliasDict({"a": 1, "b": 2})
60
+ ad.add_alias("a", "ab")
61
+ assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 1)]
62
+
63
+ ad.add_alias("b", "ab")
64
+ assert list(ad.items()) == [('a', 1), ('b', 2), ('ab', 2)]
65
+ ```
66
+ - read all aliases
67
+ ```python
68
+ ad = AliasDict({"a": 1, "b": 2})
69
+ ad.add_alias("a", "aa")
70
+ ad.add_alias("b", "bb", "B")
71
+ ad.add_alias("a", "ab", "A")
72
+ assert list(ad.aliases()) == ['aa', 'bb', 'B', 'ab', 'A']
73
+ ```
74
+ - aliased_keys
75
+ <br>(read <i>keys</i> with corresponding <i>alias(es)</i>)
76
+ ```python
77
+ assert dict(ad.aliased_keys()) == {'a': ['aa', 'ab', 'A'], 'b': ['bb', 'B']}
78
+ ```
79
+ - read dictviews
80
+ <br>(<i>dict.keys()</i> and <i>dict.items()</i> include <i>aliased</i> versions)
81
+ ```python
82
+ ad = AliasDict({"x": 10, "y": 20})
83
+ ad.add_alias("x", "Xx")
84
+ ad.add_alias("y", "Yy", "xyz")
85
+
86
+ ad.keys()
87
+ ad.values()
88
+ ad.items()
89
+ ```
90
+ ```shell
91
+ dict_keys(['x', 'y', 'Xx', 'Yy', 'xyz'])
92
+ dict_values([10, 20])
93
+ dict_items([('x', 10), ('y', 20), ('Xx', 10), ('Yy', 20), ('xyz', 20)])
94
+ ```
95
+ - remove key and aliases
96
+ ```python
97
+ ad.pop("y")
98
+ assert list(ad.items()) == [('x', 10), ('Xx', 10)]
99
+ ```
100
+ - origin_keys
101
+ <br>(get original <i>keys</i> only)
102
+ ```python
103
+ assert list(ad.origin_keys()) == ['x', 'y']
104
+ ```
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ aldict/__init__.py
5
+ aldict/alias_dict.py
6
+ aldict/exception.py
7
+ aldict.egg-info/PKG-INFO
8
+ aldict.egg-info/SOURCES.txt
9
+ aldict.egg-info/dependency_links.txt
10
+ aldict.egg-info/requires.txt
11
+ aldict.egg-info/top_level.txt
12
+ tests/test_alias_dict.py
@@ -0,0 +1,6 @@
1
+
2
+ [dev]
3
+ pytest>=8.3.4
4
+ ruff>=0.8.3
5
+ build>=1.2.2
6
+ twine>=6.0.1
@@ -0,0 +1 @@
1
+ aldict
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["setuptools>=75.6.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "aldict"
7
+ version = "1.0.0"
8
+ readme = "README.md"
9
+ authors = [{ name = "Kaloyan Ivanov", email = "kaloyan.ivanov88@gmail.com" }]
10
+ description = "Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values"
11
+ keywords = ["multi-key dictionary", "multidict", "alias-dict"]
12
+ urls = { repository = "https://github.com/kaliv0/aldict" }
13
+
14
+ requires-python = ">= 3.11"
15
+
16
+ [project.optional-dependencies]
17
+ dev = [
18
+ "pytest>=8.3.4",
19
+ "ruff>=0.8.3",
20
+ "build>=1.2.2",
21
+ "twine>=6.0.1",
22
+ ]
23
+
24
+ [tool.setuptools.packages.find]
25
+ where = ["."]
26
+ include = ["aldict"]
27
+ exclude = ["tests"]
aldict-1.0.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,246 @@
1
+ import pytest
2
+
3
+ from aldict import AliasDict, AliasValueError, AliasError
4
+
5
+
6
+ def test_alias_dict(alias_dict):
7
+ assert alias_dict[".toml"] == {
8
+ "callable": "load",
9
+ "import_mod": "tomli",
10
+ "read_mode": "r",
11
+ }
12
+ assert (
13
+ alias_dict[".yml"]
14
+ == alias_dict[".yaml"]
15
+ == {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"}
16
+ )
17
+
18
+
19
+ def test_add_alias(alias_dict):
20
+ alias_dict.add_alias(".toml", ".tml")
21
+ assert (
22
+ alias_dict[".toml"]
23
+ == alias_dict[".tml"]
24
+ == {"callable": "load", "import_mod": "tomli", "read_mode": "r"}
25
+ )
26
+
27
+
28
+ def test_add_multiple_aliases(alias_dict):
29
+ alias_dict.add_alias(".json", ".jsn", ".joojoo", ".jazz")
30
+ assert list(alias_dict.keys()) == [
31
+ ".json",
32
+ ".yaml",
33
+ ".toml",
34
+ ".yml",
35
+ ".jsn",
36
+ ".joojoo",
37
+ ".jazz",
38
+ ]
39
+
40
+
41
+ def test_add_alias_raises(alias_dict):
42
+ with pytest.raises(
43
+ AliasValueError, match="Key and corresponding alias cannot be equal: '.toml'"
44
+ ):
45
+ alias_dict.add_alias(".toml", ".toml")
46
+
47
+
48
+ def test_update_alias(alias_dict):
49
+ # redirect ".yml" to point to ".toml"
50
+ alias_dict.add_alias(".toml", ".yml")
51
+ assert list(alias_dict.items()) == [
52
+ (".json", {"callable": "load", "import_mod": "json", "read_mode": "r"}),
53
+ (".yaml", {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"}),
54
+ (".toml", {"callable": "load", "import_mod": "tomli", "read_mode": "r"}),
55
+ (".yml", {"callable": "load", "import_mod": "tomli", "read_mode": "r"}),
56
+ ]
57
+
58
+
59
+ def test_update_alias_raises(alias_dict):
60
+ with pytest.raises(KeyError, match=".foo"):
61
+ alias_dict.add_alias(".foo", ".bar")
62
+
63
+
64
+ def test_remove_alias(alias_dict):
65
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
66
+
67
+ alias_dict.remove_alias(".yml")
68
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml"]
69
+ assert list(alias_dict.items()) == [
70
+ (".json", {"callable": "load", "import_mod": "json", "read_mode": "r"}),
71
+ (".yaml", {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"}),
72
+ (".toml", {"callable": "load", "import_mod": "tomli", "read_mode": "r"}),
73
+ ]
74
+
75
+
76
+ def test_remove_alias_raises(alias_dict):
77
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
78
+ with pytest.raises(AliasError, match=".foo"):
79
+ alias_dict.remove_alias(".foo")
80
+
81
+
82
+ def test_remove_multiple_aliases(alias_dict):
83
+ alias_dict.add_alias(".json", ".jsn")
84
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml", ".jsn"]
85
+
86
+ alias_dict.remove_alias(".yml", ".jsn")
87
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml"]
88
+
89
+
90
+ def test_read_aliases(alias_dict):
91
+ alias_dict.add_alias(".toml", ".tml")
92
+ alias_dict.add_alias(".json", ".jsn")
93
+ alias_dict.add_alias(".json", ".whaaaaat!")
94
+ assert list(alias_dict.aliases()) == [".yml", ".tml", ".jsn", ".whaaaaat!"]
95
+
96
+
97
+ def test_dictviews(alias_dict):
98
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
99
+ assert list(alias_dict.values()) == [
100
+ {"import_mod": "json", "callable": "load", "read_mode": "r"},
101
+ {"import_mod": "yaml", "callable": "safe_load", "read_mode": "r"},
102
+ {"import_mod": "tomli", "callable": "load", "read_mode": "r"},
103
+ ]
104
+ assert list(alias_dict.items()) == [
105
+ (".json", {"callable": "load", "import_mod": "json", "read_mode": "r"}),
106
+ (".yaml", {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"}),
107
+ (".toml", {"callable": "load", "import_mod": "tomli", "read_mode": "r"}),
108
+ (".yml", {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"}),
109
+ ]
110
+
111
+
112
+ def test_remove_key_and_aliases(alias_dict):
113
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
114
+ alias_dict.pop(".yaml")
115
+ assert list(alias_dict.keys()) == [".json", ".toml"]
116
+
117
+
118
+ def test_contains(alias_dict):
119
+ alias_dict.add_alias(".toml", ".tml")
120
+ assert (".toml" in alias_dict) is True
121
+ assert (".tml" in alias_dict) is True
122
+ assert (".foo" in alias_dict) is False
123
+
124
+
125
+ def test_get(alias_dict):
126
+ assert alias_dict.get(".yaml") == {
127
+ "callable": "safe_load",
128
+ "import_mod": "yaml",
129
+ "read_mode": "r",
130
+ }
131
+ assert alias_dict.get(".yml") == {
132
+ "callable": "safe_load",
133
+ "import_mod": "yaml",
134
+ "read_mode": "r",
135
+ }
136
+ assert alias_dict.get(".foo") is None
137
+
138
+
139
+ def test_pop_alias_doesnt_remove_key(alias_dict):
140
+ assert alias_dict.pop(".yml") == {
141
+ "callable": "safe_load",
142
+ "import_mod": "yaml",
143
+ "read_mode": "r",
144
+ }
145
+ assert ".yaml" in alias_dict
146
+
147
+
148
+ def test_iter(alias_dict):
149
+ assert [k for k in alias_dict] == [".json", ".yaml", ".toml", ".yml"]
150
+
151
+
152
+ def test_origin_keys(alias_dict):
153
+ assert list(alias_dict.origin_keys()) == [".json", ".yaml", ".toml"]
154
+
155
+
156
+ def test_aliased_keys(alias_dict):
157
+ assert list(alias_dict.aliased_keys()) == [(".yaml", [".yml"])]
158
+ alias_dict.add_alias(".toml", ".tml", ".tommy", ".tomograph")
159
+ assert list(alias_dict.aliased_keys()) == [
160
+ (".yaml", [".yml"]),
161
+ (".toml", [".tml", ".tommy", ".tomograph"]),
162
+ ]
163
+
164
+
165
+ def test_repr(alias_dict):
166
+ assert str(alias_dict) == (
167
+ "AliasDict(dict_items(["
168
+ "('.json', {'import_mod': 'json', 'callable': 'load', 'read_mode': 'r'}), "
169
+ "('.yaml', {'import_mod': 'yaml', 'callable': 'safe_load', 'read_mode': 'r'}), "
170
+ "('.toml', {'import_mod': 'tomli', 'callable': 'load', 'read_mode': 'r'}), "
171
+ "('.yml', {'import_mod': 'yaml', 'callable': 'safe_load', 'read_mode': 'r'})"
172
+ "]))"
173
+ )
174
+
175
+
176
+ def test_eq():
177
+ ad_0 = {"a": 1, "b": 2}
178
+ ad_1 = AliasDict(ad_0)
179
+ ad_1.add_alias("a", "aa", "aaa")
180
+
181
+ ad_2 = AliasDict(ad_0)
182
+ ad_2.add_alias("a", "aa", "aaa")
183
+
184
+ ad_3 = AliasDict(ad_0)
185
+ ad_3.add_alias("a", "abc")
186
+
187
+ assert ad_1 == ad_2
188
+ assert ad_1 != ad_3
189
+ assert ad_2 != ad_3
190
+
191
+
192
+ def test_dict_len_includes_aliases(alias_dict):
193
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
194
+ assert len(alias_dict) == 4
195
+
196
+
197
+ def test_popitem(alias_dict):
198
+ # pops first item -> MutableMapping.popitem()
199
+ assert alias_dict.popitem() == (
200
+ ".json",
201
+ {"callable": "load", "import_mod": "json", "read_mode": "r"},
202
+ )
203
+ assert alias_dict.popitem() == (
204
+ ".yaml",
205
+ {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"},
206
+ )
207
+ assert len(alias_dict.aliased_keys()) == 0
208
+ assert list(alias_dict.keys()) == [".toml"]
209
+
210
+
211
+ def test_clear(alias_dict):
212
+ alias_dict.clear()
213
+ assert len(alias_dict.items()) == 0
214
+
215
+
216
+ def test_clear_aliases(alias_dict):
217
+ alias_dict.clear_aliases()
218
+ assert len(alias_dict.aliases()) == 0
219
+ assert list(alias_dict.items()) == [
220
+ (".json", {"callable": "load", "import_mod": "json", "read_mode": "r"}),
221
+ (".yaml", {"callable": "safe_load", "import_mod": "yaml", "read_mode": "r"}),
222
+ (".toml", {"callable": "load", "import_mod": "tomli", "read_mode": "r"}),
223
+ ]
224
+
225
+
226
+ def test_setdefault():
227
+ ad = AliasDict({"a": 1, "b": 2})
228
+ ad.setdefault("foo", "bar")
229
+ ad.add_alias("foo", "fizz")
230
+ assert ad["foo"] == "bar"
231
+ assert ad["fizz"] == "bar"
232
+
233
+
234
+ def test_setdefault_on_existing_aliased_key():
235
+ ad = AliasDict({"a": 1, "b": 2})
236
+ ad.setdefault("a", 42)
237
+ ad.add_alias("a", "aa")
238
+ assert ad["a"] == 1
239
+ assert ad["aa"] == 1
240
+
241
+
242
+ def test_update_modifies_aliases():
243
+ ad = AliasDict({"a": 1, "b": 2})
244
+ ad.add_alias("a", "aa", "aaa")
245
+ ad.update(**{"a": 40, "y": 50})
246
+ assert list(ad.items()) == [("a", 40), ("b", 2), ("y", 50), ("aa", 40), ("aaa", 40)]