Skip to content

Instantly share code, notes, and snippets.

View djfroofy's full-sized avatar

Drew Smathers djfroofy

  • Pro Solutions Llc.
  • Seattle, WA
View GitHub Profile
from djredis.models import DredisMixin
import djredis.fields
class Blog(models.Model, DredisMixin): # inherit from the mixin class
author = models.ForeignKey('Author')
title = models.CharField(max_length=200)
viewcount = djredis.fields.Counter()
# optionally add a unique keyspace for the instance - default is shown below
def redis_key(self):
def switchProtocol(currProto, newProto):
currentData = currProto.recvd
currProto.recvd = ''
newProto.makeConnection(currProto.transport)
if currentData:
newProto.dataReceived(currentData)
@djfroofy
djfroofy / gist:988833
Created May 24, 2011 14:42
Traceback after trying PUT in txyoga
web.Server Traceback (most recent call last):
<type 'exceptions.AttributeError'>: Request instance has no attribute 'body'
/Users/dsmathers/Envs/scrubbed/lib/python2.6/site-packages/Twisted-11.0.0_r31757-py2.6-macosx-10.6-universal.egg/twisted/web/server.py, line 126 in process
124 self.postpath = map(unquote, string.split(self.path[1:], '/'))
125 try:
126 resrc = self.site.getResourceFor(self)
127 self.render(resrc)
/Users/dsmathers/Envs/scrubbed/lib/python2.6/site-packages/Twisted-11.0.0_r31757-py2.6-macosx-10.6-universal.egg/twisted/web/server.py, line 542 in getResourceFor
540 # servers and disconnected sites.
@djfroofy
djfroofy / noway.py
Created July 15, 2011 14:36
Making an iterable from __getitem__
# http://lucumr.pocoo.org/2011/7/9/python-and-pola/
class NoWay(object):
def __getitem__(self, key):
if key == 0:
return 'a'
if key == 1:
return 'b'
if key == 'apple':
@djfroofy
djfroofy / quantumhealing.py
Created August 3, 2011 16:03
BL comp - Quantum Healing
from comps.core import *
player_rw = rw([0,0,0,0,R(5,0),5,0,5,0,0,0,5,17,5,0,0,12,24,29,24,5,5,0,0,0,0,0,5,17,5,5,0,0])
def mod_player():
next = player_rw.next()
player.noteFactory.amount = next
blakmaparada = Instrument('sf2/string/blakmaparada.sf2')
player = Player(blakmaparada, Adder(OrderedArp([53,55,60,57])), OrderedArp([127,100,120,90]), interval=0.5)
@djfroofy
djfroofy / pylog_service.py
Created November 25, 2011 20:50
A Twisted Multiservice that sets ILogObserver
from twisted.python.log import ILogObserver, PythonLoggingObserver
from twisted.application import service
class PythonLoggingMultiService(service.MultiService):
usePythonLogging = True
def setServiceParent(self, parent):
service.MultiService.setServiceParent(self, parent)
if self.usePythonLogging:
@djfroofy
djfroofy / txparallalsearch.py
Created February 16, 2012 00:05
parallelSearch with inlineCallbacks
@inlineCallbacks
def parallelSearch(keywords):
out = []
for key in keywords:
out.append((yield search(k)))
returnValue(out)
@djfroofy
djfroofy / amortize.lua
Created May 28, 2012 16:22
LUA Mortgage payment calculator
#!/usr/bin/env lua
function monthly_payment(principal, apr, years)
local i = apr / 12
local n = years * 12
return principal * (i + (i / ((1 + i)^n - 1)))
end
local principal = tonumber(arg[1])
local apr = tonumber(arg[2]) / 100
@djfroofy
djfroofy / performance_results.txt
Created August 4, 2012 17:15
Lists vs. StringIO vs. Regular String Concat for building strings in Python
IPython 0.10.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [2]: from string_building_perf import *
In [3]: %timeit build_ul_list(100000)
10 loops, best of 3: 77.9 ms per loop
@djfroofy
djfroofy / ipython-pypy-1.9.out
Created August 12, 2012 14:00
Tupling a generator vs. list comping
pypy-1.9 Python 2.7.2
In [1]: g = xrange(10000)
In [2]: %timeit tuple(i for i in g)
1000 loops, best of 3: 659 us per loop
In [3]: %timeit [i for i in g]
10000 loops, best of 3: 77.9 us per loop