Skip to content

Instantly share code, notes, and snippets.

View slacy's full-sized avatar

Steve Lacy slacy

  • Mountain View, CA
View GitHub Profile
@slacy
slacy / dynamic_form.py
Created January 24, 2011 21:43
Django Dynamic Form Choices
class CreateItemForm(forms.Form):
categories = forms.MultipleChoiceField(
# Must fill in choices and initial from the DB at runtime
widget=forms.CheckboxSelectMultiple(),
required=False,
)
def __init__(self, *args, **kwargs):
user = kwargs['user']
del kwargs['user']
@slacy
slacy / session.py
Created March 9, 2011 19:41
Pyramid sessions object implemented on top of MongoDB using minimongo.
import datetime
import time
import random
import logging
from minimongo.model import Model, MongoCollection
from pyramid.interfaces import ISession
from zope.interface import implements
from pyramid.response import Response
@slacy
slacy / pip_shell_session
Created April 5, 2011 00:37
Crazy pip shell session. Note how I explicitly ask for 1.9 and yet I get 1.10. ugh!
(env)slacy@slacy-sunfire:~/src/pvn2$ pip install pymongo==1.9
Downloading/unpacking pymongo==1.9
Running setup.py egg_info for package pymongo
Installing collected packages: pymongo
Running setup.py install for pymongo
Successfully installed pymongo
Cleaning up...
(env)slacy@slacy-sunfire:~/src/pvn2$ python -c "import pymongo; print pymongo.version"
1.10
@slacy
slacy / gist:2770786
Created May 22, 2012 18:29
Kombu vs. boto
import boto
from kombu import BrokerConnection, Queue
def main():
queue = Queue("steve-test-kombu")
with BrokerConnection('sqs://<KEY_ID>@<ACCESS_KEY>:80/') as connection:
connection.connect()
$ dd if=/dev/zero of=./small.ext4 bs=1000000 count=100
$ dd if=/dev/zero of=./medium bs=1000000 count=50
$ mkfs.ext4 ./small.ext4
$ sudo mkdir /mnt/tmp
$ sudo mount -o loop ./small.ext4 /mnt/tmp
$ sudo chmod a+rxw /mnt/tmp
$ sftp localhost
sftp> put medium /mnt/tmp/a
sftp> put medium /mnt/tmp/b
sftp> put medium /mnt/tmp/b
@slacy
slacy / test_rudementary.py
Created October 9, 2012 20:42
Rudimentary Twisted Trial Unit test that hangs
from twisted.trial import unittest
from twisted.internet.defer import Deferred
class TestRudimentary(unittest.TestCase):
# This test case succeeds, but hangs at the end of the invocation when running through nosetests.
# How can this be fixed?
def test_rudimentary(self):
def done(_ignored):
#include <iostream>
#include "math.h"
template<int n> class Field {};
template<typename B, typename T>
class Member : public B {
public:
Member(T initial) { value_ = initial; }
T value() { return value_; }
@slacy
slacy / .bash_prompt.sh
Created June 12, 2012 22:42
git supercharged bash prompt
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
reset='\[\e[00m\]'
bold='\[\e[01m\]'
red='\[\e[31m\]'
green='\[\e[32m\]'
orange='\[\e[33m\]'
blue='\[\e[34m\]'
purple='\[\e[35m\]'
@slacy
slacy / models.py
Created September 4, 2012 21:56
Django Model mixin for pushing signal handling back into instance methods.
class SignalModel(models.Model):
"""A Model mixin class that lets you put your signal handler methods back into your Model class."""
@classmethod
def _sig_pre_delete(cls, instance, *args, **kwargs):
"""dispatch the pre_delete method to a regular instance method. """
return instance.sig_pre_delete(*args, **kwargs)
@classmethod
def _sig_post_delete(cls, instance, *args, **kwargs):
@slacy
slacy / mock_str_test.py
Created October 26, 2012 22:14
Broken code for trying to test that __str__() is called on a MagicMock instance.
"""
Example code that illustrates how un-intuitive it is to assert that the __str__() method of a MagicMock
instance was called. I'm not even sure how to git this test code to work correctly.
"""
import mock
from mock import call
def main():