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
#!/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" />
@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
)
@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 / gist:3901864
Last active October 11, 2015 18:37
A Common Crawl Experiment (moved to https://github.com/paxan/ccooo)
@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 / 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 / mylogger.py
Created December 25, 2012 23:31
Try "gunicorn --logger-class mylogger.MyLogger mylogger:app" for both cases: Exhibit A and Exhibit B. Also observe that when you do "python mylogger.py" you will get "AttributeError: 'module' object has no attribute 'glogging'" in the case of Exhibit A.
if True:
import gunicorn # Exhibit A
else:
import gunicorn.glogging # Exhibit B
class MyLogger(gunicorn.glogging.Logger):
pass
def app(environ, start_response):
data = "Hello, World!\n"
@paxan
paxan / .screenrc
Created January 29, 2013 18:46
.screenrc by @offby1
# -*-conf-space-*-
# This screenrc file was obtained as-is from Pavel's friend
# Eric Hanchrow (https://github.com/offby1). It just works!
# Typical screen installations use Control-A to access screen
# functions, but this one uses Control-Z. As far as I know,
# it's the same as a typical screen setup in every other way.
# This seems to interfere least with Emacs
escape ^Zz
@paxan
paxan / README.md
Last active December 11, 2015 23:28
A script that launches OS X FileMerge to inspect Git diffs

Intro

Sometimes you may want a better way to inspect/review Git diffs than what GitX and other tools provide (which is basically a glorified pretty printed linear view of diff hunks).

If you're on a Mac, try ["gdiff" shell script.][1] It's a wrapper that launches OS X FileMerge application to inspect diffs.

Installation

Just place gdiff somewhere in your PATH, chmod +x it too.

Usage

You should invoke gdiff just like git diff from inside a repo directory.

@paxan
paxan / gist:4982896
Last active December 13, 2015 22:09
This snippet of fenced Clojure code breaks Jekyll. Related GH issue: https://github.com/mojombo/jekyll/issues/814
---
layout: post
title: Boom!
category: posts
---
# Repro
Here's a code example: