Skip to content

Instantly share code, notes, and snippets.

View charles-l's full-sized avatar
👾
making games

Charles charles-l

👾
making games
View GitHub Profile
@charles-l
charles-l / noise.py
Created August 24, 2020 14:04
Render a noise pattern onto an image in Blender
import bpy
import numpy as np
from mathutils import noise
img = bpy.data.images['some-img']
w, h = img.size
# 4 channel image (RGBA), f64
a = np.array(img.pixels).reshape((h, w, 4))
@charles-l
charles-l / has_many.py
Created June 7, 2020 18:11
has_many relationship from Rails for Pandas
import pandas as pd
@pd.api.extensions.register_dataframe_accessor("rel")
class RelationshipAccessor:
'''
Add a relationship accessor to dataframe objects allowing Rails-like
access to related dataframes. e.g.
>>> authors = pd.DataFrame({'name': ['C. S. Lewis', 'Lewis Carroll']})
>>> books = pd.DataFrame({'title': ["Alice's Adventures in Wonderland",
@charles-l
charles-l / frame_tracer.py
Created May 23, 2020 00:04
An eBPF frame tracer tool written in Python
'''
Expects probes to be defined in the target process, e.g.:
import stapsdt
provider = stapsdt.Provider('game')
frame_begin_probe = provider.add_probe('frame_begin')
frame_end_probe = provider.add_probe('frame_end')
provider.load()
...
@charles-l
charles-l / hexdump.zig
Last active May 7, 2020 23:43
Small hexdump utility written in zig to test the language out a bit.
const std = @import("std");
pub const BlockIterator = struct {
buffer: []const u8,
block_size: usize,
index: usize,
pub fn next(self: *BlockIterator) ?[]const u8 {
if (self.index == self.buffer.len) {
return null;
@charles-l
charles-l / hexahue.py
Created April 26, 2020 00:47
hexahue decoder
# decodes hexahue images
import cv2
import numpy as np
codewords = {'mrgybc': 'a',
'rmgybc': 'b',
'rgmybc': 'c',
'rgymbc': 'd',
'rgybmc': 'e',
'rgybcm': 'f',
@charles-l
charles-l / guix
Created April 9, 2020 20:45
openrc init script for guix daemon
#!/sbin/openrc-run
description="daemon required by the guix package manager"
command=/root/.config/guix/current/bin/guix-daemon
command_args="--build-users-group=guixbuild"
command_background="yes"
pidfile="/run/${RC_SVCNAME}.pid"
from typing import Callable, Any
from dataclasses import dataclass
class _CpsA: # (x, k) -> ()
@staticmethod
def lift(f) -> '_CpsA':
return _CpsA(lambda x, k: k(f(x)))
def __init__(self, cps: Callable[[Any, Callable], None]):
self.cps = cps
@charles-l
charles-l / Makefile
Last active January 10, 2019 03:16
c++ compile times
FILES=$(wildcard *.cpp)
NJOBS=$(shell echo $$(nproc) + 1 | bc)
all:
g++ $(FILES)
generate_empty:
echo "int main() {}" > main.cpp
for i in `seq 500`; do touch f$$i.cpp; done
@charles-l
charles-l / kalmanfilter.ijs
Created May 13, 2018 19:51
Kalman filter implementation in the J Language
NB. Port of https://scipy-cookbook.readthedocs.io/items/KalmanFiltering.html
require 'stats/distribs/normal'
n =: 50 NB. number of steps to compute
x =: _0.37227 NB. real value
z =: (x , 0.1) rnorm n NB. readings (normal distribution)
Q =: 1e_5 NB. process variance
R =: 0.1^2 NB. estimate of variance
def kalman_predict(x_hat, u, P, wheel_slip_std):
# non-linear state transition
def f(p, u):
x, y, θ = p
dΦ_l, dΦ_r = u
return v(
x + (((WHEEL_R * dΦ_l) / 2) +
((WHEEL_R * dΦ_r) / 2)) * DT * cos(θ),
y + (((WHEEL_R * dΦ_l) / 2) +
((WHEEL_R * dΦ_r) / 2)) * DT * sin(θ),