Skip to content

Instantly share code, notes, and snippets.

@remram44
remram44 / ipython-errors.py
Created August 30, 2013 23:39
Formatting IPython remote errors
def print_compositeerror(e):
sys.stderr.write("Got %d exceptions from IPython engines:\n" %
len(e.elist))
for e_type, e_msg, formatted_tb, infos in e.elist:
sys.stderr.write("Error from engine %d (%r):\n" % (
infos['engine_id'], infos['engine_uuid']))
sys.stderr.write("%s\n" % strip_ansi_codes(formatted_tb))
def print_remoteerror(e):
#!/bin/sh
# rename.sh [-n] [-r] <regex> [file [...]]
notreally=
sedopts=
while getopts ":nr" opt ; do
case $opt in
n)
@remram44
remram44 / color.py
Created August 20, 2013 17:33
HSV color generator
def rgb2hsv(rgb):
"""Converts RGB to HSV.
Note that H may be None when S is 0 (for grey colors).
"""
r, g, b = rgb
minimum = min(r, g, b)
maximum = max(r, g, b)
v = maximum
@remram44
remram44 / timeprefix.py
Created July 22, 2013 15:28
Prefixes command output with time
import sys
import time
if __name__ == '__main__':
begin = time.time()
l = sys.stdin.readline()
while l:
sys.stdout.write("%f %s" % (time.time() - begin, l))
l = sys.stdin.readline()
@remram44
remram44 / QtWrapper.py
Created July 12, 2013 16:17
PySide/PyQt4 abstraction module
"""Adapted from http://askubuntu.com/a/141641
This compatibility layer allows to use either PySide or PyQt4 as the Qt
binding. It will choose what's available, defaulting on PySide, unless the
QT_API environment variable is set.
"""
import os
import sys
@remram44
remram44 / ipython_callbacks.py
Last active December 19, 2015 05:09
Subclass of IPython.parallel.Client providing a thread-safe callback mechanism
from IPython.parallel import Client
import threading
import time
import contextlib
class SafeClient(object):
"""Wrapper for IPython.parallel.Client adding callbacks for AsyncResults.
@remram44
remram44 / pickleable_staticmethods.py
Created June 12, 2013 22:21
Allows pickling staticmethods through a metaclass
# From http://stackoverflow.com/a/1914798/711380
class _PickleableStaticMethod(object):
def __init__(self, fn, cls=None):
self.cls = cls
self.fn = fn
def __call__(self, *args, **kwargs):
return self.fn(*args, **kwargs)
def __get__(self, obj, cls):
return _PickleableStaticMethod(self.fn, cls)
def __getstate__(self):
@remram44
remram44 / debugmt.py
Last active December 18, 2015 00:49
Debug metaclass listing calls to methods
import contextlib
pad = ''
@contextlib.contextmanager
def call():
global pad
pad += " "
yield
@remram44
remram44 / matrix_to_quaternion.c
Created March 28, 2013 04:20
matrix to quaternion
/* quaternion from matrix */
/* from Id Software, From Quaternion to Matrix and Back, 2005 */
float ReciprocalSqrt(float x)
{
long i;
float y, r;
y = x * 0.5f;
i = *(long*)&x;
i = 0x5f3759df - (i >> 1);
def file_is_binary(filename):
with open(filename) as fp:
return chr(0) in iter(fp.read(8000))