11import re
22from typing import List , Iterable , Tuple , Callable , TypeVar , Dict
33
4- T = TypeVar ('T' )
5- S = TypeVar ('S' )
6- U = TypeVar ('U' )
7- V = TypeVar ('V' )
4+ T = TypeVar ("T" )
5+ S = TypeVar ("S" )
6+ U = TypeVar ("U" )
7+ V = TypeVar ("V" )
88
99
1010def lmap (fn : Callable [[S ], T ], iterable : Iterable [S ]) -> List [T ]:
@@ -23,17 +23,19 @@ def tfilter(fn: Callable[[S], bool], iterable: Iterable[S]) -> Tuple[S, ...]:
2323 return tuple (filter (fn , iterable ))
2424
2525
26- def lpartition (fn : Callable [[S ], bool ], iterable : Iterable [S ]) -> \
27- Tuple [List [S ], List [S ]]:
26+ def lpartition (
27+ fn : Callable [[S ], bool ], iterable : Iterable [S ]
28+ ) -> Tuple [List [S ], List [S ]]:
2829 """Partition list into two with a boolean function. Not particularly
2930 efficient because of the double pass, but fine for small lists."""
3031 true_list = lfilter (lambda x : fn (x ), iterable )
3132 false_list = lfilter (lambda x : not fn (x ), iterable )
3233 return true_list , false_list
3334
3435
35- def tpartition (fn : Callable [[S ], bool ], iterable : Iterable [S ]) -> \
36- Tuple [Tuple [S , ...], Tuple [S , ...]]:
36+ def tpartition (
37+ fn : Callable [[S ], bool ], iterable : Iterable [S ]
38+ ) -> Tuple [Tuple [S , ...], Tuple [S , ...]]:
3739 """Partition tuple into two with a boolean function. Not particularly
3840 efficient because of the double pass, but fine for small tuples."""
3941 true_list = tfilter (lambda x : fn (x ), iterable )
@@ -49,17 +51,15 @@ def dvmap(fn: Callable[[U], T], dictionary: Dict[S, U]) -> Dict[S, T]:
4951 return {k : fn (v ) for k , v in dictionary .items ()}
5052
5153
52- def dmap (fn : Callable [[S , T ], Tuple [U , V ]], dictionary : Dict [S , T ]) \
53- -> Dict [U , V ]:
54+ def dmap (fn : Callable [[S , T ], Tuple [U , V ]], dictionary : Dict [S , T ]) -> Dict [U , V ]:
5455 return {k : v for k , v in map (fn , dictionary .keys (), dictionary .values ())}
5556
5657
5758def dkfilter (fn : Callable [[S ], bool ], dictionary : Dict [S , T ]) -> Dict [S , T ]:
5859 return {k : dictionary [k ] for k in lfilter (fn , dictionary .keys ())}
5960
6061
61- def dfilter (fn : Callable [[Tuple [S , T ]], bool ], dictionary : Dict [S , T ]) \
62- -> Dict [S , T ]:
62+ def dfilter (fn : Callable [[Tuple [S , T ]], bool ], dictionary : Dict [S , T ]) -> Dict [S , T ]:
6363 return {k : v for k , v in tfilter (fn , dictionary .items ())}
6464
6565
@@ -89,12 +89,12 @@ def flatten(t):
8989 return [item for sublist in t for item in sublist ]
9090
9191
92- def partition_n (n : int , fn : Callable [[T ], int ], listlike : Iterable [T ]) \
93- -> List [List [T ]]:
92+ def partition_n (n : int , fn : Callable [[T ], int ], listlike : Iterable [T ]) -> List [List [T ]]:
9493 return_obj : List [List [T ]] = [[] for _ in range (n )]
9594 for elem in listlike :
9695 k = fn (elem )
97- assert 0 <= k < n , "partition function needs to return an integer " \
98- "in the interval [0, {n})."
96+ assert (
97+ 0 <= k < n
98+ ), f"partition function needs to return an integer in the interval [0, { n } )."
9999 return_obj [k ].append (elem )
100100 return return_obj
0 commit comments