Skip to content

Instantly share code, notes, and snippets.

@mik3y
mik3y / throttle.py
Last active December 18, 2019 22:11
DRF Counter-backed throttle
from django.core.cache import cache as default_cache
from rest_framework.throttling import BaseThrottle
from rest_framework.settings import api_settings
import logging
logger = logging.getLogger('throttle')
def parse_rate(rate):
"""
Given the request rate string, return a two tuple of:
@mik3y
mik3y / celery.py
Last active August 29, 2015 14:00
Celery Beat scheduler backed by Redis
from __future__ import absolute_import
"""Celery beat scheduler backed by Redis.
The schedule will be saved as a pickled data in the key
'celery:beat:<filename>', where filename is the schedule filename
configured in celery
Prerequisite:
You are using Redis as your broker (BROKER_URL = 'redis://...')
@mik3y
mik3y / simple_tenant_mapper.py
Last active December 19, 2015 12:59
django-db-multitenant simple mapper example
"""Maps a request to a tenant using the first part of the hostname.
For example:
foo.example.com:8000 -> foo
bar.baz.example.com -> bar
This is a simple example; you should probably verify tenant names
are valid before returning them, since the returned tenant name will
be issued in a `USE` SQL query.
"""
@mik3y
mik3y / redis_tenant_mapper.py
Last active May 3, 2017 06:24
django-db-multitenant redis mapper example
"""Maps a request to a tenant using Redis.
Redis should store a map of hostname -> tenant name. The tenant name
is then used to form the database and cache names.
"""
from db_multitenant import mapper
from django.http import Http404
import redis
@mik3y
mik3y / update-drinks.py
Created April 18, 2012 04:57
Update pykeg drinks
# Manually reset the volume of all drinks in a session.
# You should run this by hand in "kegbot-admin.py shell"
# Be sure to run "kegbot-admin.py kb_regen_stats" after doing so.
ML_PER_TICK = 1.0/2200 # change me
SESSION_ID = 123 # change me
# Should not need to edit past here.
from pykeg.core import models