Skip to content

Instantly share code, notes, and snippets.

View jdunck's full-sized avatar

Jeremy Dunck jdunck

View GitHub Profile
@jdunck
jdunck / redis_leaky_bucket.py
Created November 17, 2012 08:16
leaky bucket queue - redis 2.6 + lua + python
#cribbed from http://vimeo.com/52569901 (Twilio carrier call origination moderation)
# The idea is that many fan-in queues can enqueue at any rate, but
# dequeue needs to happen in a rate-controlled manner without allowing
# any individual input queue to starve other queues.
# http://en.wikipedia.org/wiki/Leaky_bucket (second sense, "This version is referred to here as the leaky bucket as a queue.")
#
# requires:
# redis 2.6+
# redis-py>=2.7.0
# anyjson
@jdunck
jdunck / static_fallback_open.py
Last active August 13, 2019 14:17
Get the contents of a django static file, regardless of whether it has been collected
import posixpath
from django.contrib.staticfiles import finders
from django.contrib.staticfiles.storage import staticfiles_storage
def static_fallback_open(static_path, mode='r'):
writing = 'w' in mode or 'a' in mode
if writing or staticfiles_storage.exists(static_path):
return staticfiles_storage.open(static_path, mode)
@jdunck
jdunck / s3_multipart.py
Last active May 15, 2019 13:55 — forked from BradWhittington/s3_multipart.py
upload to s3 with streaming, multi-part, threaded upload, with key rollover as you pass the 4gb limit, with adjustable buffering and pooling. Don't forget to call uploader.close() when you're done.
from multiprocessing.pool import ThreadPool
import logging, os, threading
from StringIO import StringIO
import boto.s3
logger = logging.getLogger('s3upload')
class MultiPartUploader:
upload_part = 0
@jdunck
jdunck / example.html
Created March 17, 2019 18:26
crawl stub
<html>
<body>
<img src="foo"/>
<img src="bar"/>
</body>
</html>
@jdunck
jdunck / heap.py
Created March 13, 2019 04:44
Min/Max heap for python
import heapq
class Heap(object):
def __init__(self, max=False):
self.max = max
self.data = []
@property
def delta(self):
return -1 if self.max else 1
def push(self, item):
@jdunck
jdunck / warn_on_path_overlap.py
Created March 6, 2011 06:58
Python wart remover: avoid overlapping sys.path which results in duplicate imports.
import sys, os.path, logging
try:
import sysconfig
except ImportError:
sysconfig = None
def warn_on_path_overlap():
"""
@jdunck
jdunck / README.md
Last active August 17, 2018 20:29
Python preamble ordering

It's been hard for me to remember what the required relative order of python preambles (that is, encoding markers, future imports, and module docstrings) should be.

This is an empirical approach, showing that the proper order is:

# -*- coding: utf8 -*-

"""døcst®îñg"""

from __future__ import unicode_literals
@jdunck
jdunck / circle.yml.fragment
Last active January 19, 2018 16:16
py.test parallel execution for CircleCI
test:
override:
- find . -maxdepth 1 -type d -not -name . > test_dirs:
parallel: true
- python circle_node_to_dirs.py < test_dirs > test_dirs_for_node:
parallel: true
- echo "Testing `cat test_dirs_for_node`":
parallel: true
- py.test `cat test_dirs_for_node`:
parallel: true
@jdunck
jdunck / chunked_bulk_create.py
Last active January 17, 2017 23:23
Chunked bulk_create under mysql
import math
from itertools import izip_longest
from django.db import connections
def get_cursor(alias='default'):
"""
Get a raw cursor out of django's DB connection
"""
wrapped_conn = connections[alias]
@jdunck
jdunck / Example.sh
Created September 28, 2016 20:14
Handmade CJK coverage util
$ ls -1 font-repo/
HelveticaNeue.ttf
LucidaGrande.ttc
meiryo.ttc
ヒラギノ丸ゴ ProN W4.ttc
ヒラギノ角ゴシック W8.ttc
$ python handmade-cjk.py
num desired: 23216
num unsupported: 6640
extras (supported by not desired): 6563