Skip to content

Instantly share code, notes, and snippets.

View josiahcarlson's full-sized avatar

Josiah Carlson josiahcarlson

View GitHub Profile
@josiahcarlson
josiahcarlson / follow_file.py
Created April 13, 2014 18:03
Python function to offer "tail -F"-like functionality in Python
'''
follow_file.py
Written April 13, 2014 by Josiah Carlson
Released under the MIT license.
This could likely be made more efficient with the use of the pyinotify library,
and some of the module-level constants could be tuned for better performance,
depending on your platform.
@josiahcarlson
josiahcarlson / zunion_range_score.py
Last active August 29, 2015 13:57
Zunion Range Score, an interesting scripting solution.
'''
Written on March 28, 2014 by Josiah Carlson
Released into the public domain
ZUNIONRANGESCORE:
Zunion Range Score performs a Redis ZUNIONSTORE operation, selecting *only
those items in the provided ranges. The scores are added. Proof of concept.
Warning: untested, use at your own risk.
@josiahcarlson
josiahcarlson / int_packing.py
Last active January 3, 2016 11:59
Pack large integers into doubles, and extract large integers from doubles.
'''
Written October 26, 2012 by Josiah Carlson
Released into the public domain.
I'm sure this has been done before, but I've not seen any code for it.
This code will take an integer with absolute value of up to 2**63-2**53
and pack it losslessly into an IEEE 753 FP double, preserving sort order
by reusing the exponent field for high order bits.
Note that if you keep your values under 2**62, we only use standard fp
@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
@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 / sort_zset_cols.py
Last active August 23, 2022 08:15
A method to get sql-like multiple-column order by in Redis
'''
sort_zset_cols.py
Copyright 2013 Josiah Carlson
Released into the public domain.
'''
'''
Let's imagine that there are 3 restaurants with price, score, distance info
being:
@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]
'''
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 / 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
@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