View async.py
import asyncio, time
async def do_nothing():
await asyncio.sleep(1)
futures = []
for i in range(9999):
futures.append(asyncio.ensure_future(do_nothing()))
loop = asyncio.get_event_loop()
View get_pex.sh
cd `mktemp -d`
virtualenv .
source bin/activate
pip install pex
mkdir -p ~/bin
pex pex -c pex -o ~/bin/pex
deactivate
cd ~
View fad.py
>>> from collections import namedtuple
>>> d = {'one': 1, 'two': 2}
>>> frozen_attr_dict = namedtuple('frozen_attr_dict', d.keys())
>>> f_d = frozen_attr_dict(**d)
>>> f_d
frozen_attr_dict(two=2, one=1)
View reduce.py
class Reduce(object):
def __init__(self, default_dicts):
self.seed = default_dicts[0]
self.remainder = default_dicts[1:]
def merge(self, a, b):
for key in b:
a[key] += b[key]
def reduce(self):
View proxy.py
class Foo(object):
def __init__(self, pos):
self.pos = pos
def bar(self):
print(self.pos)
class ProxiedFoo(object):
proxied = None
View app.py
from flask import Flask
application = Flask(__name__)
@application.route('/')
def hello_world():
return 'Hello, World!'
View git workflow
git checkout master
git fetch upstream
git rebase upstream/master
git push # to make remote master on GitHub the same as upstream
git checkout -b my-new-shiny-branch
# work on it
git push -u origin my-new-shiny-branch
# some time passes and someone commits to upstream/master
git checkout master
git fetch upstream
View reify.py
import six
import types
def _reify(self):
""" This function exists to be bound to the mutatable class """
if self.reify:
# mutate the instance to become a new class
instance = AlternateClass()
View swap_in_sys_and_path.py
import sys
import copy
from contextlib import contextmanager
orig_sys_modules = copy.copy(sys.modules)
orig_path = copy.copy(sys.path)
View instance_attrs.py
import logging
logging.basicConfig(level=logging.INFO)
def instance_hijacker(fn):
def use_inst_attr(inst, *args, **kwargs):
inst.logger.info('hi from the decorator!')
return fn(inst, *args, **kwargs)
return use_inst_attr