Skip to content

Instantly share code, notes, and snippets.

View luhn's full-sized avatar

Theron Luhn luhn

View GitHub Profile
@luhn
luhn / stats.py
Created May 9, 2012 23:50
CDF and PDF without SciPy
#Taken from http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html
def pdf(x):
"""Normal probably distribution function."""
return exp(-x**2/2)/sqrt(2*pi)
#Taken from http://stackoverflow.com/a/809402/600247
def erfcc(x):
"""Complementary error function."""
z = abs(x)
t = 1. / (1. + 0.5*z)
@luhn
luhn / sqlalchemy.py
Created July 10, 2012 18:19
SQLAlchemy pool_recylce
sqlalchemy.create_engine(settings['db_url'], pool_recycle=14400)
@reify
def db(self):
def cleanup(self):
self.db.close()
self.add_finished_callback(cleanup)
return self.registry.settings['sessionmaker']()
#!/bin/sh
set -e
touch /tmp/linked
ln -s /tmp/linked . || true
cat <<EOF > Dockerfile
FROM busybox:ubuntu-14.04
ADD . /data
array = [1, 2, 3, 4]
array.collect! do |n|
n ** 2
end
puts array.inspect # => [1, 4, 9, 16]
from twisted.python.failure import Failure
from autobahn.wamp import WampServerProtocol
class WampException(Exception):
"""A base class for an application exception. Can be subclass and
define a unique msg."""
msg = None
def __init__(self, *args):
if len(args) == 0:
@luhn
luhn / delete.py
Last active January 1, 2016 04:09
Pyramid cookies
request.response.set_cookie('my_cookie', None)
# OR
request.response.unset_cookie('my_cookie')
@luhn
luhn / query.json
Created January 13, 2016 17:19
Elasticsearch query
{
"query": {
"filtered": {
"filter": {
"and": [
{
"term": {
"deleted": false
}
},
(function() {
// Inspired by http://informationandvisualization.de/blog/box-plot
d3.box = function() {
var width = 1,
height = 1,
duration = 0,
domain = null,
value = Number;
showLabels = true, // whether or not to show text labels
@luhn
luhn / memoize.py
Last active January 15, 2017 16:19
Memoize method decorator
import functools
class memoize_method:
"""
A decorator to memoize a method. The cache is stored as a dictionary
inside the object.
This functionality is similar to stdlib's :func:`functools.lru_cache`.
When using lru_cache on a method, the object will be used as part of the