Skip to content

Instantly share code, notes, and snippets.

View erikrose's full-sized avatar

Erik Rose erikrose

View GitHub Profile
@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
@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:
# 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
@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,
@erikrose
erikrose / gist:1090980
Created July 18, 2011 23:55
Regex to turn Sphinx method and class refs into normal reST, for PyPI display
"""This is how I include a dumbed-down copy of the ordinarily Sphinxified version history in my PyPI README.rst in django-tidings."""
re.sub(r':[a-zA-Z]+:`[0-9a-zA-Z~_\.]+\.([^`]+)`',
r'``\1``',
open('docs/changes.rst').read())
@erikrose
erikrose / wrap_comment.py
Last active September 23, 2015 14:27
Comment-wrapping filter for BBEdit 10
"""BBEdit UNIX filter that wraps one or more optionally-indented, commented
lines to 79 chars, preserving indentation.
Just select all the lines (in their entirety) the comment spans, and invoke.
Works with #, --, and //-style comments.
For example, this... ::
# Upping this to 10000 makes it 3x faster. 10000 takes 15.936s. 5000 takes 16.303s.