Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / resource_limits.py
Last active August 29, 2015 14:04
runtime resource limits (for your IPython notebook)
# do this first:
! sudo apt-get install cpulimit
from os import getpid
from resource import setrlimit, RLIMIT_RSS, RLIM_INFINITY, getrusage, RUSAGE_SELF
# limit CPU: use only 1% of 1 CPU
pid = getpid()
! cpulimit -b -p $pid -c 1 -l 1
@dutc
dutc / __ipynb__.py
Last active August 29, 2015 14:04
import IPython notebooks as modules
#!/usr/bin/env python
from imp import new_module
class IPythonModule(type(new_module(''))):
from json import loads
from imp import new_module
loads = staticmethod(loads)
new_module = staticmethod(new_module)
def __init__(self, name, path):
@dutc
dutc / newton.py
Last active August 29, 2015 14:05
Netwon's method (or any other iterative solving technique) with numpy
from numpy.random import randint
from numpy import sqrt
# Newton's method:
# `x' is input, `y' is iteratively refined answer
# solve for square root of x: y**2 = x
# f (y) = y^2 - x
# f'(y) = 2y
# y[1] = y[0] - f(y[0]) / f'(y[0])
@dutc
dutc / Makefile
Last active August 29, 2015 14:08
testing dlmopen
CC=gcc -std=c99 -Wall
dlmopen-test: dlmopen-test.c
${CC} -o $@ $^ -ldl
@dutc
dutc / Makefile
Last active August 29, 2015 14:08
Did you mean? in Python (Bonus Round!)
CC=gcc -std=c99 -Wall
didyoumean.so: didyoumean.c didyoumean-safe.c
${CC} `python-config --cflags` `python-config --includes` -Wl,--export-dynamic -fPIC -shared -o $@ $^ -ldl `python-config --libs`
@dutc
dutc / wheel.py
Last active August 29, 2015 14:17
simple Price is Right game
# from sys import version_info
# assert version_info.major == 3
candidates = ['dontusethiscode', 'roguelynn', ] # ...
from time import sleep
slp = lambda n, sleep=sleep: lambda: sleep(n)
from itertools import chain, repeat
sleeps = chain(repeat(slp(.01), 1000),
@dutc
dutc / swapmeta,py
Created June 13, 2015 02:01
metaclass that swaps the order of the base classes
#!/usr/bin/env python3
class Swapped(type):
def __new__(cls, name, bases, body):
if not bases:
t = type.__new__(cls, name, bases, body)
t.__type = name, bases, body
return t
if len(bases) == 1:
st = type.__new__(cls, name, bases[1:], body)
@dutc
dutc / kwargh.py
Created September 1, 2015 16:04
help(f) # KWARGHHH!!!
#!/usr/bin/env python3
from ast import parse
from ast import AST, FunctionDef, Lambda, Call
from collections import Iterable
# some notes
# kwargh:
# when you're using matplotlib or pandas
# and you do help(func) to figure out what
@dutc
dutc / genmethod.py
Last active October 10, 2015 18:30
use a generator as an instancemethod
from itertools import count, cycle
from collections import Iterable
from functools import wraps
class genmethod:
def __init__(self, g):
self.g = g
def __get__(self, instance, owner):
gi = self.g(instance)
@wraps(self.g)
@dutc
dutc / Makefile
Last active October 28, 2015 18:52
Makefile for building Jupyter notebook from source-built Python 3.5
REPOS = jupyter/jupyter \
jupyter/jupyter_core \
jupyter/jupyter_client \
jupyter/notebook \
jupyter/nbformat \
jupyter/nbconvert \
ipython/ipython
BINARIES = python3 node
TARBALL_python3 = https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
TARBALL_node = https://nodejs.org/dist/v4.2.1/node-v4.2.1.tar.xz