aldict 1.0.0__tar.gz → 1.0.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aldict
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
5
5
  Author-email: Kaloyan Ivanov <kaloyan.ivanov88@gmail.com>
6
6
  Project-URL: repository, https://github.com/kaliv0/aldict
@@ -25,6 +25,7 @@ Requires-Dist: twine>=6.0.1; extra == "dev"
25
25
  ![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
26
26
  [![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
27
27
  [![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
28
+ [![Downloads](https://static.pepy.tech/badge/aldict)](https://pepy.tech/projects/aldict)
28
29
 
29
30
  Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
30
31
 
@@ -102,3 +103,12 @@ assert list(ad.items()) == [('x', 10), ('Xx', 10)]
102
103
  ```python
103
104
  assert list(ad.origin_keys()) == ['x', 'y']
104
105
  ```
106
+ - origin_len
107
+ <br>(get original dict <i>length</i> without aliases)
108
+ ```python
109
+ ad = AliasDict({"a": 1, "b": 2})
110
+ ad.add_alias("a", "aa")
111
+ assert list(ad.keys()) == ["a", "b", "aa"]
112
+ assert len(ad) == 3
113
+ assert ad.origin_len() == 2
114
+ ```
@@ -9,6 +9,7 @@
9
9
  ![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
10
10
  [![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
11
11
  [![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
12
+ [![Downloads](https://static.pepy.tech/badge/aldict)](https://pepy.tech/projects/aldict)
12
13
 
13
14
  Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
14
15
 
@@ -85,4 +86,13 @@ assert list(ad.items()) == [('x', 10), ('Xx', 10)]
85
86
  <br>(get original <i>keys</i> only)
86
87
  ```python
87
88
  assert list(ad.origin_keys()) == ['x', 'y']
88
- ```
89
+ ```
90
+ - origin_len
91
+ <br>(get original dict <i>length</i> without aliases)
92
+ ```python
93
+ ad = AliasDict({"a": 1, "b": 2})
94
+ ad.add_alias("a", "aa")
95
+ assert list(ad.keys()) == ["a", "b", "aa"]
96
+ assert len(ad) == 3
97
+ assert ad.origin_len() == 2
98
+ ```
@@ -8,9 +8,10 @@ class AliasDict(UserDict):
8
8
 
9
9
  def __init__(self, dict_):
10
10
  self._alias_dict = {}
11
- super().__init__(self, **dict_)
11
+ super().__init__(**dict_)
12
12
 
13
13
  def add_alias(self, key, *aliases):
14
+ """Adds one or more aliases to specified key in the dictionary"""
14
15
  if key not in self.data.keys():
15
16
  raise KeyError(key)
16
17
  for alias in aliases:
@@ -19,6 +20,7 @@ class AliasDict(UserDict):
19
20
  self._alias_dict[alias] = key
20
21
 
21
22
  def remove_alias(self, *aliases):
23
+ """Removes one or more aliases"""
22
24
  for alias in aliases:
23
25
  try:
24
26
  self._alias_dict.__delitem__(alias)
@@ -26,29 +28,43 @@ class AliasDict(UserDict):
26
28
  raise AliasError(alias) from e
27
29
 
28
30
  def clear_aliases(self):
31
+ """Removes all aliases"""
29
32
  self._alias_dict.clear()
30
33
 
31
34
  def aliases(self):
35
+ """Returns all aliases present in the dictionary"""
32
36
  return self._alias_dict.keys()
33
37
 
34
38
  def aliased_keys(self):
39
+ """Returns a dictview of all keys with their corresponding aliases"""
35
40
  result = defaultdict(list)
36
41
  for alias, key in self._alias_dict.items():
37
42
  result[key].append(alias)
38
43
  return result.items()
39
44
 
40
45
  def origin_keys(self):
46
+ """Returns all keys"""
41
47
  return self.data.keys()
42
48
 
43
49
  def keys(self):
50
+ """Returns all keys and aliases"""
44
51
  return dict(**self.data, **self._alias_dict).keys()
45
52
 
46
53
  def values(self):
54
+ """Returns all values"""
47
55
  return self.data.values()
48
56
 
49
57
  def items(self):
58
+ """Returns a dictview with all items (including alias/value tuples)"""
50
59
  return dict(**self.data, **{k: self.data[v] for k, v in self._alias_dict.items()}).items()
51
60
 
61
+ def origin_len(self):
62
+ """Returns the length of the original dictionary (without aliases)"""
63
+ return len(self.data)
64
+
65
+ def __len__(self):
66
+ return len(self.keys())
67
+
52
68
  def __missing__(self, key):
53
69
  try:
54
70
  return super().__getitem__(self._alias_dict[key])
@@ -65,10 +81,10 @@ class AliasDict(UserDict):
65
81
  def __delitem__(self, key):
66
82
  try:
67
83
  self.data.__delitem__(key)
84
+ self._alias_dict = {k: v for k, v in self._alias_dict.items() if v != key}
68
85
  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}
86
+ # in case we try to delete alias via pop() or del
87
+ return self.remove_alias(key)
72
88
 
73
89
  def __contains__(self, item):
74
90
  return item in set(self.keys())
@@ -77,9 +93,6 @@ class AliasDict(UserDict):
77
93
  for item in self.keys():
78
94
  yield item
79
95
 
80
- def __len__(self):
81
- return len(self.keys())
82
-
83
96
  def __repr__(self):
84
97
  return f"AliasDict({self.items()})"
85
98
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aldict
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
5
5
  Author-email: Kaloyan Ivanov <kaloyan.ivanov88@gmail.com>
6
6
  Project-URL: repository, https://github.com/kaliv0/aldict
@@ -25,6 +25,7 @@ Requires-Dist: twine>=6.0.1; extra == "dev"
25
25
  ![Python 3.x](https://img.shields.io/badge/python-^3.11-blue?style=flat-square&logo=Python&logoColor=white)
26
26
  [![PyPI](https://img.shields.io/pypi/v/aldict.svg)](https://pypi.org/project/aldict/)
27
27
  [![License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)](https://github.com/kaliv0/aldict/blob/main/LICENSE)
28
+ [![Downloads](https://static.pepy.tech/badge/aldict)](https://pepy.tech/projects/aldict)
28
29
 
29
30
  Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values
30
31
 
@@ -102,3 +103,12 @@ assert list(ad.items()) == [('x', 10), ('Xx', 10)]
102
103
  ```python
103
104
  assert list(ad.origin_keys()) == ['x', 'y']
104
105
  ```
106
+ - origin_len
107
+ <br>(get original dict <i>length</i> without aliases)
108
+ ```python
109
+ ad = AliasDict({"a": 1, "b": 2})
110
+ ad.add_alias("a", "aa")
111
+ assert list(ad.keys()) == ["a", "b", "aa"]
112
+ assert len(ad) == 3
113
+ assert ad.origin_len() == 2
114
+ ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "aldict"
7
- version = "1.0.0"
7
+ version = "1.0.2"
8
8
  readme = "README.md"
9
9
  authors = [{ name = "Kaloyan Ivanov", email = "kaloyan.ivanov88@gmail.com" }]
10
10
  description = "Multi-key dictionary, supports adding and manipulating key-aliases pointing to shared values"
@@ -142,7 +142,7 @@ def test_pop_alias_doesnt_remove_key(alias_dict):
142
142
  "import_mod": "yaml",
143
143
  "read_mode": "r",
144
144
  }
145
- assert ".yaml" in alias_dict
145
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml"]
146
146
 
147
147
 
148
148
  def test_iter(alias_dict):
@@ -194,6 +194,11 @@ def test_dict_len_includes_aliases(alias_dict):
194
194
  assert len(alias_dict) == 4
195
195
 
196
196
 
197
+ def test_dict_origin_len_excludes_aliases(alias_dict):
198
+ assert list(alias_dict.keys()) == [".json", ".yaml", ".toml", ".yml"]
199
+ assert alias_dict.origin_len() == 3
200
+
201
+
197
202
  def test_popitem(alias_dict):
198
203
  # pops first item -> MutableMapping.popitem()
199
204
  assert alias_dict.popitem() == (
File without changes
File without changes
File without changes
File without changes