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 / with-open-seq.clj
Last active August 29, 2015 13:56
with-open-seq
(require '[clojure.core.async :refer [chan close! go >! <!!]])
(defmacro with-open-seq
"Like with-open, but only closes resources when
the sequence generated by the body is exhausted."
[bindings & body]
`(let [c# (chan)]
(go
(try
(with-open ~bindings
@paxan
paxan / librato.go
Created December 5, 2014 23:55
HACK: Librato measurement sender
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"regexp"
@paxan
paxan / demo
Last active August 29, 2015 14:19
try-let
```
> (try-let [x (/ 5 1) y (/ 5 2)] [x y])
[5 5/2]
> (try-let [x (/ 5 1) y (/ 5 0)] [x y])
#<ArithmeticException java.lang.ArithmeticException: Divide by zero>
```
@paxan
paxan / runtime-metrics.clj
Created May 23, 2015 17:40
boot.clj Heroku runtime metrics
{:time #inst "2015-05-23T16:45:35.294Z", :from "run.6221", :text "source=run.6221 sample#memory_total=7.62MB sample#memory_rss=0.66MB sample#memory_cache=6.96MB sample#memory_swap=0.00MB sample#memory_pgpgin=2512pages sample#memory_pgpgout=561pages"}
{:time #inst "2015-05-23T16:45:56.995Z", :from "run.6221", :text "source=run.6221 sample#memory_total=259.27MB sample#memory_rss=220.50MB sample#memory_cache=38.77MB sample#memory_swap=0.00MB sample#memory_pgpgin=91599pages sample#memory_pgpgout=25225pages"}
{:time #inst "2015-05-23T16:46:19.082Z", :from "run.6221", :text "source=run.6221 sample#load_avg_1m=1.36"}
{:time #inst "2015-05-23T16:46:19.082Z", :from "run.6221", :text "source=run.6221 sample#memory_total=627.97MB sample#memory_rss=481.32MB sample#memory_cache=7.44MB sample#memory_swap=139.21MB sample#memory_pgpgin=208632pages sample#memory_pgpgout=83508pages"}
{:time #inst "2015-05-23T16:46:19.083Z", :from "run.6221", :text "Process running mem=627M(122.7%)"}
{:time #inst "2015-05-23T16:46:19.083Z", :fr
@paxan
paxan / dynamic-jar-loading
Created June 12, 2015 23:38
Example of loading a local jar file dynamically using boot-clj
#!/usr/bin/env boot
;; -*- mode: clojure -*-
(set-env! :dependencies '[[org.apache.hadoop/hadoop-common "2.4.0"]
[org.apache.pig/pig "0.12.0"
:exclusions [commons-logging
commons-net
net.java.dev.jets3t/jets3t
org.slf4j/slf4j-api
tomcat/jasper-compiler
#!/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')