Skip to content

Instantly share code, notes, and snippets.

@bofm
bofm / copy_key.py
Last active May 18, 2016 13:43
Copy item from one dict to another supporting subdicts access using dots in keys
from functools import reduce
from contextlib import suppress
def copy_key(src_dict, dst_dict, src_key, dst_key):
_s, _d = src_dict, dst_dict
with suppress(KeyError):
if '.' in dst_key:
*subkeys, dst_key = dst_key.split('.')
dst_dict = reduce(lambda x, y: x.setdefault(y, {}), subkeys, dst_dict)
@bofm
bofm / myinit.sh
Last active June 10, 2016 22:29
Tiny init script in plain shell. Suitable for Docker. Handles signals and forwards them to the child processes for a clean shutdown.
#!/usr/bin/env sh
# Define your services here. One line per service.
services='
php-fpm -F
nginx -g "daemon off;"
'
pids=""
die(){ [ -n "$pids" ] && kill $pids 2>/dev/null; wait $pids; }
@bofm
bofm / netcat.sh
Last active June 23, 2016 12:07
Netcat secure file transfer on OS X
# receive
nc -l 0.0.0.0 1234 | openssl aes-256-cbc -salt -d | pv | tar vx -
# send
tar -c <file or dir> | openssl aes-256-cbc -salt -e | nc -w 3 <host> 1234
@bofm
bofm / tarfile_socket.ipynb
Last active July 26, 2016 13:25
Python tarfile + socket
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bofm
bofm / lru_cache2.py
Last active August 5, 2016 11:16
Python lru_cache-like decorator which supports mutable args
import typing
from collections import OrderedDict
def lru_cache2(size=128):
cache = OrderedDict()
def make_key(x):
if isinstance(x, typing.Mapping):
@bofm
bofm / lazy_stats.ipynb
Created September 9, 2016 12:24
Lazy stats
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bofm
bofm / sublime-keymap.ipynb
Last active September 10, 2016 17:24
Jupyter Notebook Sublime keymap
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bofm
bofm / async_lazy_apply.ipynb
Last active October 8, 2016 09:17
Python lazily asynchronously apply many functions to one iterable.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bofm
bofm / myqueue.py
Created December 7, 2016 13:50
Enhanced Python queue with additional .getall(), .clear() and .close() methods.
import threading
from queue import Empty, Full
class QueueClosed(Exception):
pass
class MyQueue():
@bofm
bofm / docker-ci.sh
Created December 13, 2016 16:17
docker-compose and fswatch based CI one-liner
ctnrs=(nginx); fswatch -e '.*/\..*' `grep context docker-compose.yml|awk -F':' '{print $2}'`|while read; do kill -0 "$p" &>/dev/null && kill "$p" && p=; sleep 0.2; docker-compose rm -f -v $ctnrs && docker-compose up --build $ctnrs &; p=$!; done