Skip to content

Instantly share code, notes, and snippets.

Avatar

Christopher chr5tphr

View GitHub Profile
@chr5tphr
chr5tphr / require-venv.sh
Created Jul 30, 2020
Require/ install python virtual environment with flock to support concurrent calls.
View require-venv.sh
#!/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
View h5merge.py
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')
View fillsvg.py
#!/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 Oct 11, 2019
LazyProxy Python
View lazy_proxy.py
"""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 Oct 10, 2019
hdf5 tree visualizer
View h5tree.py
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 ''))
View pulse_simple_wave.c
#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;
View usock_pdb.py
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)
View fsiorate.rb
#!/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
View sgetaskhost.rb
#!/usr/bin/env ruby
require "rexml/document"
fd = IO.popen(["/opt/ge/bin/lx-amd64/qstat", "-j", ENV.fetch("JOB_ID", "0"), "-xml"])
doc = REXML::Document.new(fd.read)
xpath = "/detailed_job_info/djob_info/element/JB_ja_tasks/element[JAT_task_number/text()=1]/JAT_granted_destin_identifier_list/element/JG_qhostname/text()"
STDOUT << REXML::XPath.first(doc, xpath)
@chr5tphr
chr5tphr / bwsemi
Created Mar 1, 2019
Bubblewrap semi isolation
View bwsemi
#!/bin/sh
BWROOT="${BWROOT:-$PWD/base}"
env -i bwrap \
--bind "$BWROOT" / \
--unshare-user \
--unshare-cgroup \
--unshare-ipc \
--unshare-pid \
--unshare-uts \
You can’t perform that action at this time.