Skip to content

Instantly share code, notes, and snippets.

View chr5tphr's full-sized avatar

Christopher chr5tphr

  • Technically a University in Berlin
  • There's no place like 0x7f000001
View GitHub Profile
@chr5tphr
chr5tphr / gitupd
Last active July 29, 2022 09:31
Synchronize uncomitted changes
#!/bin/bash
RMTE="${1:?"No host supplied!"}"
RDIR="${2:?"No path supplied!"}"
REPO="${3:-"refs/heads/sync"}"
git push "${RMTE}:${RDIR}" "+@:${REPO}" && git diff HEAD | ssh "${RMTE}" \
"{ git -C '${RDIR}' reset --hard '${REPO}' && git -C '${RDIR}' apply --index - ; }"
@chr5tphr
chr5tphr / combiget.sh
Last active May 22, 2023 13:56
Get elements from a combination of bash arrays given a flat index
#!/usr/bin/env bash
# combiget: get appropriate elements from a combination of arrays given a flat index
combiget() {
ind="${1:?'No index.'}"
arrs=("${@:2}")
for (( i = 0 ; i < ${#arrs[@]} ; i++)); do
n="$ind"
for (( j = i + 1 ; j < ${#arrs[@]} ; j++ )); do
@chr5tphr
chr5tphr / require-venv.sh
Created July 30, 2020 20:03
Require/ install python virtual environment with flock to support concurrent calls.
#!/usr/bin/env bash
RETVAL=0
VENV="${1:?"No path to virtual environment supplied!"}"
shift
exec 3>"${VENV}.lock"
if flock --exclusive --wait 180 3; then
if ! [ -d "$VENV" ]; then
import h5py
import click
@click.command()
@click.argument('sources', nargs=-1, type=click.Path(exists=True, dir_okay=False))
@click.argument('destination', type=click.Path(dir_okay=False, writable=True))
def main(sources, destination):
outfd = h5py.File(destination, 'a')
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
import logging
import os
import click
import yaml
logger = logging.getLogger(__name__)
@chr5tphr
chr5tphr / lazy_proxy.py
Last active October 11, 2019 13:06
LazyProxy Python
"""LazyProxy and LazyDict for lazily evaluated objects"""
class LazyProxy:
"""Lazily apply a chain of functions on a single object"""
def __init__(self, parent=None):
"""Lazily apply a chain of functions on a single object
Parameters
----------
parent : LazyProxy or None
@chr5tphr
chr5tphr / h5tree.py
Last active October 10, 2019 13:03
hdf5 tree visualizer
import h5py
def h5tree(base, key='/', depth=1):
if isinstance(base, h5py.Group):
return key + '\n' + '\n'.join(' ' * depth + h5tree(obj, key, depth + 1) for key, obj in base.items())
else:
blist = str(base[()]).split('\n', maxsplit=1)
return '{}: {}'.format(key, blist.pop(0) + (' (...)' if blist else ''))
@chr5tphr
chr5tphr / pulse_simple_wave.c
Created October 9, 2019 22:37
pulseaudio c wave
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 1024
void square(short *data, unsigned int length, unsigned int period, short magnitude) {
unsigned int i;
from socket import socket, AF_UNIX
from pdb import Pdb
from os import unlink, getuid, getpid, chmod
from signal import signal, SIGUSR1
def rtrace(signum, frame):
spath = '/tmp/{:d}.pdb.{:d}'.format(getuid(), getpid())
with socket(AF_UNIX) as sock:
chmod(sock.fileno(), 0o700)
sock.bind(spath)
#!/usr/bin/env ruby
usg = []
loop {
begin
usg = ([IO.popen(["df", "--output=used", "--block-size=1024", "."]).readlines[1].to_i] + usg)[0, 5]
if usg.length > 4
STDOUT << "\r" << (->(x){x.sum / x.length}).call(usg[0..-2].zip(usg[1..-1]).collect {|x, y| x - y}) << " KiB/s "
end
sleep 0.5
rescue Interrupt