Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
/******* TEST ***********/ | |
void Plotter::addPlotPoints() | |
{ | |
m_plot = new KPlotWidget( ); | |
QGraphicsProxyWidget *proxy; | |
QGraphicsScene scene; | |
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout; | |
QGraphicsWidget *form = new QGraphicsWidget; |
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)) |
# 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) |
def line(x0, x1, y0, y1): | |
points = [] | |
def swap(o1, o2): | |
o1 ^= o2 | |
o2 ^= o1 | |
o1 ^= o2 | |
steep = abs(y1-y0) > abs(x1 - x0) |
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) |
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) |
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 |
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 | |
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): |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!