custom-combination 0.0.1__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.
@@ -0,0 +1,13 @@
1
+ __version__ = "0.0.1"
2
+
3
+ from . import cli # backward compat
4
+ from .core import generate as _generate
5
+
6
+
7
+ class CustomCombination:
8
+ @staticmethod
9
+ def generate(items, pick=None, min_picks=None, max_picks=None):
10
+ return _generate(items, pick, min_picks, max_picks)
11
+
12
+
13
+ __all__ = ["CustomCombination", "cli", "__version__"]
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env python
2
+ from typing import Annotated
3
+
4
+ import typer
5
+
6
+ from .core import generate as _core_generate
7
+
8
+ app = typer.Typer()
9
+
10
+ _SEPARATOR = ","
11
+
12
+
13
+ @app.command()
14
+ def generate(
15
+ items: Annotated[
16
+ str, typer.Option("--items", help="Comma-separated list, e.g. --items a,b,c")
17
+ ] = None,
18
+ min_picks: Annotated[
19
+ list[(str, int)],
20
+ typer.Option("--min-picks", "-minp", help="The items to be picked minimum e.g. -minp b,1"),
21
+ ] = None,
22
+ max_picks: Annotated[
23
+ list[(str, int)],
24
+ typer.Option("--max-picks", "-maxp", help="The items to be picked maximum e.g. -maxp b,1"),
25
+ ] = None,
26
+ pick: Annotated[
27
+ int,
28
+ typer.Option(
29
+ "--pick", "-p", "-n", help="The number of items to be picked, e.g. -p 3"
30
+ ),
31
+ ] = None,
32
+ out: Annotated[
33
+ str,
34
+ typer.Option(
35
+ "--out", "-o", help="Write results to a filepath, e.g. --out output.txt"
36
+ ),
37
+ ] = None,
38
+ ):
39
+
40
+ item_list = items.split(_SEPARATOR)
41
+
42
+ min_pick_list = [(item[0], int(item[1])) for item in [pick.split(_SEPARATOR) for pick in min_picks]]
43
+ max_pick_list = [(item[0], int(item[1])) for item in [pick.split(_SEPARATOR) for pick in max_picks]]
44
+
45
+ results = _core_generate(items=item_list, pick=pick, min_picks=min_pick_list, max_picks=max_pick_list)
46
+
47
+ if out:
48
+ with open(out, "w") as f:
49
+ f.write("\n".join([_SEPARATOR.join(perm) for perm in results]))
50
+ else:
51
+ for perm in results:
52
+ print(perm)
53
+
54
+ return results
55
+
56
+
57
+ if __name__ == "__main__":
58
+ app()
@@ -0,0 +1,76 @@
1
+ from collections import Counter
2
+ from itertools import combinations
3
+
4
+
5
+ def getDiff(list1, list2):
6
+ return [item1 for item1 in list1 if item1 not in list2]
7
+
8
+ def does_exceed_max(combination_from_remaining, max_picked_list):
9
+ print(combination_from_remaining, max_picked_list)
10
+ combination_from_remaining_freq = Counter(combination_from_remaining)
11
+ max_pick_list_freq = Counter(max_picked_list)
12
+
13
+ for k, v in combination_from_remaining_freq.items():
14
+ if k in max_pick_list_freq and v > max_pick_list_freq[k]:
15
+ return True
16
+
17
+ return False
18
+
19
+
20
+ def get_result(rest_item_list, min_picked_list, max_picked_list, rest_n):
21
+
22
+ result = []
23
+ visited = {}
24
+
25
+ for combination_from_remaining in combinations(rest_item_list, rest_n):
26
+ if str(combination_from_remaining) not in visited and not does_exceed_max(combination_from_remaining, max_picked_list):
27
+ visited[str(combination_from_remaining)] = True
28
+ new_combination = min_picked_list[:]
29
+ new_combination.extend(combination_from_remaining)
30
+ new_combination.sort()
31
+ result.append(new_combination)
32
+
33
+ result.sort()
34
+
35
+ return result
36
+
37
+
38
+ def extract_ignored_items(items):
39
+ ignoreList = []
40
+
41
+ if items:
42
+ for item in items:
43
+ if type(item) is tuple:
44
+ if not item[1]:
45
+ ignoreList.append(item[0])
46
+
47
+ return ignoreList
48
+
49
+
50
+ def extract_items(items):
51
+ item_list = []
52
+
53
+ if items:
54
+ for item in items:
55
+ if type(item) is str:
56
+ item_list.append(item)
57
+ elif type(item) is tuple:
58
+ item_list.extend([item[0]] * item[1])
59
+
60
+ return item_list
61
+
62
+
63
+ def generate(items, pick=None, min_picks=None, max_picks=None):
64
+
65
+ item_list = extract_items(items)
66
+ min_picked_list = extract_items(min_picks)
67
+ max_picked_list = extract_items(max_picks)
68
+ pick_ignore_list = extract_ignored_items(max_picks)
69
+
70
+ rest_pick = pick - len(min_picked_list)
71
+
72
+ rest_item_list = getDiff(getDiff(item_list, min_picked_list), pick_ignore_list)
73
+
74
+ result = get_result(rest_item_list, min_picked_list, max_picked_list, rest_pick)
75
+
76
+ return result
@@ -0,0 +1,113 @@
1
+ Metadata-Version: 2.4
2
+ Name: custom-combination
3
+ Version: 0.0.1
4
+ Summary: A custom combination generator
5
+ Project-URL: Homepage, https://github.com/yilmazhasan/custom_combination
6
+ Project-URL: Issues, https://github.com/yilmazhasan/custom_combination/issues
7
+ Author-email: Hasan YILMAZ <ylmzhsn@proton.me>, Reego Software <reego.software@gmail.com>
8
+ License-File: LICENCE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.8
13
+ Requires-Dist: typer
14
+ Description-Content-Type: text/markdown
15
+
16
+ # Custom Combination
17
+
18
+ > Version 0.0.1
19
+
20
+ Python implementation for Custom Combination.
21
+
22
+ ## Usage
23
+
24
+ ### Programmatic usage
25
+
26
+ ```py
27
+ from custom_combination import CustomCombination
28
+
29
+ CustomCombination.generate(
30
+ items=[("a", 1), ("b", 2), ("c", 3), "d"],
31
+ pick=3,
32
+ min_picks=[("b", 1), ("a", 0)],
33
+ max_picks=[("a", 0)])
34
+ ```
35
+
36
+ ### CLI usage
37
+
38
+ ```sh
39
+ custom_combination --items "a,b,b,c,c,c,d" -pick 3 -minp "b,1" -maxp "a,0"
40
+ ```
41
+
42
+ Output:
43
+
44
+ ```sh
45
+ ['b', 'c', 'd']
46
+ ['b', 'c', 'c']
47
+ ```
48
+
49
+ ## Set up locally for development
50
+
51
+ 1. Clone the repository
52
+
53
+ > `git clone git@github.com:yilmazhasan/custom_combination.git`
54
+
55
+ 2. Create and activate the virtural environment
56
+
57
+ > `python -m venv .venv`
58
+
59
+ > `source .venv/bin/activate`
60
+
61
+ 3. Install requirements:
62
+
63
+ > `pip install -r requirements.txt`
64
+
65
+ 4. Run the app:
66
+
67
+ > `python src/custom_combination/app.py --items a,b,c,d -pick a,0 -minp b,1 -max a,0`
68
+
69
+ 5. Test the app:
70
+
71
+ > `pytest`
72
+
73
+ ## Args Explained
74
+
75
+ ```sh
76
+ --items "a,b,b,c,c,c,d" -pick 3 -min-picks b,1 -max-picks a,0
77
+ ```
78
+
79
+ or shortly:
80
+
81
+ ```sh
82
+ --items "a,b,b,c,c,c,d" -p 3 -minp b,1 -maxp a,0
83
+ ```
84
+
85
+ This indicates that:
86
+
87
+ - We have four items in the set `["a", "b", "c", "d"]` but with different frequencies.
88
+ - Thus we have the list as `["a", "b", "b", "c", "c", "c", "d"]`
89
+
90
+ - We want to generate combinations that have **3** items, which `-pick 3` specifies.
91
+
92
+ - We want to pick at least:
93
+ - **1** occurrences of `"b"`
94
+
95
+ - We want to pick at max:
96
+ - **0** occurrences of `"a"`
97
+
98
+ This indicates the list to generate from is:
99
+
100
+ `["b", "b", "c", "c", "c", "d"]` out of
101
+
102
+ `["a", "b", "b", "c", "c", "c", "d"]` as `"a"` is excluded.
103
+
104
+ Since `"b"` is to be included once, we need to select 2 more items from the list `["c", "c", "c", "d"]`, so we can select `["c", "c"]` or `["c", "d"]`
105
+
106
+ Expected output:
107
+
108
+ ```sh
109
+ [
110
+ ["b", "c", "c"],
111
+ ["b", "c", "d"]
112
+ ]
113
+ ```
@@ -0,0 +1,8 @@
1
+ custom_combination/__init__.py,sha256=0oSFbqAatnqE3QiG2w3ohRtGmhMQ2co2MVCR85g9Gls,329
2
+ custom_combination/cli.py,sha256=uMfoUmfE5mYEA3NXWHr4wEiIL_sj5A0uSGBO4eV4mrs,1574
3
+ custom_combination/core.py,sha256=JRdnC9BtCBsWxVLqIW6UCh0HHYyneLmwxUlXaqA77nA,2176
4
+ custom_combination-0.0.1.dist-info/METADATA,sha256=A1_MN0O3MT-f80q4hmdX7YVQj3yZuOvJxWGeEAiQ8ps,2442
5
+ custom_combination-0.0.1.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
6
+ custom_combination-0.0.1.dist-info/entry_points.txt,sha256=gPZRcLbjQpH8BTtR3aKoDEmZeYfhHj4n-Wr8qDa_I8Y,66
7
+ custom_combination-0.0.1.dist-info/licenses/LICENCE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
8
+ custom_combination-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ custom_combination = custom_combination.cli:app
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.