Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / 00.bloop_multitable.py
Created Nov 10, 2021
Small class to map multiple Bloop models onto a shared table
View 00.bloop_multitable.py
# bloop 3.0.0
import copy
from typing import Union
from bloop import BaseModel, Column
from bloop.models import subclassof, instanceof
from bloop.types import Type
__all__ = ["Mapper"]
@numberoverzero
numberoverzero / run_forked.rs
Created Nov 6, 2021
execute a function in a detached process and exit
View run_forked.rs
// fork = "0.1"
use fork::{self, Fork};
use std::process;
/// no error handling, no logging, just one shot to run in a forked process
fn run_forked<R>(f: fn() -> R) -> bool
{
match fork::fork() {
Ok(Fork::Parent(_)) => {
// we're in the parent process, must have forked successfully
@numberoverzero
numberoverzero / 00. costFactorForDuration.ts
Created Mar 15, 2021
compute an optimal bcrypt costfactor for a target hash time on the current machine
View 00. costFactorForDuration.ts
import assert from 'assert'
import bcrypt from 'bcrypt'
import crypto from 'crypto'
import { PerformanceObserver, performance } from 'perf_hooks'
const MINIMUM_BCRYPT_COST_FACTOR = 10
const RECOMMENDED_BCRYPT_COST_FACTOR = costFactorForDuration(250)
/**
* Calculate a cost factor based on a target duration (in milliseconds) for hashing.
@numberoverzero
numberoverzero / 00. 32bit key
Last active May 23, 2020
Monogame RenderQueue designs
View 00. 32bit key
MSB LSB
00 06 14 15 26 32
┣━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━╋━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━┫
┃pass 64┃layer 256┃ 0 ┃effect 512┃texture 256┃ BATCHED MATERIAL
┣━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━╋━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┫
┃pass 64┃layer 256┃ 1 ┃data ~2 bytes┃ DYNAMIC MATERIAL
┣━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
00 06 14 15 32
MSB LSB
@numberoverzero
numberoverzero / 00_hash_naming.cs
Created Apr 18, 2020
utility functions for filename nonces
View 00_hash_naming.cs
byte[] Sha256(string s)
{
using (var hash = SHA256.Create())
{
return hash.ComputeHash(Encoding.UTF8.GetBytes(s));
}
}
byte[] Fold(byte[] input)
{
@numberoverzero
numberoverzero / 01 sample_api.py
Last active Mar 31, 2020
accordian api change
View 01 sample_api.py
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
View bloop_shared_table_helper.py
# 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
View bloop_query_helper.py
# 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 Feb 11, 2019
compile stockfish from github on RHEL into /opt/stockfish
View compile_stockfish.sh
#!/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 Jul 26, 2019
support integral base string representations, especially for lexicographical sorting
View 00_base.py
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))