Skip to content

Instantly share code, notes, and snippets.

View josiahcarlson's full-sized avatar

Josiah Carlson josiahcarlson

View GitHub Profile
@josiahcarlson
josiahcarlson / redis_sentinel.py
Created May 12, 2012 20:28
Redis sentinel monitor assignments
# A proposed method for choosing which slaves to monitor within the
# Redis HA sentinel monitors
def select_slaves_to_monitor(slave_info, all_sentinels, my_id, my_slaves):
'''
slave_info = [
('ip_or_host:port', ['sentinel-id, ...]),
...
]
@josiahcarlson
josiahcarlson / refresh_lock.py
Created October 15, 2012 05:36
A way to refresh a lock from chapter 6 of Redis in Action
'''A function to refresh a lock that can timeout, before it times out.'''
def refresh_lock(conn, lockname, identifier, lock_timeout=10):
pipe = conn.pipeline(True)
lockname = 'lock:' + lockname
while True:
try:
pipe.watch(lockname)
if pipe.get(lockname) == identifier:
@josiahcarlson
josiahcarlson / parallel_mget.py
Created December 7, 2012 15:26
A function to do parallel pipelined mget calls to a group of shards
'''
Written December 7, 2012 by Josiah Carlson
Released into the public domain.
Untested.
'''
from binascii import crc32
@josiahcarlson
josiahcarlson / refresh_lock.py
Created April 24, 2013 15:57
Refreshing a lock defined in chapter 6 of Redis in Action
'''
Answer to this thread:
http://www.manning-sandbox.com/thread.jspa?messageID=140363
'''
def refresh_lock(conn, lockname, identifier, timeout=10):
pipe = conn.pipeline(True)
lockname = 'lock:' + lockname
@josiahcarlson
josiahcarlson / prefix_code.py
Created May 6, 2013 16:29
Encode/decode sequences of integers to offer the ability to easily compare encoded sequences of integers as though they were decoded (which can be used to implement array comparisons in a database that doesn't offer native arrays).
'''
prefix_code.py
Copyright 2012-2013 Josiah Carlson
Released under the GNU LGPL v 2.1 license
This module offers the ability to encode/decode a sequence of integers into
strings that can then be used to compare sequences of integers (or paths on
trees) quickly. This module was originally intended to be used in a case-
preserving index in a relational database (where 'Z' comes before 'a', as is
@josiahcarlson
josiahcarlson / steg.py
Last active December 17, 2015 05:39
Steganography in Python
# -*- coding: utf-8 -*-
'''
Steganogrphy in Python
Copyright 2013 Josiah Carlson
Released under the GNU LGPL v2.1 license
What, how, why, etc, are discussed:
http://www.dr-josiah.com/2013/05/steganography-in-python.html
'''
This is a module that defines some helper classes and functions for
expiring groups of related keys at the same time.
Written July 1-2, 2013 by Josiah Carlson
Released into the public domain
'''
import time
@josiahcarlson
josiahcarlson / many_to_many.py
Created October 17, 2013 22:21
An example of building a many-to-many relationship with rom.
import rom
class ModelA(rom.Model):
m2m_test = rom.OneToMany('HiddenModel')
@property
def b_models(self):
return [x.model_b for x in self.m2m_test]
@josiahcarlson
josiahcarlson / test_intersect_performance.py
Created November 1, 2013 19:32
This tests example Redis intersection vs. a manual Lua intersection. Released into the public domain.
import time
import redis
def _script_load(script):
'''
Borrowed from my book, Redis in Action:
https://github.com/josiahcarlson/redis-in-action/blob/master/python/ch11_listing_source.py
'''
@josiahcarlson
josiahcarlson / null_session.py
Created November 23, 2013 07:00
Doesn't record objects in the session, you would need to explicitly call `session.save()` in order to save any entities. Also, if an entity falls out of all scopes, it is deallocated and you need to fetch from Redis again.
'''
The Josiah Carlson recommended way of replacing the session object. This code
should be run prior to any subsequent imports/uses of the session object.
Released into the public domain.
'''
from rom import util