Skip to content

Instantly share code, notes, and snippets.

View dcramer's full-sized avatar
💭
I may be slow to respond.

David Cramer dcramer

💭
I may be slow to respond.
View GitHub Profile
"""
raven.contrib.django.views
~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import itertools
from django.conf import settings
django-debug-toolbar==0.9.4
django-data-tools==0.1
django-devserver==0.3
nashvegas==0.8-disqus-6
Sphinx>=1.0.7
piplint==0.1.1
amqplib==1.0.2
anyjson==0.3.1
argparse==1.1
@dcramer
dcramer / gist:3298424
Created August 8, 2012 20:33
Server side cursors in Django
#1: Connection method
with connections['default'].sscursor():
Foo.objects.all()
^^ this sucks because we make assumptions about where Foo routes
#2: QuerySet method
Foo.objects.all().sscursor()
@mock.patch('modeldict.base.time')
def test_switch_creation(self, time_mod):
self.mydict['hello'] = 'foo'
self.assertEquals(self.cache.set.call_count, 2)
time_mod.time.return_value = 1344893511
print time_mod.mock_calls
print self.cache.set.mock_calls
self.cache.set.assert_any_call(self.mydict.cache_key, {u'hello': u'foo'})
self.cache.set.assert_any_call(self.mydict.last_updated_cache_key, time_mod.time.return_value)
@dcramer
dcramer / gist:3898601
Created October 16, 2012 10:40
pmp Concept

Goal

Implement a capped pool which will start dropping messages once it reaches <size>. This will have a much lower rate of failure (due to load) than a database or standard queue.

For example, if there is a burst of data coming in, we'd first stick it into the pool, and then fire off a job to the queue (the job would be argless and simple say "get data from pool", it could also eventually be replaced with a continuous processor). This guarantees that your queue isnt overflowing with large amounts of data, but rather potential no-ops (just like our buffer implementation), and will also ensure you don't

"""
sentry.pool.redis
~~~~~~~~~~~~~~~~~
:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import random
from nydus.db import create_cluster
@dcramer
dcramer / signals.py
Last active December 10, 2015 16:49
Signals with decorator syntax in Django
from functools import wraps
from django.dispatch import Signal
class BetterSignal(Signal):
def connect(self, receiver=None, **kwargs):
"""
Support decorator syntax:
>>> @signal.connect(sender=type)
@dcramer
dcramer / heapqueue.py
Last active December 11, 2015 04:09
Refined Heap Queue for Python (including capped and max heap implementation)
import heapq
from threading import Lock
class HeapQueue(object):
def __init__(self, values=None, maxsize=None, reversed=False):
"""
Create a new heap queue.
- ``maxsize`` will create a capped queue.
@dcramer
dcramer / gist:4569278
Created January 18, 2013 22:36
XPath / JSON / Sentry filters concept
The gist is, you give a path expression (xpath-like), a condition, and a action.
----
Scrub all local vars in a stackframe where the key matches something like a password:
Path:
//sentry.interfaces.Stacktrace/frames[*]/vars/key
Condition: