Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / update_with_defaults.py
Last active September 7, 2017 09:39
sample impl of tracking marked columns and also columns with default values
def get_marked(obj):
"""Returns the set of marked columns for an object"""
return set(_obj_tracking[obj]["marked"])
def get_marked_or_default(obj):
marked = get_marked(obj)
for column in obj.Meta.columns:
if column.default is not missing:
marked.add(column)
@numberoverzero
numberoverzero / _trace.py
Created August 3, 2017 16:35
add TRACE level to python logging
import logging
_trace_installed = False
def install_trace_logger():
global _trace_installed
if _trace_installed:
return
level = logging.TRACE = logging.DEBUG - 5
@numberoverzero
numberoverzero / run_scheduled.py
Last active February 16, 2020 07:58
00.async.periodic.rst
import asyncio
import functools
import json
import secrets
import aiohttp
from concurrent.futures import ALL_COMPLETED
@numberoverzero
numberoverzero / 00_views.py
Last active July 3, 2017 21:41
Another way to attach event handlers to a bottom Client without hardcoding the Client instance
import bottom
from collections import defaultdict
_handlers = defaultdict(list)
def attach_handlers(client: bottom.Client) -> bottom.Client:
for event, funcs in _handlers.items():
for func_builder in funcs:
client.on(event, func=func_builder(client))
@numberoverzero
numberoverzero / 00_nested_handlers.py
Created April 27, 2017 22:44
Nested handlers without recursion
from typing import Any, Callable, Iterable
def noop(next_handler: Callable, *args: Any, **kwargs: Any) -> None:
pass
def process(handlers: Iterable[Callable], *args: Any, **kwargs: Any) -> None:
next_args = args
next_kwargs = kwargs
@numberoverzero
numberoverzero / bloop_lambda_replication.py
Last active September 18, 2017 04:52
Example of a possible Bloop extension to do cross-region replication with AWS Lambda in 30 lines
from bloop import Engine
from bloop.ext._lambda import unpack
from boto3 import Session
from my_models_script import MyModel
primary = Engine()
session = Session(region_name="us-east-1")
backup = Engine(
dynamodb=session.client("dynamodb"),
@numberoverzero
numberoverzero / pep487.py
Last active March 24, 2017 13:27
PEP 487 is insane, holy fuck this is awesome
from typing import Any, Dict, List, NamedTuple, Optional, Tuple, TypeVar, Generic, Type
T = TypeVar("T")
class Field(Generic[T]):
key: str
t: Type[T]
readonly: bool
def __init__(self, t: Type[T], readonly: bool=True) -> None:
@numberoverzero
numberoverzero / lazy.py
Last active August 29, 2019 13:31
Python deferred instantiation
import inspect
from typing import Callable, TypeDef
T = TypeDef("T")
def defer(func: Callable[[], T]) -> T:
sig = inspect.signature(func)
base = sig.return_type
double_entry = False
@numberoverzero
numberoverzero / LICENSE.txt
Last active December 8, 2016 06:22
micro ajax 'library' wrapping XMLHttpRequest in a Promise. 315 bytes before compression.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2016 Joe Cross
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@numberoverzero
numberoverzero / 00_list.py
Created October 31, 2016 18:12
temporary List hack for Bloop 0.9.12
from bloop import List
# Uses inner type's serialization for single values
# ie. List(String).contains("one value")
class List(List):
def _dump(self, value, *, context=None, **kwargs):
if value is None:
return value
if not isinstance(value, list):