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
saved = {} | |
def web_lookup(url): | |
if url in saved: | |
return saverd[url] | |
page = urrlib.urlopen(url).read() | |
saved[url] = page | |
return page | |
# NEEDS TO BE A PURE FUNCTION, NO SIDE EFFECTS LIKE PRINT |
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
try: | |
os.remove('somefile.tmp') | |
except OSError: | |
pass | |
# note: checking if the file exists before deletion leads to a race condition | |
# PYTHON 3 | |
from contextlib import suppress |
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 multiprocessing import Pool | |
pool = Pool(4) | |
results = pool.map(urllib2.urlopen, urls) | |
pool.close() | |
pool.join() | |
# if you want threads instead of processes | |
from multiprocessing.dummy import Pool |
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
class DBConnection(object): | |
def __init__(self, address): | |
self.crappy_db = CrappyDBConnection(address) | |
def __enter__(self): | |
self.crappy_db.connect() | |
return self # what is returned here will be availabe via "as" | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
if exc_type == ConnectionError: |
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 contextlib import contextmanager | |
@contextmanager | |
def connect_to_db(address): | |
db = CrappyDBConnection(address) | |
try: | |
yield db | |
except ConnectionError: | |
logging.exception('Connection dropped') | |
db.cleanup('rollback') |
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
# USAGE | |
@track_entry_and_exit('widget loader') | |
def activity(): | |
print('Some time consuming activity goes here') | |
load_widget() | |
with track_entry_and_exit('widget loader'): | |
print('Some time consuming activity goes here') | |
load_widget() |
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
@f1(arg) | |
@f2 | |
def func(): pass | |
# is equivalent to | |
def func(): pass | |
func = f1(arg)(f2(func)) | |
def my_decorator(a_function_to_decorate): | |
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
class RoutingTable(object): | |
def __init__(self, crappy_rt): | |
self.crappy_rt = crappy_rt | |
def __len__(self): | |
return self.crappy_rt.getSize() | |
def __getitem__(self, index): | |
if index >= len(self): | |
raise IndexError |
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 product | |
product(('ENDPOINT1', 'ENDPOINT2'), ('GET', 'POST'), ('200', '404', '500')) | |
# returns a generator | |
[('ENDPOINT1', 'GET', '200'), ('ENDPOINT1', 'GET', '404'), ('ENDPOINT1', 'GET', '500'), ('ENDPOINT1', 'POST', '200'), ('ENDPOINT1', 'POST', '404'), ('ENDPOINT1', 'POST', '500'), ('ENDPOINT2', 'GET', '200'), ('ENDPOINT2', 'GET', '404'), ('ENDPOINT2', 'GET', '500'), ('ENDPOINT2', 'POST', '200'), ('ENDPOINT2', 'POST', '404'), ('ENDPOINT2', 'POST', '500')] |
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 collection import Counter | |
from collection import OrderedDict | |
Counter('luppolo') | |
Counter({'p': 2, 'l': 2, 'o': 2, 'u': 1}) | |
help(Counter) | |
| Method resolution order: | |
| Counter | |
| __builtin__.dict | |
| __builtin__.object |