Skip to content

Instantly share code, notes, and snippets.

View luhn's full-sized avatar

Theron Luhn luhn

View GitHub Profile
@luhn
luhn / db.py
Created October 15, 2014 19:32
Psycopg2 Connection extension
import psycopg2.extensions
class Connection(psycopg2.extensions.connection):
"""
An extension of Psycopg's connection class, to give us some additional
utilties.
"""
@luhn
luhn / Vagrantfile
Created July 22, 2014 15:55
Twisted #4539 failure
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
@luhn
luhn / connect.py
Created July 18, 2014 15:55
txpostgres connection function (With automatic reconnect)
from psycopg2.extras import DictCursor
from txpostgres import txpostgres
from txpostgres.reconnection import DeadConnectionDetector
def db_pool_from_config(config):
"""
Create a connection pool. Returns a deferred that will fire when the
connection pool is ready.
"""
@luhn
luhn / haproxy.initd
Last active August 29, 2015 13:56
Install HAProxy 1.5 in Ubuntu
#!/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
#!/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
@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')
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:
array = [1, 2, 3, 4]
array.collect! do |n|
n ** 2
end
puts array.inspect # => [1, 4, 9, 16]
@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):
@reify
def db(self):
def cleanup(self):
self.db.close()
self.add_finished_callback(cleanup)
return self.registry.settings['sessionmaker']()