Skip to content

Instantly share code, notes, and snippets.

View paxan's full-sized avatar

Pavel Repin paxan

  • aws.amazon.com
  • Seattle, WA
View GitHub Profile
@paxan
paxan / lang.py
Created November 16, 2012 19:30
__all__ is well :)
from __future__ import absolute_import
import inspect
from contextlib import contextmanager
class PublicDecorator(object):
"""
The global instance of this class, public, provides the mechanism
for managing __all__ list, thus liberating developers from
error-prone maintenance of __all__ by hand.
@paxan
paxan / example.py
Created October 24, 2012 19:25
Example: stream oriented parsing of XML (pretty hacky!)
# Remixed from: http://www.dabeaz.com/coroutines/cosax.py
# Don't shy away from reading http://www.dabeaz.com/coroutines/ if the stuff below
# seems super weird.
from xml.sax import ContentHandler, parse
from collections import namedtuple
ElementStart = namedtuple('ElementStart', 'name attrs')
ElementEnd = namedtuple('ElementEnd', 'name')
@paxan
paxan / gist:3901864
Last active October 11, 2015 18:37
A Common Crawl Experiment (moved to https://github.com/paxan/ccooo)
@paxan
paxan / snippet.md
Created January 27, 2012 22:06
JSON-oriented Flask Application

Let's say you are creating a RESTful web service that typically sees JSON requests and responds with JSON back. When things go wrong, default errors that Flask/Werkzeug respond with are all HTML. Which breaks the clients who expect JSON back even in case of errors.

Here's an approach to mitigate this. All errors that Werkzeug may throw are now intercepted and converted into JSON response. You can customize what goes into the response by tweaking the line with response = jsonify(...).

Also note that make_json_error will be used when your code throws an arbitrary exception (e.g. division by zero) not derived from HTTPException.

@paxan
paxan / git-grep.el
Created September 2, 2010 20:17 — forked from offby1/git-grep.el
;; There's something similar (but fancier) in vc-git.el: vc-git-grep
;; -I means don't search through binary files
(defcustom git-grep-switches "--extended-regexp -I -n --ignore-case"
"Switches to pass to `git grep'."
:type 'string)
(defcustom git-grep-default-work-tree (expand-file-name "~/Documents/src/kits")
"Top of your favorite git working tree. \\[git-grep] will search from here if it cannot figure out where else to look."
:type 'directory
)
#!/usr/bin/env python
import paste.httpserver
form = """\
<html>
<body>
<form method="post" enctype="multipart/form-data" action="/">
<input type="file" name="photopicker" />
<input type="submit" />