This file contains hidden or 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
from typing import Dict, Generic, TypeVar | |
import pytest | |
from adt import Case, adt | |
T = TypeVar("T") | |
@adt | |
class Option(Generic[T]): |
This file contains hidden or 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
from adt import Case, adt | |
@adt | |
class Expression: | |
LITERAL: Case[float] | |
ADD: Case["Expression", "Expression"] | |
SUBTRACT: Case["Expression", "Expression"] | |
MULTIPLY: Case["Expression", "Expression"] | |
DIVIDE: Case["Expression", "Expression"] |
This file contains hidden or 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
from typing import Dict, Iterable, Set, Tuple | |
from more_itertools import partition | |
def process( | |
names: Iterable[str], whitelisted_names: Set[str], name_to_email: Dict[str, str] | |
) -> Tuple[Iterable[str], Iterable[str]]: | |
refused_names, approved_names = partition( | |
lambda name: name in whitelisted_names, names |
This file contains hidden or 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
from typing import Dict, Set | |
def flatten_multivalues(key_to_values: Dict[str, Set[int]]) -> Set[int]: | |
all_values = set() | |
for values in key_to_values.values(): | |
all_values.update(values) | |
return all_values | |
This file contains hidden or 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
from typing import Dict, Set | |
from more_itertools import flatten | |
def flatten_multivalues(key_to_values: Dict[str, Set[int]]) -> Set[int]: | |
return set(flatten(key_to_values.values())) | |
def test_flattens_multivalue_dicts(): | |
# shamelessly taken from: https://www.elastic.co/blog/found-elasticsearch-from-the-bottom-up |
This file contains hidden or 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
from typing import Dict, Iterable, Set, Tuple | |
from more_itertools import map_except | |
def process( | |
names: Iterable[str], whitelisted_names: Set[str], name_to_email: Dict[str, str] | |
) -> Iterable[str]: | |
whitelisted_name_to_email = { | |
name: email for name, email in name_to_email.items() if name in whitelisted_names |
This file contains hidden or 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
from typing import List | |
def increment_first_by(values: List[int], add_me: int) -> int: | |
return values[0] + add_me | |
def test_increment_first(): | |
assert increment_first_by([1, 2, 3], 5) == 5 + 1 |
This file contains hidden or 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
from typing import List | |
from more_itertools import chunked | |
def batch_insert(col, items: List[dict], batch_size: int): | |
batches = chunked(items, batch_size) # 1 | |
for batch in batches: | |
col.insert_many(batch) # 2 |
This file contains hidden or 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
from typing import Set | |
from dataclasses import dataclass | |
from itertools import product | |
@dataclass(frozen=True, eq=True) | |
class UserOption: # 1 | |
nickname: str | |
hobby: str | |
email: str |
This file contains hidden or 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
from more_itertools import always_iterable | |
always_iterable([1,3, 5]) # => <list_iterator at 0x107deca30> | |
list(always_iterable([1,3, 5])) # => [1, 3, 5] | |
list(always_iterable({1,3, 5})) # => [1, 3, 5] (works on all iterables) | |
list(always_iterable(42)) # => [42] | |
list(always_iterable([])) # => [] | |
list(always_iterable(None)) # => [] |