Skip to content

Instantly share code, notes, and snippets.

bofm /
Last active Apr 20, 2017
python generate random string
# Python 3
import random
import string
''.join(random.choice(string.ascii_letters + string.digits + '#$-=_') for n in range(32))
# Python 2
import random
import string
''.join([random.choice(string.ascii_letters + string.digits + '#$-=_') for n in xrange(32)])
bofm /
Last active Apr 11, 2017
defines IPython Notebook line magic to render 2d-array as HTML table.
from IPython.core.magic import (register_line_magic, register_cell_magic,
from six import string_types
class T(list):
def _repr_html_(self):
html = [u"<table>"]
for row in self:
bofm / my_sed
Last active Oct 22, 2019
Painless sed
View my_sed
my_sed() {
python -c 'import re,sys; sys.stdout.write(re.sub(sys.argv[1], sys.argv[2],' "$1" "$2"
bofm /
Last active Sep 26, 2015 sources.list apt ubuntu
#!/usr/bin/env bash
cp /etc/apt/sources.list /etc/apt/sources.list_bkp
sed -ri 's/\/\/\w+\.ubuntu\.com/\/\/mirror\.yandex\.ru/' /etc/apt/sources.list
# OR
cat /etc/apt/sources.list | \
python -c 'import re,sys; sys.stdout.write(re.sub(sys.argv[1], sys.argv[2],' \
'//\w+\.ubuntu\.com' '//' > /etc/apt/sources.list1
mv /etc/apt/sources.list1 /etc/apt/sources.list
# It works on RedHat, OEL, Centos
curl -o /usr/bin/rlwrap && chmod 755 /usr/bin/rlwrap
bofm /
Created Nov 6, 2015
Shows disk space usage of Docker containers.
#!/usr/bin/env bash
for d in `docker ps -aq`; do
d_name=`docker inspect -f {{.Name}} $d`
echo "========================================================="
echo "$d_name ($d) container size:"
sudo du -d 2 -h /var/lib/docker/aufs | grep `docker inspect -f "{{.Id}}" $d`
echo "$d_name ($d) volumes:"
for mount in `docker inspect -f "{{range .Mounts}} {{.Source}}:{{.Destination}}
{{end}}" $d`; do
bofm / docker_cleanup
Last active Nov 19, 2015
Docker cleanup
View docker_cleanup
#!/usr/bin/env bash
docker rm $(docker ps -aq -f status=exited)
docker rmi $(docker images --quiet --filter "dangling=true")
bofm /
Last active May 5, 2016
Simple function to print diff of two strings
import subprocess
def diff(one, two):
with tempfile.TemporaryDirectory() as tmpdir:
f1, f2 = ('%s/%s' % (tmpdir, n) for n in (1,2))
for f, txt in zip((f1, f2), (one, two)):
with open(f, 'w') as f:
cmd = ('diff -w %s %s' % (f1, f2)).split()
bofm /
Last active Jul 30, 2019
Escape invalid XML characters in Python 3
#!/usr/bin/env python3
import sys
import re
_illegal_unichrs = ((0x00, 0x08), (0x0B, 0x1F), (0x7F, 0x84), (0x86, 0x9F),
(0xD800, 0xDFFF), (0xFDD0, 0xFDDF), (0xFFFE, 0xFFFF),
(0x1FFFE, 0x1FFFF), (0x2FFFE, 0x2FFFF),
(0x3FFFE, 0x3FFFF), (0x4FFFE, 0x4FFFF),
bofm /
Last active May 18, 2016
Python merge records (lists of dicts) by key
def merge_records_by_key(dicts1, dicts2, key):
"""Updates each dict1 containing key `key` from `dicts1`
with the corresponding dict2 from `dicts2` having
dict1[key] == dict2[key].
if len(dicts1) > len(dicts2):
probe, build = dicts1, dicts2
hashmap = {d[key]: d for d in build if key in d}