Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / 00_base.py
Last active July 26, 2019 17:23
support integral base string representations, especially for lexicographical sorting
def in_base(n: int, alphabet: str) -> str:
b = len(alphabet)
s = []
while n > 0:
d = n % b
s.append(alphabet[d])
n //= b
return "".join(reversed(s))
@numberoverzero
numberoverzero / 00_multimodel.py
Last active January 27, 2019 15:13
Helpers to partition a single table as demonstrated in https://youtu.be/HaEPXoXVf2k
import functools
from typing import Type
from bloop.conditions import Condition, iter_columns
from bloop.models import BaseModel, Column, IMeta, bind_column
def default_hk_query(cls, engine, key=None, **kwargs):
key = Condition() | key
hk = cls.Meta.hash_key
if hk not in set(iter_columns(key)):
@numberoverzero
numberoverzero / tx_demo.py
Last active January 9, 2019 09:51
demo of using bloop's transaction class
# Three-table solution to https://stackoverflow.com/q/35825034
# Generally this is a terrible idea, but it demos transactions (see the `new_user` method) and
# provides guaranteed uniqueness over a triplet of attributes.
# You could instead use optimistic writes (but be ready to clean up dangling records!), such as:
# (1) write Username if pk not exist, else fail
# (2) write Email if pk not exist, else roll back (1) and fail
# (3) write User if pk not exist, else roll back (1), (2) and fail
# (4) update (1), (2), (3) with attributes from each other so they are no longer dangling
@numberoverzero
numberoverzero / factorio_rcon_notes.txt
Created August 11, 2018 07:39
notes from benchmarking factorio's rcon capabilities
tool: rcon-cli https://github.com/itzg/rcon-cli
control.lua:
function Bench(x)
rcon.print(x+3)
end
commands.txt:
/silent-command Bench(2)
[9998 more copies]
@numberoverzero
numberoverzero / index.html
Created June 20, 2018 01:42
using msgpack-lite and base64-js together
<!doctype html><meta charset="utf-8"/>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/msgpack-lite/0.1.26/msgpack.min.js"
integrity="sha256-xnDLLYKxKFwLEmQK1SkZ9I7IwmjdeURGtXUk/0WnTRo="
crossorigin="anonymous"></script>
<script
src="https://cdn.jsdelivr.net/npm/base64-js@1.3.0/base64js.min.js"
integrity="sha256-6Nq3ERANZFF5/7q3A7vSDHKttstfHieUf5+eFo9W4I0="
crossorigin="anonymous"></script>
<script>
@numberoverzero
numberoverzero / sc2tools.py
Created March 29, 2018 01:13
bank file loading
import hashlib
import untangle
def load(filename):
with open(filename) as f:
return untangle.parse(f.read())
def canonicalize_section(section):
@numberoverzero
numberoverzero / windows-binding.json
Created March 14, 2018 21:40
Home/End bindings for Karabiner-Elements
{
"title": "Windows Keys",
"rules": [
{
"description": "Windows-style Home/End for non-iTerm applications",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "end"
@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