itertooldict 0.1.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 emunozgutier
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.
@@ -0,0 +1,97 @@
1
+ Metadata-Version: 2.4
2
+ Name: itertooldict
3
+ Version: 0.1.2
4
+ Summary: A missing feature of itertools: labeled product of multiple choices from a dictionary.
5
+ Author-email: Eduardo Munoz <emunozgutier@gmail.com>
6
+ Project-URL: Homepage, https://github.com/emunozgutier/iterdict
7
+ Project-URL: Bug Tracker, https://github.com/emunozgutier/iterdict/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.7
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # itertooldict
17
+
18
+ A missing feature of `itertools`: labeled product of multiple choices from a dictionary.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install itertooldict
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ `itertooldict` takes a dictionary where values are iterables and yields dictionaries representing their Cartesian product.
29
+
30
+ ```python
31
+ from itertooldict import itertooldict
32
+
33
+ data = {
34
+ "voltage": ["Vmax", "Vmin"],
35
+ "temp": ["hot", "cold"]
36
+ }
37
+
38
+ for combo in itertooldict(data):
39
+ print(combo)
40
+
41
+ # Output:
42
+ # {'voltage': 'Vmax', 'temp': 'hot'}
43
+ # {'voltage': 'Vmax', 'temp': 'cold'}
44
+ # {'voltage': 'Vmin', 'temp': 'hot'}
45
+ # {'voltage': 'Vmin', 'temp': 'cold'}
46
+ ```
47
+
48
+ ### Excluding Combinations
49
+
50
+ You can use the `.remove()` method to exclude specific combinations or patterns.
51
+
52
+ ```python
53
+ it = itertooldict(data)
54
+ it.remove({"voltage": "Vmax", "temp": "hot"})
55
+
56
+ for combo in it:
57
+ print(combo)
58
+ # {'voltage': 'Vmax', 'temp': 'cold'}
59
+ # {'voltage': 'Vmin', 'temp': 'hot'}
60
+ # {'voltage': 'Vmin', 'temp': 'cold'}
61
+ ```
62
+
63
+ You can also remove based on partial matches:
64
+
65
+ ### Randomizing Order
66
+
67
+ You can randomize the order of the generated combinations:
68
+
69
+ ```python
70
+ it = itertooldict(data).random()
71
+ for combo in it:
72
+ print(combo) # Order will be shuffled
73
+ ```
74
+
75
+ ### Updating Key Order
76
+
77
+ You can specify the order of keys in the resulting dictionaries:
78
+
79
+ ```python
80
+ it = itertooldict(data)
81
+ it.updateKeyOrder(["temp", "voltage"])
82
+ for combo in it:
83
+ print(combo) # {'temp': 'hot', 'voltage': 'Vmax'}, ...
84
+ ```
85
+
86
+ ### Compatibility with list() and enumerate()
87
+
88
+ `itertooldict` works seamlessly with standard Python functions:
89
+
90
+ ```python
91
+ # Convert to list
92
+ all_combos = list(itertooldict(data))
93
+
94
+ # Use with enumerate
95
+ for i, combo in enumerate(itertooldict(data)):
96
+ print(f"{i}: {combo}")
97
+ ```
@@ -0,0 +1,82 @@
1
+ # itertooldict
2
+
3
+ A missing feature of `itertools`: labeled product of multiple choices from a dictionary.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install itertooldict
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ `itertooldict` takes a dictionary where values are iterables and yields dictionaries representing their Cartesian product.
14
+
15
+ ```python
16
+ from itertooldict import itertooldict
17
+
18
+ data = {
19
+ "voltage": ["Vmax", "Vmin"],
20
+ "temp": ["hot", "cold"]
21
+ }
22
+
23
+ for combo in itertooldict(data):
24
+ print(combo)
25
+
26
+ # Output:
27
+ # {'voltage': 'Vmax', 'temp': 'hot'}
28
+ # {'voltage': 'Vmax', 'temp': 'cold'}
29
+ # {'voltage': 'Vmin', 'temp': 'hot'}
30
+ # {'voltage': 'Vmin', 'temp': 'cold'}
31
+ ```
32
+
33
+ ### Excluding Combinations
34
+
35
+ You can use the `.remove()` method to exclude specific combinations or patterns.
36
+
37
+ ```python
38
+ it = itertooldict(data)
39
+ it.remove({"voltage": "Vmax", "temp": "hot"})
40
+
41
+ for combo in it:
42
+ print(combo)
43
+ # {'voltage': 'Vmax', 'temp': 'cold'}
44
+ # {'voltage': 'Vmin', 'temp': 'hot'}
45
+ # {'voltage': 'Vmin', 'temp': 'cold'}
46
+ ```
47
+
48
+ You can also remove based on partial matches:
49
+
50
+ ### Randomizing Order
51
+
52
+ You can randomize the order of the generated combinations:
53
+
54
+ ```python
55
+ it = itertooldict(data).random()
56
+ for combo in it:
57
+ print(combo) # Order will be shuffled
58
+ ```
59
+
60
+ ### Updating Key Order
61
+
62
+ You can specify the order of keys in the resulting dictionaries:
63
+
64
+ ```python
65
+ it = itertooldict(data)
66
+ it.updateKeyOrder(["temp", "voltage"])
67
+ for combo in it:
68
+ print(combo) # {'temp': 'hot', 'voltage': 'Vmax'}, ...
69
+ ```
70
+
71
+ ### Compatibility with list() and enumerate()
72
+
73
+ `itertooldict` works seamlessly with standard Python functions:
74
+
75
+ ```python
76
+ # Convert to list
77
+ all_combos = list(itertooldict(data))
78
+
79
+ # Use with enumerate
80
+ for i, combo in enumerate(itertooldict(data)):
81
+ print(f"{i}: {combo}")
82
+ ```
@@ -0,0 +1,22 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "itertooldict"
7
+ version = "0.1.2"
8
+ authors = [
9
+ { name="Eduardo Munoz", email="emunozgutier@gmail.com" },
10
+ ]
11
+ description = "A missing feature of itertools: labeled product of multiple choices from a dictionary."
12
+ readme = "README.md"
13
+ requires-python = ">=3.7"
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+
20
+ [project.urls]
21
+ "Homepage" = "https://github.com/emunozgutier/iterdict"
22
+ "Bug Tracker" = "https://github.com/emunozgutier/iterdict/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .core import itertooldict
2
+
3
+ __all__ = ["itertooldict"]
@@ -0,0 +1,63 @@
1
+ import itertools
2
+
3
+ class itertooldict:
4
+ """
5
+ An iterator that yields dictionaries representing the Cartesian product
6
+ of values in the input dictionary.
7
+ """
8
+ def __init__(self, data):
9
+ for key, value in data.items():
10
+ if not isinstance(value, list):
11
+ raise TypeError(f"Value for key '{key}' must be a list, got {type(value).__name__}")
12
+ if len(value) == 0:
13
+ raise ValueError(f"Value for key '{key}' must have at least one entry")
14
+
15
+ self._data = data
16
+ self._keys = list(data.keys())
17
+ self._exclusions = []
18
+ self._randomize = False
19
+
20
+ def updateKeyOrder(self, keys):
21
+ """Reorder the keys for iteration."""
22
+ if set(keys) != set(self._keys):
23
+ raise ValueError("Provided keys do not match existing keys")
24
+ self._keys = list(keys)
25
+ return self
26
+
27
+ def random(self):
28
+ """Randomize the order of yielded combinations."""
29
+ self._randomize = True
30
+ return self
31
+
32
+ def __iter__(self):
33
+ values = [self._data[k] for k in self._keys]
34
+ combos = list(itertools.product(*values))
35
+
36
+ if self._randomize:
37
+ import random
38
+ random.shuffle(combos)
39
+
40
+ for combo in combos:
41
+ d = dict(zip(self._keys, combo))
42
+ # Filter out excluded combinations
43
+ if not any(all(d.get(k) == v for k, v in exclusion.items()) for exclusion in self._exclusions):
44
+ yield d
45
+
46
+ def __len__(self):
47
+ # This is a bit tricky if there are exclusions
48
+ # But for list() compatibility, let's provide a rough estimate or handle it
49
+ # Actually, list() uses __iter__. __len__ is good for progress bars etc.
50
+ # However, itertools.product doesn't have len.
51
+ # Let's just calculate it.
52
+ import math
53
+ total = math.prod(len(self._data[k]) for k in self._keys)
54
+ return total # Note: this doesn't account for exclusions, but it's common in iterators
55
+
56
+
57
+ def remove(self, exclusion):
58
+ """
59
+ Exclude specific combinations from the iteration.
60
+ Example: it.remove({"voltage": Vmax, "temp": hot})
61
+ """
62
+ self._exclusions.append(exclusion)
63
+ return self
@@ -0,0 +1,97 @@
1
+ Metadata-Version: 2.4
2
+ Name: itertooldict
3
+ Version: 0.1.2
4
+ Summary: A missing feature of itertools: labeled product of multiple choices from a dictionary.
5
+ Author-email: Eduardo Munoz <emunozgutier@gmail.com>
6
+ Project-URL: Homepage, https://github.com/emunozgutier/iterdict
7
+ Project-URL: Bug Tracker, https://github.com/emunozgutier/iterdict/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.7
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # itertooldict
17
+
18
+ A missing feature of `itertools`: labeled product of multiple choices from a dictionary.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install itertooldict
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ `itertooldict` takes a dictionary where values are iterables and yields dictionaries representing their Cartesian product.
29
+
30
+ ```python
31
+ from itertooldict import itertooldict
32
+
33
+ data = {
34
+ "voltage": ["Vmax", "Vmin"],
35
+ "temp": ["hot", "cold"]
36
+ }
37
+
38
+ for combo in itertooldict(data):
39
+ print(combo)
40
+
41
+ # Output:
42
+ # {'voltage': 'Vmax', 'temp': 'hot'}
43
+ # {'voltage': 'Vmax', 'temp': 'cold'}
44
+ # {'voltage': 'Vmin', 'temp': 'hot'}
45
+ # {'voltage': 'Vmin', 'temp': 'cold'}
46
+ ```
47
+
48
+ ### Excluding Combinations
49
+
50
+ You can use the `.remove()` method to exclude specific combinations or patterns.
51
+
52
+ ```python
53
+ it = itertooldict(data)
54
+ it.remove({"voltage": "Vmax", "temp": "hot"})
55
+
56
+ for combo in it:
57
+ print(combo)
58
+ # {'voltage': 'Vmax', 'temp': 'cold'}
59
+ # {'voltage': 'Vmin', 'temp': 'hot'}
60
+ # {'voltage': 'Vmin', 'temp': 'cold'}
61
+ ```
62
+
63
+ You can also remove based on partial matches:
64
+
65
+ ### Randomizing Order
66
+
67
+ You can randomize the order of the generated combinations:
68
+
69
+ ```python
70
+ it = itertooldict(data).random()
71
+ for combo in it:
72
+ print(combo) # Order will be shuffled
73
+ ```
74
+
75
+ ### Updating Key Order
76
+
77
+ You can specify the order of keys in the resulting dictionaries:
78
+
79
+ ```python
80
+ it = itertooldict(data)
81
+ it.updateKeyOrder(["temp", "voltage"])
82
+ for combo in it:
83
+ print(combo) # {'temp': 'hot', 'voltage': 'Vmax'}, ...
84
+ ```
85
+
86
+ ### Compatibility with list() and enumerate()
87
+
88
+ `itertooldict` works seamlessly with standard Python functions:
89
+
90
+ ```python
91
+ # Convert to list
92
+ all_combos = list(itertooldict(data))
93
+
94
+ # Use with enumerate
95
+ for i, combo in enumerate(itertooldict(data)):
96
+ print(f"{i}: {combo}")
97
+ ```
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/itertooldict/__init__.py
5
+ src/itertooldict/core.py
6
+ src/itertooldict.egg-info/PKG-INFO
7
+ src/itertooldict.egg-info/SOURCES.txt
8
+ src/itertooldict.egg-info/dependency_links.txt
9
+ src/itertooldict.egg-info/top_level.txt
10
+ tests/test_core.py
@@ -0,0 +1 @@
1
+ itertooldict
@@ -0,0 +1,73 @@
1
+ import unittest
2
+ from itertooldict import itertooldict
3
+
4
+ class TestIterdict(unittest.TestCase):
5
+ def test_basic_product(self):
6
+ data = {"a": [1, 2], "b": ["x", "y"]}
7
+ it = itertooldict(data)
8
+ results = list(it)
9
+ expected = [
10
+ {"a": 1, "b": "x"},
11
+ {"a": 1, "b": "y"},
12
+ {"a": 2, "b": "x"},
13
+ {"a": 2, "b": "y"},
14
+ ]
15
+ self.assertEqual(results, expected)
16
+
17
+ def test_remove_single(self):
18
+ data = {"voltage": [10, 20], "temp": ["hot", "cold"]}
19
+ it = itertooldict(data)
20
+ it.remove({"voltage": 10, "temp": "hot"})
21
+ results = list(it)
22
+ expected = [
23
+ {"voltage": 10, "temp": "cold"},
24
+ {"voltage": 20, "temp": "hot"},
25
+ {"voltage": 20, "temp": "cold"},
26
+ ]
27
+ self.assertEqual(results, expected)
28
+
29
+ def test_remove_partial(self):
30
+ data = {"a": [1, 2], "b": ["x", "y"]}
31
+ it = itertooldict(data)
32
+ it.remove({"a": 1}) # Should remove all where a=1
33
+ results = list(it)
34
+ expected = [
35
+ {"a": 2, "b": "x"},
36
+ {"a": 2, "b": "y"},
37
+ ]
38
+ self.assertEqual(results, expected)
39
+
40
+ def test_validation_not_list(self):
41
+ with self.assertRaises(TypeError):
42
+ itertooldict({"a": "not a list"})
43
+
44
+ def test_validation_empty_list(self):
45
+ with self.assertRaises(ValueError):
46
+ itertooldict({"a": []})
47
+
48
+ def test_updateKeyOrder(self):
49
+ data = {"a": [1], "b": [2]}
50
+ it = itertooldict(data)
51
+ it.updateKeyOrder(["b", "a"])
52
+ result = list(it)[0]
53
+ self.assertEqual(list(result.keys()), ["b", "a"])
54
+
55
+ def test_random(self):
56
+ data = {"a": list(range(10)), "b": list(range(10))}
57
+ it1 = list(itertooldict(data))
58
+ it2 = list(itertooldict(data).random())
59
+ self.assertNotEqual(it1, it2)
60
+
61
+ def test_list_and_enumerate(self):
62
+ data = {"a": [1, 2], "b": [3, 4]}
63
+ it = itertooldict(data)
64
+ # Check list compatibility
65
+ l = list(it)
66
+ self.assertEqual(len(l), 4)
67
+ # Check enumerate compatibility
68
+ for i, val in enumerate(itertooldict(data)):
69
+ self.assertEqual(val, l[i])
70
+
71
+
72
+ if __name__ == "__main__":
73
+ unittest.main()