Skip to content

Instantly share code, notes, and snippets.

@bofm
bofm / run_timeout.py
Created November 8, 2017 10:23
set maximum execution time for a python script
import signal
def set_run_timeout(timeout):
"""Set maximum execution time of the current Python process"""
def alarm(*_):
raise SystemExit("Timed out!")
signal.signal(signal.SIGALRM, alarm)
signal.alarm(timeout)
@bofm
bofm / jupyter-system-site-packages.sh
Created April 20, 2017 15:24
Use virtualenv python kernel inside the Jupyter Notebook installed in the system site-packages
# install Jupyter Notebook into system site-packages
pip install -U notebook
# create a virtualenv and use the Jupyter from the system site-packages
mkvirtualenv --system-site-packages myvenv
# the virtualenv is now activated
pip install ipykernel
VM_NAME=myvm python -m ipykernel install --user --name $VM_NAME --display-name $VM_NAME
python -m jupyter notebook
# In the browser: New -> select kernel "myvm"
@bofm
bofm / st_capture_edits.py
Last active March 3, 2017 09:27
Sublime Text captute edits
# Source:
# https://forum.sublimetext.com/t/retrieving-inserted-and-deleted-text/18697/24
import sublime
import sublime_plugin
def get_cursors(view):
# can't use `view.sel()[:]` because it gives an error `TypeError: an integer is required`
return [cursor for cursor in view.sel()]
@bofm
bofm / side_effect.py
Created February 2, 2017 12:47
Side Effect Python decorator.
from functools import wraps
def side_effect(callback, before=True, after=False):
"""A decorator.
Wraps the decorated function. The callback will be called before and after each
call of the decorated function according to the parameters.
Args:
@bofm
bofm / binary_clock_task.ipynb
Created January 26, 2017 11:45
Binary clock task
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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
@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 / async_lazy_apply.ipynb
Last active October 8, 2016 09:17
Python lazily asynchronously apply many functions to one iterable.
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bofm
bofm / lazy_stats.ipynb
Created September 9, 2016 12:24
Lazy stats
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.