Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,101 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[run] | ||
branch = True | ||
source = | ||
pytoolz/ | ||
ftoolz/ | ||
omit = | ||
|
||
[report] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ __pycache__/ | |
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
sudo: required | ||
dist: xenial | ||
language: python | ||
python: | ||
- "3.6" | ||
- "3.7" | ||
|
||
install: | ||
- make install | ||
- pip install coveralls | ||
|
||
script: | ||
- make release-check | ||
|
||
after_success: | ||
- coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, TypeVar | ||
|
||
from ftoolz.typing import Map | ||
|
||
K = TypeVar('K') | ||
V = TypeVar('V') | ||
|
||
|
||
def swap(d: Map[K, Any], key1: K, key2: K) -> Map[K, Any]: | ||
""" | ||
Swap arbitrary values for given keys creating new mapping. | ||
>>> swap({'k1': [1, 2, 3], 'k2': {1, 2, 3}}, 'k1', 'k2') | ||
{'k1': {1, 2, 3}, 'k2': [1, 2, 3]} | ||
Original mapping is returned if at least one key does not exist in `d`. | ||
>>> swap({'k1': 1}, 'k1', 'k2') | ||
{'k1': 1} | ||
>>> swap({'k2': 2}, 'k1', 'k2') | ||
{'k2': 2} | ||
""" | ||
return swap_values(d, key1, key2) | ||
|
||
|
||
def swap_values(d: Map[K, V], key1: K, key2: K) -> Map[K, V]: | ||
""" | ||
Swap values for given keys creating new mapping. | ||
>>> swap_values({'k1': 1, 'k2': 2}, 'k1', 'k2') | ||
{'k1': 2, 'k2': 1} | ||
Original mapping is returned if at least one key does not exist in `d`. | ||
>>> swap_values({'k1': 1}, 'k1', 'k2') | ||
{'k1': 1} | ||
>>> swap_values({'k2': 2}, 'k1', 'k2') | ||
{'k2': 2} | ||
""" | ||
return {**d, key1: d[key2], key2: d[key1]} \ | ||
if key1 in d and key2 in d \ | ||
else d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Any, Callable, Tuple, Type, TypeVar, Union | ||
|
||
# Invariant | ||
A = TypeVar('A') | ||
B = TypeVar('B') | ||
C = TypeVar('C') | ||
D = TypeVar('D') | ||
|
||
# Contravariant | ||
A_in = TypeVar('A_in', contravariant=True) | ||
B_in = TypeVar('B_in', contravariant=True) | ||
C_in = TypeVar('C_in', contravariant=True) | ||
D_in = TypeVar('D_in', contravariant=True) | ||
|
||
# Covariant | ||
A_out = TypeVar('A_out', covariant=True) | ||
B_out = TypeVar('B_out', covariant=True) | ||
C_out = TypeVar('C_out', covariant=True) | ||
D_out = TypeVar('D_out', covariant=True) | ||
|
||
|
||
def try_except( | ||
e: Union[Type[Exception], Tuple[Type[Exception], ...]], | ||
f: Callable[..., A], | ||
g: Callable[..., A], | ||
*args: Any, | ||
**kwargs: Any | ||
) -> A: | ||
""" | ||
Call `f(args, kwargs)` and on exception `e` fallback to `g(args, kwargs)`. | ||
>>> try_except(ValueError, int, lambda s: s.upper(), '1') | ||
1 | ||
>>> try_except(ValueError, int, lambda s: s.upper(), 'a') | ||
'A' | ||
One can pass both args and kwargs to `try_except`. | ||
>>> def f(x: int, y: str): | ||
... return x + int(y) | ||
>>> def g(x: int, y: str): | ||
... return f'error: {x} + {y}' | ||
>>> try_except(ValueError, f, g, 1, y='2') | ||
3 | ||
>>> try_except(ValueError, f, g, 1, y='x') | ||
'error: 1 + x' | ||
Errors that are not mentioned in `e` are propagated to outer scope. | ||
>>> try_except(ValueError, lambda d: d['k'], str, {'a': 1}) | ||
Traceback (most recent call last): | ||
... | ||
KeyError: 'k' | ||
""" | ||
# noinspection PyBroadException | ||
try: | ||
return f(*args, **kwargs) | ||
except e: | ||
return g(*args, **kwargs) |
Oops, something went wrong.