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
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
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 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
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
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
def fibonacci(n): | |
x = 0 | |
y = 1 | |
for i in range(n): | |
print x | |
tmp = y | |
y = x + y | |
x = tmp | |
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
d = defaults.copy() | |
d.update(os.environ) | |
d.update(command_line_arguments) | |
# PYTHON3 ONLY!!! | |
from collections import ChainMap | |
d = ChainMap(command_line_arguments, os.environ, defaults) | |
# it's view, not a real dictionary. if you want something equivalent to a new one | |
d = ChainMap({}, command_line_arguments, os.environ, defaults) |
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
colors = ['green', 'blue', 'green', 'red', 'blue', 'green'] | |
d = {} | |
for color in colors: | |
if color not in d: | |
d[color] = 0 | |
d[color] += 1 | |
# slighlty better |
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
d = {} | |
for name in names: | |
key = len(name) | |
if key not in d: | |
d[key] = [] | |
d[key].append(name) | |
# slightly better | |
d = {} |