Skip to content

Instantly share code, notes, and snippets.

View erikrose's full-sized avatar

Erik Rose erikrose

View GitHub Profile
def do_stuff(thing):
config = {
2: {'message': "Flibbety jibbet!"},
4: {'message': "A!\nFlibbety jibbet!"},
5: {'message': "B!\nFlibbety jibbet!"},
7: {'message': "C!", 'run_funcs': False},
}
if thing not in config:
return
print config[thing]['message']
def do_stuff(thing):
if thing == 2:
print "Flibbety jibbet!"
render_golfclubs()
sneeze_loudly(True)
do_other_stuff()
if thing == 4:
print "A!"
print "Flibbety jibbet!"
@erikrose
erikrose / parse_css.py
Created May 21, 2013 23:40
A quick and dirty CSS parser using Parsimonious
from parsimonious import Grammar
from parsimonious.nodes import NodeVisitor
# Adapted from https://raw.github.com/bhyde/css-parser/master/css.peg:
# This is an incredibly inefficient grammar, but it would be simple to
# optimize. Eventually, Parsimonious will know how to do that automatically.
css = Grammar(r"""
program = S* stylesheet
digit = ~"[0123456789]"
@erikrose
erikrose / gist:5603537
Last active December 17, 2015 11:39
Where Happens When Firefox Crashes? A talk at The Fifth Elephant, Bangaluru, India, July 2013
- A sense of scale
- A Firefox crash is 150K.
- 50 crashes/second. 3M/day.
- 110 TB in HDFS
- 500 GB in PostgreSQL
- 120 boxes. Real hardware, not VMs.
- Challenges
- Dictum: “Never lose a crash.”
- Upside-down user story: few users, much data
- Hither and thither
# Not strictly necessary but should save a lot of RewriteConds and fstats:
RewriteRule ^/search$ - [L]
RewriteRule ^$ - [L]
RewriteRule ^/static($|/.*) - [L]
RewriteRule ^.*/$ - [L] # Don't muck with the front page or any directory-browing page.
# Append ".html" to files in the tree but not to ones that exist as requested...
RewriteCond /home/vagrant/dxr/tests/test_basic/target/trees%{REQUEST_FILENAME} !-f
# ...and not to folders:
RewriteCond /home/vagrant/dxr/tests/test_basic/target/trees%{REQUEST_FILENAME} !-d
@dxr_blueprint.route('/<path:tree_and_path>')
def browse(tree_and_path):
"""Show a directory listing or a single file from one of the trees."""
tree, _, path = tree_and_path.partition('/')
tree_folder = os.path.join(current_app.instance_path, 'trees', tree)
if isdir(os.path.join(tree_folder, path)):
# It's a bare directory. Add the index file to the end:
path = os.path.join(path, current_app.config['DIRECTORY_INDEX'])
else:
@erikrose
erikrose / gist:4221142
Created December 6, 2012 01:30
Gets DXR running.
# First, install everything via vagrant.
# Then...
mkdir ~/bin
cd ~/bin
wget http://pypi.python.org/packages/source/p/python-hglib/python-hglib-0.2.tar.gz
tar -xzf python-hglib-0.2.tar.gz
cd python-hglib-0.2
sudo python setup.py install
@erikrose
erikrose / gist:4019721
Created November 5, 2012 19:18
RPN iterator
from collections import deque
from more_itertools import consumer
OPS = {'+': lambda a, b: b + a,
'/': lambda a, b: b / a,
'-': lambda a, b: b - a,
'*': lambda a, b: b * a}
@erikrose
erikrose / gist:3835820
Created October 4, 2012 19:23
Fluent, Fluid APIs (for Confoo)

Designing a good API is an art, but there are reasons behind the feelings and enumerable rules of thumb that can save costly missteps. Come on an adventure through compactness, orthogonality, consistency, safety, coupling, state handling, layering, and more, illustrated with shining examples (and gruesome mistakes) from popular Python libraries—and walk away with an actionable design plan.

Details

Here's a rough outline that will certainly grow and develop before the actual talk.

  • What is an API? Everything is.
  • Even if you're not going to expose it publicly, every routine,
- [ ] Rising standards of repeatability:
- [ ] pip freeze
- [ ] This gives you the basics, defending you against buggy or
incompatible upstream updates.
- [ ] Be sure to include dependencies! Install with --no-deps
and run your test suite to make sure it works.
- [ ] hashes
- [ ] This defends you against package replacement (which PyPI
doesn't allow anymore but other indexes might), indexes
getting hacked, and MITM attacks. (HTTPS also contributes