Skip to content

Instantly share code, notes, and snippets.

from typing import Dict, Generic, TypeVar
import pytest
from adt import Case, adt
T = TypeVar("T")
@adt
class Option(Generic[T]):
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"]
@gidgid
gidgid / partition_example.py
Last active April 19, 2023 11:18
shows how to use the more_itertools partition function
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
@gidgid
gidgid / flatten_manual.py
Last active January 30, 2021 12:52
shows how we can manually run flatten
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
@gidgid
gidgid / flatten.py
Created January 30, 2021 12:48
we use more_itertools flatten to do a much better job
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
@gidgid
gidgid / map_except_example.py
Last active February 1, 2021 19:47
map_except in more_itertools example
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
@gidgid
gidgid / traditional_first.py
Created February 2, 2021 12:46
shows how to get the first element of a list and add it
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
@gidgid
gidgid / chunked_example.py
Created February 4, 2021 12:33
using more_itertools chunked to save batches
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
@gidgid
gidgid / product_example.py
Created February 4, 2021 13:06
shows how to use product from itertools to avoid nested loops
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
@gidgid
gidgid / always_itertable_examples.py
Last active February 5, 2021 11:46
Examples of how more_itertools always_iterable works
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)) # => []