Skip to content

Instantly share code, notes, and snippets.

/******* TEST ***********/
void Plotter::addPlotPoints()
{
m_plot = new KPlotWidget( );
QGraphicsProxyWidget *proxy;
QGraphicsScene scene;
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
QGraphicsWidget *form = new QGraphicsWidget;
@joshz
joshz / gist:1213620
Created September 13, 2011 11:21
split/map/pass to func simple test
def f(m, n, t, p):
pass
if __name__ == "__main__":
import time
for j in range(5):
start1 = time.clock()
for i in range(2000000):
data = "100 10 2 1".split()
f(*map(int, data))
@joshz
joshz / update_all_modules.py
Created September 20, 2011 08:56
update all modules with pip
# py 2.7 - upgrading brakes for: pil, pyzmq, lxml, ipython, numpy, pyflakes, pygame pylint, some complain about non empty build dir, easy fix
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("pip install --upgrade " + dist.project_name, shell=True)
@joshz
joshz / bresenham.py
Created October 10, 2011 00:11
Bresenham's line algorithm
def line(x0, x1, y0, y1):
points = []
def swap(o1, o2):
o1 ^= o2
o2 ^= o1
o1 ^= o2
steep = abs(y1-y0) > abs(x1 - x0)
@joshz
joshz / gist:1371355
Created November 16, 2011 20:55
iterate over several dicts with no merging
from itertools import chain
for k,v in chain(d1.iteritems(), d2.iteritems(), d3.iteritems()):
do_some_stuff(k, v)
#or
ds = d1,d2,d3
for k,v in chain.from_iterable(d.iteritems() for d in ds):
do_some_stuff(k, v)
@joshz
joshz / enfe.py
Created November 29, 2011 22:36
extract first number from an email
import email
import re
e = '''
some string with full email contents
'''
msg = email.message_from_string(e)
body = msg.get_payload()
n = re.search(r'\d+', body, re.MULTILINE)
@joshz
joshz / cl.py
Created November 30, 2011 16:06
clean some content inside tags
import re
s = 'abcd<aaa>some thing <#^&*some more!#$@ </aaa> abcdefgasf <aaa>asfaf %^&*$saf asf %$^ </aaa> <another tag> some text </another tag> <aaa>sfafaff#%%%^^</aaa>'
inside_tags = re.findall('<aaa>(.+?)</aaa>', s)
cleaned_contents = [ re.sub('\W', '_' ,content) for content in inside_tags ]
zipped = zip(inside_tags, cleaned_contents)
s
for old, new in zipped:
s = s.replace(old, new)
print s
@joshz
joshz / partialshuffle.py
Created December 1, 2011 22:24
couple implementations of partial shuffling of a string
import random
def partial_shuffle(st, p=20):
p = int(round(p/100.0*len(st)))
ds = dict([(c, i) for c, i in enumerate(st)])
shuffled = list()
pick_index = random.choice
add_to_list = shuffled.append
@joshz
joshz / memoized.py
Created January 6, 2012 14:16
Memoizing decorator, caches function's return value, expires after ttl time in seconds or ctl calls - Python
import time
class memoized(object):
def __init__(self, ctl=3, ttl=2):
self.cache = {}
self.ctl = ctl
self.ttl = ttl
self._call_count = 1
def __call__(self, func):
@joshz
joshz / tree.md
Created April 23, 2012 22:33 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!