Skip to content

Instantly share code, notes, and snippets.

View luhn's full-sized avatar

Theron Luhn luhn

View GitHub Profile
@luhn
luhn / gist:2381814
Created April 14, 2012 03:11
Generic Getters and Setters
class Foo(object):
def setter(var):
def set(self, value):
setattr(self, var, value+' unicorn')
return set
def getter(var):
def get(self):
return getattr(self, var)+' sasquatch'
@luhn
luhn / comprehensions.py
Last active October 3, 2015 09:37
Python one-liner
>>> [ range(x*5, x*5+5) for x in range(5) ]
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]
@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']()
@luhn
luhn / getter_and_setter.py
Created November 29, 2012 18:35
Using getters and setters with SQLAlchemy
class Table(Base):
id = Column(Integer, primary_key=True)
_name = Column('name', String(24))
@property
def name(self):
return self._name;
@name.setter
def name(self, value):
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')
#!/bin/sh
### BEGIN INIT INFO
# Provides: haproxy
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: fast and reliable load balancing reverse proxy
# Description: This file should be used to start and stop haproxy.
### END INIT INFO