Skip to content

Instantly share code, notes, and snippets.

View josiahcarlson's full-sized avatar

Josiah Carlson josiahcarlson

View GitHub Profile
@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 / search_output.txt
Last active February 14, 2016 07:37
A new ordering on an array that offers faster searching than binary search, yet is just as simple. Allows for O(log(n)) modifications and searches. Paper and details forthcoming.
$ gcc -O4 -o shuffle_search.out shuffle_search.c
$ ./shuffle_search.out
Generate 10000000 random input values...
Select 1000000 random items from the input and duplicating 10x times...
Random reorder selected items
Sorting input... qsort O(nlogn)
Shuffle transforming input... O(n)
Binary searching
3.932018
@josiahcarlson
josiahcarlson / reply_to_blog.txt
Created February 19, 2016 04:29
Reply to a comment
This is my reply to Alex Mills: http://www.binpress.com/tutorial/introduction-to-rate-limiting-with-redis/155
Yes, with threads, processes, or an async reactor. That said...
$ redis-benchmark -n 2000000 -c 20 -q
[snip]
SET: 115141.05 requests per second
GET: 134048.27 requests per second
INCR: 180505.41 requests per second
LPUSH: 176056.33 requests per second
# Josiah Carlson - Programming Challange from Erann Gat:
# http://www.flownet.com/ron/papers/lisp-java/
# Given a list of words and a list of phone numbers, find all the ways that
# each phone number can be expressed as a list of words.
from collections import defaultdict
import sys
MAPPING = {'e':0, 'j':1, 'n':1, 'q':1, 'r':2, 'w':2, 'x':2, 'd':3, 's':3,
'y':3, 'f':4, 't':4, 'a':5, 'm':5, 'c':6, 'i':6, 'v':6, 'b':7, 'k':7,
@josiahcarlson
josiahcarlson / poly1305.py
Created January 24, 2011 00:51
Imeplementation of DJB's Poly1305-AES algorithm
'''
Implementation of Poly1305-AES as described by Daniel J. Bernstein in
documents linked from: http://cr.yp.to/mac.html
Implemented by Josiah Carlson <josiah.carlson@gmail.com> on 2011-01-23,
released into the public domain.
Note: this implementation of Poly1305-AES uses Python's built-in long integer
implementation, so is not terribly performant, and likely suffers from a
side-channel attack related to the timing of bigint modulo. It also uses
@josiahcarlson
josiahcarlson / redis_simple_chat.py
Created June 24, 2011 22:07
A way of implementing a poll-based chat inside Redis
'''
redis_simple_chat.py
Written June 24, 2011 by Josiah Carlson
Released under the GNU GPL v2
available: http://www.gnu.org/licenses/gpl-2.0.html
Other licenses may be available upon request.
'''
Copyright 2010 Josiah Carlson
Released into the public domain
copy_redis.py
A convenient utility function for copying data from one redis server to
another.
Requires http://github.com/andymccurdy/redis-py .
@josiahcarlson
josiahcarlson / rom_example_bruno.py
Last active August 8, 2019 04:47
Example from a livecoding stream, updated with fixes
'''
This this was retyped from https://www.twitch.tv/videos/459202303 for notes and
then updated by the author of rom, me, Josiah Carlson
Notes:
* For created_at, or any default, if it is "callable", it will be called at
entity creation/instantiation. In the demo in the video, we see:
created_at = rom.DateTime(default=datetime.datetime.now())
@josiahcarlson
josiahcarlson / async_http_client.py
Created December 25, 2011 23:24
Simple asyncore/asynchat derived http client
'''async_http_client.py
Originally written December 25, 2011 by Josiah Carlson
Released into the public domain.
This simple asynchat.async_chat subclass offers the ability to connect to http
servers to download files. The example download_file() function shows how to
use the class to download files from a remote webserver, automatically
handling redirects and errors.
@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.