This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def line(x0, x1, y0, y1): | |
points = [] | |
def swap(o1, o2): | |
o1 ^= o2 | |
o2 ^= o1 | |
o1 ^= o2 | |
steep = abs(y1-y0) > abs(x1 - x0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******* TEST ***********/ | |
void Plotter::addPlotPoints() | |
{ | |
m_plot = new KPlotWidget( ); | |
QGraphicsProxyWidget *proxy; | |
QGraphicsScene scene; | |
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout; | |
QGraphicsWidget *form = new QGraphicsWidget; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import pstats | |
def meth(): | |
check = 'abst' | |
c=1 | |
r=1 | |
location = (-1, -1) | |
with open("temp.txt", 'rb') as p: | |
ch = p.read(1) |