Skip to content

Instantly share code, notes, and snippets.

Avatar

sevagh

View GitHub Profile
@sevagh
sevagh / metaform.py
Created Nov 30, 2019
Terraform version manager
View metaform.py
#!/usr/bin/env python3
import os
import stat
import sys
import urllib.request
import zipfile
from html.parser import HTMLParser
@sevagh
sevagh / fs_trie.rs
Created Nov 30, 2019
Rust trie that serializes to filesystem
View fs_trie.rs
//! # A trie that can be saved to and loaded from a file
//!
//! This crate implements a Trie with char keys.
//! The trie can be saved to and loaded from a file on the local filesystem.
//! This allows the user to persist the trie between executions.
//!
//! Basic example:
//!
//! ```ignore
//! let trie_file = "/path/to/trie-file";
@sevagh
sevagh / env_injector.rs
Created Nov 30, 2019
Environment variable injector
View env_injector.rs
#![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 Nov 30, 2019
Golfed shell in Python 3.6
View slow_worm.py
#!/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 Nov 30, 2019
Count colors in an image
View palette.py
#!/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 Nov 30, 2019
Get pid at any depth with procfs
View pppid.h
#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 Dec 7, 2019
scipy-quadtree-sparse-matrix
View quadtree_sparse_matrix.py
'''
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 Jan 7, 2020
basic Bloom Filter with 4 hashing functions
View bloom_filter.rs
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 {
You can’t perform that action at this time.