Skip to content

Instantly share code, notes, and snippets.

View samuraisam's full-sized avatar

Samuel Sutch samuraisam

View GitHub Profile
@samuraisam
samuraisam / redis_store.py
Created October 6, 2011 16:59
Piston Redis OAuth Store
import hashlib
import redis
from django.db import DEFAULT_DB_ALIAS
from django.db.models import Q, signals as db_signals
from piston.models import Consumer, Token
from piston.store import DataStore as PistonDataStore
from mirrio.api._redis import redis
redis = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB)
@samuraisam
samuraisam / gist:1562600
Created January 4, 2012 22:40
Adding to a counter column
// adding to a potentially non-existent counter column
Mutator<String> m2 = HFactory.createMutator(ServiceDescriptor.keyspace(), StringSerializer.get());
m2.addCounter(indexRowKey, "streamidx", HFactory.createCounterColumn(indexColKey, 1, StringSerializer.get()));
@samuraisam
samuraisam / gist:1562901
Created January 4, 2012 23:49
java.lang.IncompatibleClassChangeError: Implementing class
Mutator<String> m1 = HFactory.createMutator(ServiceDescriptor.keyspace(), StringSerializer.get());
m1.addInsertion(seriesRowKey, "StreamTS", HFactory.createColumn(id, item.getType().getName(),
UUIDSerializer.get(), StringSerializer.get()));
m1.execute(); // incompatible class change error
@samuraisam
samuraisam / model_cache.py
Created May 4, 2012 19:30
Django Model Cache and Cached Piston OAuth Store
# The author disclaims copyright to this source code. In place of a legal
# notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
import hashlib, cPickle as pickle, logging
from django.db.models import signals as db_signals
from django.core.cache import cache
@samuraisam
samuraisam / authentication.py
Created May 7, 2012 05:26
An OAuth store for Piston that will cache your consumers and tokens
from django.db.models import Q
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend
from piston.authentication import (OAuthAuthentication as
PistonOAuthAuthentication, send_oauth_error)
from piston import oauth
from model_cache import ModelCache
user_cache = ModelCache(User)
@samuraisam
samuraisam / parse_accept.py
Created May 16, 2012 21:43
Parse an accept header in Python w/ support for vendor types
# The author disclaims copyright to this source code. In place of a legal
# notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
# It is based on a snipped found in this project:
# https://github.com/martinblech/mimerender
@samuraisam
samuraisam / series_handler.py
Created May 22, 2012 21:44
An example of what the entity framework will be like
from lenny.handler import BaseHandler
from lightt.api.db.series import Series
class SeriesHandler(BaseHandler):
"""
Documentation that will be generated for /series
"""
name = 'series' # urls can be automatically generated
version = 1.2 # version is selected via Accept header
@samuraisam
samuraisam / lc.py
Created May 30, 2012 21:33
Recursive line count
#!/usr/bin/env python
"""Counts all the lines from a directory inward."""
import os
import os.path
import sys
lines = 0
@samuraisam
samuraisam / newrelic_pycassa_trace.py
Created June 8, 2012 21:56
Adds function-level tracing to pycassa
import newrelic.api.function_trace
def instrument_pycassa_connection(module):
newrelic.api.function_trace.wrap_function_trace(
module, 'Connection.__init__')
_cf_methods = ['xget', 'get', 'get_indexed_slices', 'multiget', 'get_count',
'multiget_count', 'get_range', 'insert', 'batch_insert',
'add', 'remove', 'remove_counter', 'truncate']
@samuraisam
samuraisam / wordlist.py
Created June 11, 2012 22:53
A wordlist for python. 67k words, with "bad words" filtered out. Use responsibly :)
This file has been truncated, but you can view the full file.
import random
def random_words(num, separator='-'):
"""
Return `num`-random concatinated to each other.
They will be joined by `separator`
"""
words = []