|
9 | 9 | __all__ = [ |
10 | 10 | "map_value_product", |
11 | 11 | "map_product", |
| 12 | + "map_sorted", |
12 | 13 | "map_permutations", |
13 | 14 | "map_combinations", |
14 | 15 | "map_combinations_with_replacement", |
15 | 16 | ] |
16 | 17 |
|
17 | 18 | import itertools |
18 | 19 | import logging |
19 | | -from typing import Any, Mapping, Tuple |
| 20 | +from typing import Any, Generator, Mapping, Optional, Tuple |
20 | 21 |
|
21 | 22 | logger = logging.getLogger(__name__) |
22 | 23 |
|
23 | 24 |
|
24 | | -def map_value_product(dicts: Mapping) -> Any: |
25 | | - """description""" |
26 | | - return (dict(zip(dicts, x)) for x in itertools.product(*dicts.values())) |
| 25 | +def map_value_product(mappings: Optional[Mapping]) -> Optional[Generator[dict, None, None]]: |
| 26 | + """ |
| 27 | +
|
| 28 | + :param mappings: |
| 29 | + :return: |
| 30 | + """ |
| 31 | + if mappings is None: |
| 32 | + return None |
| 33 | + |
| 34 | + return (dict(zip(mappings, x)) for x in itertools.product(*mappings.values())) |
| 35 | + |
| 36 | + |
| 37 | +def map_reversed(mapping: Optional[Mapping]) -> Optional[dict]: |
| 38 | + """ |
| 39 | +
|
| 40 | + :param mapping: |
| 41 | + :return: |
| 42 | + """ |
| 43 | + if mapping is None: |
| 44 | + return None |
| 45 | + |
| 46 | + return dict(reversed(list(mapping.items()))) |
| 47 | + |
| 48 | + |
| 49 | +def map_sorted(mapping: Optional[Mapping], **kwargs) -> Optional[dict]: |
| 50 | + """ |
27 | 51 |
|
| 52 | + :param mapping: |
| 53 | + :param kwargs: |
| 54 | + :return: |
| 55 | + """ |
| 56 | + if mapping is None: |
| 57 | + return None |
28 | 58 |
|
29 | | -def map_product(dicts: Mapping, repeat: int = 2) -> Any: |
| 59 | + return dict(sorted(mapping.items(), **kwargs)) |
| 60 | + |
| 61 | + |
| 62 | +def map_product(mapping: Mapping, repeat: int = 2) -> Any: |
30 | 63 | """description""" |
31 | 64 | yield from zip( |
32 | | - itertools.product(dicts.keys(), repeat=repeat), |
33 | | - itertools.product(dicts.values(), repeat=repeat), |
| 65 | + itertools.product(mapping.keys(), repeat=repeat), |
| 66 | + itertools.product(mapping.values(), repeat=repeat), |
34 | 67 | ) |
35 | 68 |
|
36 | 69 |
|
37 | | -def map_permutations(dicts: Mapping, repeat: int = 2) -> Tuple: |
38 | | - """description""" |
| 70 | +def map_permutations(mapping: Mapping, repeat: int = 2) -> Generator[Tuple[Any, ...], None, None]: |
| 71 | + """ |
| 72 | +
|
| 73 | + :param mapping: |
| 74 | + :param repeat: |
| 75 | + :return: |
| 76 | + """ |
39 | 77 | yield from zip( |
40 | | - itertools.permutations(dicts.keys(), repeat), |
41 | | - itertools.permutations(dicts.values(), repeat), |
| 78 | + itertools.permutations(mapping.keys(), repeat), |
| 79 | + itertools.permutations(mapping.values(), repeat), |
42 | 80 | ) |
43 | 81 |
|
44 | 82 |
|
45 | | -def map_combinations(dicts: Mapping, repeat: int = 2) -> Tuple: |
46 | | - """description""" |
| 83 | +def map_combinations(mapping: Mapping, repeat: int = 2) -> Generator[Tuple[Any, ...], None, None]: |
| 84 | + """ |
| 85 | +
|
| 86 | + :param mapping: |
| 87 | + :param repeat: |
| 88 | + :return: |
| 89 | + """ |
47 | 90 | yield from zip( |
48 | | - itertools.combinations(dicts.keys(), repeat), |
49 | | - itertools.combinations(dicts.values(), repeat), |
| 91 | + itertools.combinations(mapping.keys(), repeat), |
| 92 | + itertools.combinations(mapping.values(), repeat), |
50 | 93 | ) |
51 | 94 |
|
52 | 95 |
|
53 | | -def map_combinations_with_replacement(dicts: Mapping, repeat: int = 2) -> Tuple: |
54 | | - """description""" |
| 96 | +def map_combinations_with_replacement( |
| 97 | + mappings: Mapping, repeat: int = 2 |
| 98 | +) -> Generator[Tuple[Any, ...], None, None]: |
| 99 | + """ |
| 100 | +
|
| 101 | + :param mappings: |
| 102 | + :param repeat: |
| 103 | + :return: |
| 104 | + """ |
| 105 | + if mappings is None: |
| 106 | + return None |
| 107 | + |
55 | 108 | yield from zip( |
56 | | - itertools.combinations_with_replacement(dicts.keys(), repeat), |
57 | | - itertools.combinations_with_replacement(dicts.values(), repeat), |
| 109 | + itertools.combinations_with_replacement(mappings.keys(), repeat), |
| 110 | + itertools.combinations_with_replacement(mappings.values(), repeat), |
58 | 111 | ) |
59 | 112 |
|
60 | 113 |
|
|
0 commit comments