Skip to content

Instantly share code, notes, and snippets.

@jdp
jdp / redis-movekeys.sh
Created August 22, 2013 22:24
Move keys matching pattern from one database to another
#!/bin/sh
#
# Usage: ./redis-movekeys.sh [-h host] [-p port] [-n src] [-m dest] pattern
#
# Move keys matching pattern from the src Redis database to the
# dest Redis database.
set -e
HOST="localhost"
@jdp
jdp / redis-delkeys.sh
Created August 21, 2013 19:19
Delete keys matching a pattern from Redis
#!/bin/sh
#
# Usage: ./redis-delkeys.sh [-h host] [-p port] [-n db] pattern
#
# Matches keys with the KEYS command matching pattern
# and deletes them from the specified Redis DB.
set -e
HOST="localhost"
@jdp
jdp / loglog.go
Last active December 20, 2015 07:48
LogLog and HyperLogLog estimators in Golang
package main
import (
// "encoding/binary"
"crypto/rand"
"fmt"
"hash"
"hash/fnv"
"io"
"io/ioutil"
@jdp
jdp / vial.py
Last active December 17, 2015 10:59
prototype for Vial, a small web framework
import re
from werkzeug.exceptions import NotFound, MethodNotAllowed
from werkzeug.wrappers import Request, Response
class RouteNode(object):
@property
def converter(self):
return None
@jdp
jdp / countmin.py
Created April 7, 2013 15:49
example count-min sketch implementation
import random
class CountMinSketch(object):
def __init__(self, w, d, p):
self.w = w
self.d = d
self.p = p
self.C = [[0] * self.w for _ in range(self.d)]
self.a = [random.randint(1, self.p) for _ in range(self.d)]
@jdp
jdp / loglog.py
Created April 5, 2013 06:26
example LogLog and HyperLogLog cardinality estimators
import math
import struct
def rank(num):
if num == 0:
return 32
p = 0
while (num >> p) & 1 == 0:
p += 1
@jdp
jdp / fenwick.py
Last active June 28, 2019 17:01
Fenwick Range Tree (Binary Indexed Tree) on top of Redis
import sys
from calendar import timegm
from collections import Counter, defaultdict
from datetime import datetime, timedelta
import redis
class DictBackend(object):
def __init__(self, maximum=None):
@jdp
jdp / sdbquery.py
Created January 12, 2013 21:10
Quick DSL for building SimpleDB queries
"""
SimpleDB Query DSL
>>> stmt = select(Star(), 'mydomain')
>>> stmt.where(Field('city') == 'Seattle').to_sql()
'select * from mydomain where city = "Seattle"'
>>> city = Field('city')
>>> stmt.where((city == 'Seattle') | (city == 'Portland')).to_sql()
'select * from mydomain where (city = "Seattle") or (city = "Portland")'
@jdp
jdp / dungeons.py
Created December 25, 2012 19:09
Simple implementation of Zelda dungeons in Python
def floor(size=10):
"""Return a list of rooms represented by dictionaries.
The list is procedurally generated. Each room is represented by a ``dict``
with ``'x'``, ``'y'``, and ``'exits'`` keys.
>>> floor(5)[0]
{'x': 0, 'y': 0, 'exits': {'up': {...}, 'right': {...}}}
"""
@jdp
jdp / searchindex.py
Created November 25, 2012 00:06
Autocomplete search with Redis and Python
from functools import partial
from itertools import imap, izip, product
from redis import Redis
class SearchIndex(object):
"""Autocomplete search index.
>>> index = SearchIndex(Redis())