Skip to content

Instantly share code, notes, and snippets.

@sevagh
sevagh / metaform.py
Created November 30, 2019 05:37
Terraform version manager
#!/usr/bin/env python3
import os
import stat
import sys
import urllib.request
import zipfile
from html.parser import HTMLParser
@sevagh
sevagh / env_injector.rs
Created November 30, 2019 05:40
Environment variable injector
#![feature(getpid)]
use std::process::{self, Command, Output, Stdio};
use std::result;
use std::io::{self, BufRead, Write};
use std::num::ParseIntError;
use std::fs::File;
type Result<T> = result::Result<T, Box<io::Error>>;
@sevagh
sevagh / slow_worm.py
Created November 30, 2019 05:41
Golfed shell in Python 3.6
#!/usr/bin/env python3.6
import sys, os, signal, readline
signal.signal(signal.SIGINT, signal.default_int_handler)
P = {'pid': ['state', 'ppid', 'name'], os.getpid(): ['0', os.getppid(), sys.argv[0]]}
W = {'worm-ps': lambda _: [print(f"{k}\t{v[1]}\t{v[0]}\t{' '.join(v[2:])}") for k, v in P.items()],
'worm-kill': lambda a: os.kill(int(a[1]), int(a[2])) if len(a) == 3 else print('usage: worm-kill pid sig', file=sys.stderr),
'worm-history': lambda _: print('\n'.join([(readline.get_history_item(i)) for i in range(readline.get_current_history_length()) if readline.get_history_item(i)])),
@sevagh
sevagh / palette.py
Created November 30, 2019 05:44
Count colors in an image
#!/usr/bin/env python3
from collections import Counter
import numpy
from PIL import Image
from webcolors import css3_hex_to_names, hex_to_rgb
from urllib.request import urlopen
import sys
import time
import matplotlib.pylab as plb
@sevagh
sevagh / pppid.h
Created November 30, 2019 05:49
Get pid at any depth with procfs
#include <sys/types.h>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <string>
#define PROC_STR "/proc/%s/stat"
pid_t
_getpppid(const char *, int);
@sevagh
sevagh / quadtree_sparse_matrix.py
Created December 7, 2019 14:41
scipy-quadtree-sparse-matrix
'''
quadtree.py
A matrix implementation based on a pointer region quadtree, implemented in pure Python.
The script takes a scipy sparse matrix format as the only argument and calls 'eval' to use that format as the control matrix.
See: https://docs.scipy.org/doc/scipy/reference/sparse.html
Several sizes and densities are tested (all matrices are square). Measured:
@sevagh
sevagh / bloom_filter.rs
Created January 7, 2020 03:28
basic Bloom Filter with 4 hashing functions
use bit_vec::BitVec;
use std::hash::{Hash, Hasher};
// use 4 hash functions
use ahash::AHasher;
use fnv::FnvHasher;
use metrohash::MetroHash64;
use siphasher::sip::SipHasher;
pub struct BloomFilter {
@sevagh
sevagh / cupy_oom_problem.py
Created April 22, 2021 15:33
demo of cupy out of memory issue with large ndarray
import numpy as np
import scipy.fft
import cupyx
import cupy
# managed memory to stream large ndarrays to cuda
cupy.cuda.set_allocator(cupy.cuda.MemoryPool(cupy.cuda.memory.malloc_managed).malloc)
@sevagh
sevagh / periphery2musdb.py
Last active August 26, 2022 13:09
convert periphery stems to musdb
import os
import numpy
import itertools
import soundfile
import sys
import multiprocessing
import argparse
sample_rate = 44100
@sevagh
sevagh / bootstrap-mdx-cpp.sh
Last active August 13, 2023 14:07
create newproj.cpp (oriented for music demixing projects e.g. demucs.cpp)
#!/usr/bin/env bash
# script for bootstrapping an '$mdx.cpp' project
# mdx = music demixing
# e.g. umx.cpp (open-unmix)
# demucs.cpp (Demucs)
set -Eou pipefail
set -x