Skip to content

Instantly share code, notes, and snippets.

View josiahcarlson's full-sized avatar

Josiah Carlson josiahcarlson

View GitHub Profile
@josiahcarlson
josiahcarlson / chunked_server_test.py
Last active March 20, 2024 20:30
Use some standard Python libraries to implement a chunked-transfer encoding web server with partially-working gzip support
'''
chunked_server_test.py
Copyright August 3, 2012
Released into the public domain
This implements a chunked server using Python threads and the built-in
BaseHTTPServer module. Enable gzip compression at your own peril - web
browsers seem to have issues, though wget, curl, Python's urllib2, my own
async_http library, and other command-line tools have no problems.
@josiahcarlson
josiahcarlson / divide_maze.py
Created April 5, 2011 22:06
Recursive Division algorithm for maze generation
'''
This is an implementation of the Recursive Division algorithm for maze
generation, primarily derived from the algorithm presented here:
http://weblog.jamisbuck.org/2011/1/12/maze-generation-recursive-division-algorithm
The input grid can be generated via:
grid = [width*[0] for i in xrange(height)]
The initial call should be of the form:
divide(grid, 0, 0, width, height)
@josiahcarlson
josiahcarlson / one_bit_difference.py
Created October 3, 2023 03:22
Find all 1-bit differences among provided integers.
"""
one_bit_difference.py
Written September / October 2023 by Josiah Carlson - josiah.carlson@gmail.com
Released into the public domain. Please include this authorship note / reference
to this GIST if you use / derive this software. Thank you.
Been thinking about this algorithm for about 17 years, haven't seen it around.
I might not know the proper literature search to find relevant references, or if
this was already done.
-- keys: key dest
-- argv: score member [score member ...]
-- e.g. keys: "data:test:1:2" "data-sum:test:1:2"
-- argv: 10 a 15 b
local key = KEYS[1]
local dest = KEYS[2]
local sum = tonumber(redis.call('get', dest)) or 0
for i=1, #ARGV, 2 do
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 4. in line 1.
$ time docker-compose run redis-server numactl -N 1 -l /usr/src/redis/src/redis-benchmark --crc 512000000 --csv --combine
algorithm,buffer,performance,crc64_matches
crc_1byte,512000000,400,1
crcspeed,512000000,1354,1
crcdual,512000000,2129,1
crctri,512000000,2243,1
crc_1byte,312000000,401,1
crcspeed,312000000,1352,1
crcdual,312000000,2090,1
crctri,312000000,2213,1
'''
redis_search.py
Written by Josiah Carlson July 3, 2010
Released into the public domain.
This module implements a simple TF/IDF indexing and search algorithm using
Redis as a datastore server. The particular algorithm implemented uses the
@josiahcarlson
josiahcarlson / goto.py
Created April 1, 2012 17:04
Decorators for adding goto/switch semantics to Python
'''
This code is derived from: http://code.activestate.com/recipes/576944/
It was released under the MIT license.
I (Josiah Carlson) have modified it to be correct in more cases, and to report
errors in some error cases.
@josiahcarlson
josiahcarlson / pass_socket.py
Created September 14, 2012 18:05
An example of passing a socket between processes using Python's multiprocessing library
'''pass_socket.py
Written September 14, 2012
Released into the public domain.
Works on Python 2.6, 2.7, and may need minor changes for 3+.
'''
import multiprocessing
@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 / rate_limit2.py
Last active April 17, 2022 09:22
Regular and sliding window rate limiting to accompany two blog posts.
'''
rate_limit2.py
Copyright 2014, Josiah Carlson - josiah.carlson@gmail.com
Released under the MIT license
This module intends to show how to perform standard and sliding-window rate
limits as a companion to the two articles posted on Binpress entitled
"Introduction to rate limiting with Redis", parts 1 and 2: