Skip to content

Instantly share code, notes, and snippets.

@gidgid
gidgid / singledispatch_example.py
Created February 24, 2021 19:39
shows how we can overload methods via singledispatch
from functools import singledispatch
@singledispatch # 1
def to_json(inp): # 1
"""implementations in dispatched functions"""
@to_json.register # 2
def as_json_str(input_str: str) -> str: # 2
@gidgid
gidgid / pydantic_literal_recap.py
Created February 21, 2021 20:23
shows a short recap of how to read different values by an env variables with Pydantic
from typing import Union
from pydantic import BaseSettings, parse_obj_as
from typing_extensions import Literal
class LocalContext(BaseSettings):
env: Literal["local"] # 1
mongo_url: str
@gidgid
gidgid / pydantic_with_singledispatch.py
Last active February 27, 2021 20:37
mixing pydantic and single dispatch
import os
from typing import Union
from pymongo import MongoClient, ReadPreference
import pytest
from functools import singledispatch
from pydantic import BaseSettings, parse_obj_as
from typing_extensions import Literal
@gidgid
gidgid / summarizing_orders_with_map_reduce.py
Created February 6, 2021 12:50
shows how to use map_reduce to summarize specific properties on values
from dataclasses import dataclass
from typing import Iterable, Tuple, Dict, Set
from more_itertools import map_reduce
@dataclass(frozen=True, eq=True)
class Order: # 1
order_id: str # 1
price: int # 1
@gidgid
gidgid / grouping_edges_map_reduce.py
Created February 6, 2021 12:38
shows how to create a multivalue dict from tuples
from typing import Iterable, Tuple, Dict, Set
from more_itertools import map_reduce
def group_edges(edges: Iterable[Tuple[str, str]]) -> Dict[str, Set[str]]:
return map_reduce(
edges,
keyfunc=lambda edge: edge[0], # 1
valuefunc=lambda edge: edge[1], # 2
reducefunc=set, # 3
@gidgid
gidgid / one_networkx_example.py
Created February 5, 2021 16:32
shows how to use more_itertools one to extract a single root
import pytest
from more_itertools import one
import networkx as nx
class GraphGenerationError(Exception):
pass
def find_root(graph) -> str:
@gidgid
gidgid / using_always_iterable.py
Last active February 5, 2021 12:31
shows how we can use always_iterable to write less code
from functools import reduce
from operator import add
import pytest
from more_itertools import always_iterable
def safer_sum(inputs) -> int:
iterable_inputs = always_iterable(inputs) # 1
return reduce(add, iterable_inputs, 0) # 1
@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)) # => []
@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 / 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