crosszip 0.1.0__py3-none-any.whl
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.
- crosszip/__init__.py +1 -0
- crosszip/crosszip.py +75 -0
- crosszip/py.typed +0 -0
- crosszip-0.1.0.dist-info/METADATA +133 -0
- crosszip-0.1.0.dist-info/RECORD +7 -0
- crosszip-0.1.0.dist-info/WHEEL +4 -0
- crosszip-0.1.0.dist-info/licenses/LICENSE.md +21 -0
crosszip/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .crosszip import crosszip as crosszip
|
crosszip/crosszip.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
from collections.abc import Callable, Iterable
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def crosszip(func: Callable[..., Any], *iterables: Iterable[Any]) -> list[Any]:
|
|
7
|
+
"""Apply a given function to all combinations of elements from multiple iterables.
|
|
8
|
+
|
|
9
|
+
This function computes the Cartesian product of the input iterables (i.e., all possible
|
|
10
|
+
combinations of their elements) and applies the provided function to each combination.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
func (Callable[..., Any]): A function that accepts as many arguments as there are iterables.
|
|
14
|
+
*iterables (Iterable[Any]): Two or more iterables to generate combinations from. Each iterable
|
|
15
|
+
should contain elements that are valid inputs for the function `func`.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
list[Any]: A list of results from applying the function to each combination of elements.
|
|
19
|
+
|
|
20
|
+
Raises:
|
|
21
|
+
TypeError: If any of the provided arguments is not an iterable.
|
|
22
|
+
|
|
23
|
+
Example Usage:
|
|
24
|
+
|
|
25
|
+
# Example 1: Basic usage with lists
|
|
26
|
+
>>> def concat(a, b, c):
|
|
27
|
+
>>> return f"{a}-{b}-{c}"
|
|
28
|
+
|
|
29
|
+
>>> list1 = [1, 2]
|
|
30
|
+
>>> list2 = ['a', 'b']
|
|
31
|
+
>>> list3 = [True, False]
|
|
32
|
+
|
|
33
|
+
>>> crosszip(concat, list1, list2, list3)
|
|
34
|
+
['1-a-True', '1-a-False', '1-b-True', '1-b-False', '2-a-True', '2-a-False', '2-b-True', '2-b-False']
|
|
35
|
+
|
|
36
|
+
# Example 2: Using tuples and a mathematical function
|
|
37
|
+
>>> def add(a, b):
|
|
38
|
+
>>> return a + b
|
|
39
|
+
|
|
40
|
+
>>> crosszip(add, (1, 2), (10, 20))
|
|
41
|
+
[11, 21, 12, 22]
|
|
42
|
+
|
|
43
|
+
# Example 3: Using sets (order may vary) and a string concatenation function
|
|
44
|
+
>>> crosszip(concat, {1, 2}, {'x', 'y'}, {'foo', 'bar'})
|
|
45
|
+
['1-x-foo', '1-x-bar', '1-y-foo', '1-y-bar', '2-x-foo', '2-x-bar', '2-y-foo', '2-y-bar']
|
|
46
|
+
|
|
47
|
+
# Example 4: Using a generator
|
|
48
|
+
>>> def gen():
|
|
49
|
+
>>> yield 1
|
|
50
|
+
>>> yield 2
|
|
51
|
+
|
|
52
|
+
>>> crosszip(concat, gen(), ['a', 'b'], ['x', 'y'])
|
|
53
|
+
['1-a-x', '1-a-y', '1-b-x', '1-b-y', '2-a-x', '2-a-y', '2-b-x', '2-b-y']
|
|
54
|
+
|
|
55
|
+
Example with Error Handling:
|
|
56
|
+
|
|
57
|
+
# Example 5: Passing a non-iterable argument (raises TypeError)
|
|
58
|
+
>>> crosszip(concat, [1, 2], 123, ['a', 'b'])
|
|
59
|
+
TypeError: Expected an iterable, but got int: 123
|
|
60
|
+
|
|
61
|
+
Notes:
|
|
62
|
+
- The function assumes that each iterable contains values compatible with the function `func`.
|
|
63
|
+
- For large input iterables, the number of combinations grows exponentially, so use with care when working with large datasets.
|
|
64
|
+
|
|
65
|
+
"""
|
|
66
|
+
# Ensure all arguments are iterable
|
|
67
|
+
for iterable in iterables:
|
|
68
|
+
if not isinstance(iterable, Iterable):
|
|
69
|
+
raise TypeError(
|
|
70
|
+
f"Expected an iterable, but got {type(iterable).__name__}: {iterable}",
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Apply the function to the Cartesian product of all the iterables
|
|
74
|
+
combinations = itertools.product(*iterables)
|
|
75
|
+
return list(itertools.starmap(func, combinations))
|
crosszip/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: crosszip
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Apply a given function to all combinations of elements from multiple iterables
|
|
5
|
+
Project-URL: Repository, https://github.com/IndrajeetPatil/crosszip
|
|
6
|
+
Project-URL: Changelog, https://github.com/IndrajeetPatil/crosszip/blob/main/CHANGELOG.md
|
|
7
|
+
Author-email: Indrajeet Patil <patilindrajeet.science@gmail.com>
|
|
8
|
+
License: # MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2024 crosszip authors
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
Keywords: iterables
|
|
30
|
+
Classifier: Development Status :: 4 - Beta
|
|
31
|
+
Classifier: Environment :: Console
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Programming Language :: Python
|
|
36
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
39
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
40
|
+
Requires-Python: >=3.12
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
|
|
43
|
+
# crosszip <img src="docs/assets/logo.png" align="right" width="240" />
|
|
44
|
+
|
|
45
|
+
[](https://pypi.org/project/crosszip/)
|
|
46
|
+
[](https://opensource.org/licenses/MIT)
|
|
47
|
+
|
|
48
|
+
`crosszip` is a Python utility that makes it easy to apply a function to all possible combinations of elements from multiple iterables.
|
|
49
|
+
It combines the power of the Cartesian product and functional programming into a single, intuitive tool.
|
|
50
|
+
|
|
51
|
+
Additionally, `crosszip_parametrize` Pytest plugin is a decorator that simplifies running tests with all possible combinations of parameter values.
|
|
52
|
+
This is particularly useful for testing functions or components across multiple dimensions of input parameters.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
Install `crosszip` via pip:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install crosszip
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Registering the crosszip marker for pytest is simple. Just add the following to your `pytest.ini` file:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
[pytest]
|
|
68
|
+
markers =
|
|
69
|
+
crosszip_parametrize: "mark test function for crosszip parametrization"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Key Features
|
|
75
|
+
|
|
76
|
+
- **Flexible Input**: Works with any iterables, including lists, tuples, sets, and generators.
|
|
77
|
+
- **pytest Plugin**: Supports parametrization of tests using `crosszip`.
|
|
78
|
+
- **Simple API**: Minimalist, intuitive design for quick integration into your projects.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Usage
|
|
83
|
+
|
|
84
|
+
Example of using `crosszip`:
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
# Label Generation for Machine Learning
|
|
88
|
+
|
|
89
|
+
from crosszip import crosszip
|
|
90
|
+
|
|
91
|
+
def create_label(category, subcategory, version):
|
|
92
|
+
return f"{category}_{subcategory}_v{version}"
|
|
93
|
+
|
|
94
|
+
categories = ["cat", "dog"]
|
|
95
|
+
subcategories = ["small", "large"]
|
|
96
|
+
versions = ["1.0", "2.0"]
|
|
97
|
+
|
|
98
|
+
labels = crosszip(create_label, categories, subcategories, versions)
|
|
99
|
+
print(labels)
|
|
100
|
+
# Output: ['cat_small_v1.0', 'cat_small_v2.0', 'cat_large_v1.0', 'cat_large_v2.0', 'dog_small_v1.0', 'dog_small_v2.0', 'dog_large_v1.0', 'dog_large_v2.0']
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Example of using `pytest` marker `crosszip_parametrize`:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
# Testing Power Function
|
|
107
|
+
|
|
108
|
+
import math
|
|
109
|
+
from crosszip_parametrize import crosszip_parametrize
|
|
110
|
+
|
|
111
|
+
@crosszip_parametrize(
|
|
112
|
+
"base", [2, 10],
|
|
113
|
+
"exponent", [-1, 0, 1],
|
|
114
|
+
)
|
|
115
|
+
def test_power_function(base, exponent):
|
|
116
|
+
result = math.pow(base, exponent)
|
|
117
|
+
assert result == base ** exponent
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
For more examples, check out the package documentation at:
|
|
121
|
+
https://indrajeetpatil.github.io/crosszip/
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
This project is licensed under the [MIT License](LICENSE.md).
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Acknowledgements
|
|
132
|
+
|
|
133
|
+
Hex sticker font is `Rubik`, and the image is taken from icon made by Freepik and available at flaticon.com.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
crosszip/__init__.py,sha256=mH09JIhqhR3m5dVKQI1UodqG_MATcbtx9zcHwhxC6f0,43
|
|
2
|
+
crosszip/crosszip.py,sha256=IqLYk1G4QzmpzblIwh3apa2DqN0V120k7ZVz9mKzst8,2917
|
|
3
|
+
crosszip/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
crosszip-0.1.0.dist-info/METADATA,sha256=ZAJhipmepliIqqz_61m-LWEoPO8izoo63tBpxheo7tQ,4777
|
|
5
|
+
crosszip-0.1.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
6
|
+
crosszip-0.1.0.dist-info/licenses/LICENSE.md,sha256=ke3wuISxJfeZGp8fg3M7EamrT8nXtbiQlPr2j9XUAe4,1075
|
|
7
|
+
crosszip-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 crosszip authors
|
|
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.
|