Skip to content

Instantly share code, notes, and snippets.

@curzona
curzona / callbacks.py
Last active August 29, 2015 14:23
Add callbacks to any function in python
from __future__ import print_function
class Watcher(object):
def __init__(self, func):
self.func = func
self.before = []
self.after = []
def __call__(self, *args, **kargs):
for callback in self.before:
@curzona
curzona / stdout.txt
Created June 25, 2015 13:09
spacebox-npc-agent Getting Started
curzona@curzona-desktop:~/Desktop/hello_spacebox/agent (master)$ cp sample.env heroku.env
curzona@curzona-desktop:~/Desktop/hello_spacebox/agent (master)$ vim heroku.env
curzona@curzona-desktop:~/Desktop/hello_spacebox/agent (master)$ foreman run -e heroku.env ./tests/construction.js
module.js:340
throw err;
^
Error: Cannot find module 'spacebox-common/src/three_helpers.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
@curzona
curzona / retry.py
Created June 22, 2015 00:55
retry utility for Python
import time
import inspect
import types
def _assert(func):
if not func():
if isinstance(func, types.FunctionType):
raise AssertionError(inspect.getsource(func).strip())
else:
raise AssertionError()
@curzona
curzona / test_child_process.py
Created June 19, 2015 02:32
cleaning up child process tree spawned from test case
import time
import subprocess
import psutil, os
def kill_proc_tree(pid, including_parent=True):
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
@curzona
curzona / hello_subprocess_kill.py
Created June 12, 2015 20:48
Killing subprocesses in python
import time
import subprocess
import threading
cmd = ['timeout', '10']
p = subprocess.Popen(cmd)
p.killed = False
def timeout():
time.sleep(1)
@curzona
curzona / get.txt
Created June 8, 2015 03:00
pytest-bdd output
py.test -l --tb=long
=========================== test session starts ===========================
platform linux2 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4
plugins: ordering, bdd, instafail, cov, xdist
collected 1 items
test_bdd.py F
================================ FAILURES =================================
_________________ test_SomeDeterminableBusinessSituation __________________
@curzona
curzona / README.md
Last active August 29, 2015 14:21
Render waveform as ascii text

i2c_write_3bytes.png

@curzona
curzona / jsonformatter.py
Created May 27, 2015 01:12
JSON auto-formatter
import json
import sys
import argparse
parser = argparse.ArgumentParser(prog='jsonformatter')
parser.add_argument('input', type=str, help='Input file')
parser.add_argument('output', type=str, nargs='?', help='Output file')
args = parser.parse_args()
@curzona
curzona / example.py
Created May 24, 2015 20:40
Add operator for Dictionaries
from pprint import pprint
a = _Dict({'a':'1'})
b = _Dict({'b':'2'})
pprint(a + b)
@curzona
curzona / od.py
Created March 1, 2015 06:36
unix od in python
data = open("myfile", "rb").read()
width = 16
lines = [data[i:i+width] for i in range(0, len(data), width)]
print "\n".join([" ".join(map(lambda x: "%02X" % x, map(ord, line))) for line in lines])