Skip to content

Instantly share code, notes, and snippets.

# fragments from evaluating some toolkit classes to build shared models as used
# in DAT401: Advanced Design Patterns for DynamoDB
# https://www.youtube.com/watch?v=HaEPXoXVf2k
import functools
from typing import Type
from bloop.conditions import Condition, iter_columns
from bloop.models import BaseModel, Column, IMeta, bind_column
# 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_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/[email protected]/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"