Skip to content

Instantly share code, notes, and snippets.

@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.
@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):