Skip to content

Instantly share code, notes, and snippets.

View nat-n's full-sized avatar

Nat Noordanus nat-n

View GitHub Profile
@nat-n
nat-n / shell_tokenizer.py
Created July 15, 2023 13:09
An experimental parser for bash syntax in python
"""
An experimental tokenizer for bash syntax.
This is an alternative to shlex from the standard library for tokenizing posix
style command line statements.
On top of the word-splitting and quote removal functionality of shlex, this
library also makes it possilbe to tell which glob characters are escaped, and
preserves semantics for variable & arithmetic expansions.
@nat-n
nat-n / asyncify.py
Created April 25, 2022 12:05
A utility for running non-async code concurrently using threads and asyncio
"""
A utility for running non-async code concurrently using threads and asyncio.
# Call a function as async in a background thread
```python
import asyncio
asyncify = AsyncThread()
@nat-n
nat-n / immutable_bitset.py
Last active February 26, 2022 21:51
A gold plated implementation of an immutable bit set in python, including full test coverage.
from collections.abc import Set
from numpy import packbits
import math
import random
from typing import Any, Callable, Collection, Iterable, Iterator
class ImmutableBitSet(Set):
_content: bytes
@nat-n
nat-n / bp_grcpio_example_usage.py
Last active November 2, 2020 16:20
POC for using betterproto with grpcio without generated a servicer
from magic_glue import create_grpc_handler, rpc_method
from .generated_code.mypackage import MyRequest, MyResponse
# Create a servicer class with the rpc methods we want to implement
class MyServicer:
# grpcio needs to know the fully qualified name of the service
# It would be better if this was available from the betterproto generated classes
# to save having to copy it from the proto file manually
service_name = "mypackage.MyService"
@nat-n
nat-n / asyncio_interactive_subproc.py
Last active November 11, 2019 17:29
A proof of concept for using asyncio to manage and dynamically interact with a subprocess via stdio.
import asyncio
class Worker:
def __init__(self, cmd, limit=None, separator=b"\n"):
self.cmd = cmd
self.limit = limit
self.separator = separator
async def send(self, data):
@nat-n
nat-n / varint.py
Last active April 4, 2019 20:33
Functions for encoding and decoding integers into byte strings with Variable-length quantity encoding. https://en.wikipedia.org/wiki/Variable-length_quantity
from typing import List, Tuple
def encode(number: int) -> bytes:
assert number >= 0, "Can only encode positive integers"
flag_mask = 0b10000000
value_mask = 0b01111111
bytes_buffer = [number & value_mask]
number = number >> 7
while number:
@nat-n
nat-n / basicAnimate.js
Last active August 31, 2018 07:59
Generic linear animation function
window.animate = (function () {
function animate(initialValue, finalValue, duration, callback, ease, startTime, currentTime) {
if (startTime) {
if (currentTime - startTime >= duration) {
callback(finalValue);
return;
}
callback(ease(currentTime - startTime, initialValue, finalValue - initialValue, duration));
}
@nat-n
nat-n / CircularBuffer.swift
Created June 3, 2018 20:24
CircularBuffer implementation in Swift 4
public struct CircularBuffer<T>: Collection, CustomStringConvertible {
private var cursor: Int = 0
private var contents: [T] = []
let capacity: Int
public init(capacity: Int) {
self.capacity = capacity
}
public subscript(i: Int) -> T {
@nat-n
nat-n / Recipe-bundling-fonts-with-headless-chrome.md
Last active February 28, 2024 19:23
How to build a fontconfig bundle for adding arbitrary fonts to headless chrome independent of the OS. This is specifically useful for deploying headless chrome to AWS lambda where it is necessary to include fonts for rendering CJK (Chinese, Japanese, Korean) characters into the deployed bundle.

Building fontconfig

Start up a lambda-like docker container:

docker run -i -t -v /tmp:/var/task lambci/lambda:build /bin/bash

Install some dependencies inside the container:

yum install gperf freetype-devel libxml2-devel git libtool -y

easy_install pip

@nat-n
nat-n / tmux_recipes.md
Last active August 29, 2015 14:16
tmux recipes

Programatically setup up session with splitpane window with different REPLs

tmux new-session -d -s repls 'irb'
tmux send-keys 'p "Hello Ruby!"' C-m
tmux rename-window 'Repls'
tmux select-window -t 'Repls'
tmux split-window -h 'python'
tmux send-keys 'print "Hello Python!"' C-m
tmux split-window -v -t 0 'node'

tmux send-keys 'console.log("Hello node!")' C-m