Skip to content

Instantly share code, notes, and snippets.

View mmalone's full-sized avatar

Michael Malone mmalone

View GitHub Profile
from django.core.management.base import BaseCommand, CommandError
from django.db.models.loading import get_apps
from django.conf import settings
import simpledb
class Command(BaseCommand):
help = ("Sync all of the SimpleDB domains.")
def handle(self, *args, **options):
class FriendDescriptor(object):
"""
A descriptor that provides access to a user's friends.
"""
def __get__(self, instance, cls):
if not instance:
raise AttributeError('Manager must be accessed via instance')
Relationship = get_model('friends', 'Relationship')
class FollowerDescriptor(object):
"""
A descriptor which provides access to a user's followers.
"""
def __get__(self, instance, cls):
if not instance:
raise AttributeError('Manager must be accessed via instance')
class RelationshipManager(models.Manager):
def get_query_set(self):
@mmalone
mmalone / gist:179410
Created September 1, 2009 21:30 — forked from joestump/gist:179397
class Model(object):
keyspace = None
column_family = None
key = None
data = {}
required = []
modified = False
def __init__(self, **kwargs):
if self.key in kwargs and len(kwargs) == 1:
@mmalone
mmalone / models.py
Created September 3, 2009 01:05
hateoasis
import datetime
from django.db import models
class Frob(models.Model):
name = models.CharField(max_length=255)
created = models.DateTimeField(default=datetime.datetime.now)
class Fritz(models.Model):
age = models.IntegerField(default=5)
"""
Some simple utilities to read the magic bytes from the beginning of a
file and determine whether the file meets certain criteria (e.g., contains
JPEG image data).
"""
import array
from operator import eq
IMAGE_MAGIC_DATA = (
from django.core.paginator import ObjectPaginator
class QuickObjectPaginator(ObjectPaginator):
max_safe_pages = 0
def __init__(self, object_list, per_page, orphans=0, max_safe_pages=0):
self.max_safe_pages = max_safe_pages
super(QuickObjectPaginator, self).__init__(object_list, per_page, orphans)
def validate_page_number(self, page_number):
@mmalone
mmalone / Reviewing profiles
Created November 13, 2009 23:57
Replacement Django runserver command that does profiling... because I've rewritten this too many times.
>>> import pstats
>>> p = pstats.Stats('p.1258156459.52174278XcQE.prof')
>>> p.strip_dirs().sort_stats(-1).print_stats(5)
Fri Nov 13 15:54:20 2009 p.1258156459.52174278XcQE.prof
124278 function calls (122386 primitive calls) in 0.589 CPU seconds
Ordered by: standard name
List reduced from 1014 to 5 due to restriction <5>
"Memcached cache backend"
from django.core.cache.backends import memcached
from django.utils.encoding import smart_unicode, smart_str
MIN_COMPRESS_LEN = 150000
class CacheClass(memcached.CacheClass):
def add(self, key, value, timeout=None, min_compress_len=MIN_COMPRESS_LEN):
if isinstance(value, unicode):
@mmalone
mmalone / readutf.java
Created February 24, 2010 22:10
Java vs. Python
/**
* Reads from the
* stream <code>in</code> a representation
* of a Unicode character string encoded in
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
* this string of characters is then returned as a <code>String</code>.
* The details of the modified UTF-8 representation
* are exactly the same as for the <code>readUTF</code>
* method of <code>DataInput</code>.
*