Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / proto_table_stream.py
Created January 6, 2018 22:33
Rough draft fork of bloop.Stream that inspects each record to decide which model to use. Untested.
from bloop.models import unpack_from_dynamodb
from bloop.signals import object_loaded
from bloop.stream.coordinator import Coordinator
class TableStream:
def __init__(self, *, stream_arn, engine, model_selector):
"""
model_selector is a function that takes a record
and returns a model class. for example:
@numberoverzero
numberoverzero / eval_gist.py
Last active November 19, 2017 03:35
A slightly safer eval: provide a constrained set of variables and whitelist attribute names for lookups
"""
Support evaluation of a very limited subset of operations and literals.
EvalContext supports a subset of operations common across many languages,
while PythonEvalContext adds on Sets, Tuples, Slices, and the literals
True, False, and None.
.. code-block:: pycon
>>> from eval_gist import EvalContext
@numberoverzero
numberoverzero / 01_models.py
Last active September 24, 2017 02:34
Example model/controller and setup code; controller sets default values for key expiration and key id
import pendulum
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
from bloop import UUID, Binary, Column
from bloop.ext.pendulum import DateTime
@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: