Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / 01 sample_api.py
Last active March 31, 2020 20:35
accordian api change
Namespace
.signal(name) -> Signal
Signal
.connect(async fn) -> async fn
.send(*a, **kw) -> Set[Task]
async .join(*a, **kw) -> List[Any]
_global = Namespace()
signal = _global.Signal
@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 / 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 / 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))
# fragments from evaluating some toolkit classes to succinctly build common queries in bloop
class QueryBuilder:
def __init__(self, engine):
self.engine = engine
self.index_cache = {}
def by_key(self, key_condition, *, index=None, **kwargs):
if isinstance(key_condition, AndCondition):
model = key_condition.values[0].model
@numberoverzero
numberoverzero / compile_stockfish.sh
Created February 11, 2019 05:26
compile stockfish from github on RHEL into /opt/stockfish
#!/usr/bin/env bash
# assumes bmi2 arch: "x86 64-bit with pext support"
# compiled binary ends in /opt/stockfish/$MAJOR_VERSION/bin/stockfish
set -e
set -x
sudo yum update -y
sudo yum install -y git gcc gcc-c++
@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>