I bought an iPad, then I read this sobering commentary. Hmmm.
In other news, I'm heading to the first PyCon AU in a few weeks to talk about giant robots and meet new people.
Saturday, May 29, 2010
Monday, May 24, 2010
Tuesday, May 11, 2010
Watching the house burn down...
Apple's new TOS actually effects peoples livelihoods. This developer considers a move to Android. I suspect many Unity3D shops will do the same.
This will have the net effect that all the best games will now run on Android, and not on the iPhone or iPad. Silly Apple.
This will have the net effect that all the best games will now run on Android, and not on the iPhone or iPad. Silly Apple.
Tuesday, May 04, 2010
Are you going to PyCon.au 2010?
PyCon.au is happening. Are you going?
I know we have plenty of Python afficandos here in Perth, so what are you waiting for? Sign Up!
I know we have plenty of Python afficandos here in Perth, so what are you waiting for? Sign Up!
Monday, May 03, 2010
Consistent Hashing in Python, Redux.
import hashlib
import bisect
class ConsistentHash(object):
def __init__(self, D=5):
self.keyspace = []
self.D = D
def partition(self, arg):
md5 = hashlib.md5(str(arg)).hexdigest()
return int(md5[:4], 16)
def add(self, hash):
for i in xrange(self.D):
k = self.partition("%s.%s"%(hash,i))
bisect.insort(self.keyspace, (k, hash))
def remove(self, hash):
self.keyspace = [i for i in self.keyspace if i[1] != hash]
def __getitem__(self, i):
return self.keyspace[i%len(self.keyspace)][1]
def hash(self, key):
p = self.partition(key)
i = bisect.bisect_left(self.keyspace, (p,None))
return self[i-1]
Consistent Hashing in Python
Update: This is not really a consistent hash. I'll fix the issues then repost. (Fixed here.)
Consistent Hashing is useful. I'm using it to build a Redis client with support for redundancy, and failover / node insertion.
Testing with the below code produces this beautiful graph. Perfect. :-) If you want to add more weight to a particular hash (server address) just add it more than once.
Consistent Hashing is useful. I'm using it to build a Redis client with support for redundancy, and failover / node insertion.
import hashlib
class ConsistentHash(object):
def __init__(self, partition_size=2):
self.partition_size = partition_size
self.hashes = []
def partition(self, arg):
md5 = hashlib.md5(str(arg)).hexdigest()
partition = int(md5[:self.partition_size], 16)
return partition
def add(self, hash):
self.hashes.append(hash)
self.hashes.sort(cmp=
lambda a,b: cmp(self.partition(a), self.partition(b)))
def __getitem__(self, i):
return self.hashes[i%len(self.hashes)]
def hash(self, key):
i = self.partition(key) % len(self.hashes)
return self[i], self[i+1], self[i+2]
Testing with the below code produces this beautiful graph. Perfect. :-) If you want to add more weight to a particular hash (server address) just add it more than once.
if __name__ == "__main__":
from pygooglechart import PieChart2D
c = ConsistentHash()
keys = "ABCDEFG"
print len(keys)
for k in keys:
c.add(k)
d = {}
for i in xrange(10000):
h = c.hash(i)[0]
count = d.get(h, 0)
d[h] = count + 1
chart = PieChart2D(128, 128)
chart.add_data([d[i] for i in keys])
print chart.get_url()
Subscribe to:
Posts (Atom)
Popular Posts
-
These are the robots I've been working on for the last 12 months. They each weigh about 11 tonnes and have a 17 meter reach. The control...
-
So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...
-
Often, when building a game, you need to test if objects are colliding. The objects could be spaceships, rocks, mouse pointers, laser beams....
-
Summary: NodeJS wins. Test Program ab -n 10000 -c 5 http://localhost/ Gevent Code from gevent import wsgi class WebServer(object): def a...
-
Why would I ask that question? Python 3 has been available for some time now, yet uptake is slow. There aren't a whole lot of packages i...
-
MiddleMan: A Pub/Sub and Request/Response server in Go. This is my first Go project. It is a rewrite of an existing Python server, based o...
-
I've just finished refactoring an awful C# class. I had been delaying the job for a while because I didn't want to do it. Then, whil...
-
Dear Lazyweb. Imagine a nice RESTful interface for working with Tags. The URL: /tags/ will return a list of all the tags. The URL: /tags/fo...
-
It is about 8 degrees C this morning. So cold, especially when last week we had high twenties. To help solve the problem, a friend suggeste...
-
We're almost done with our Giant Robot Project. Realising we had only a few hours before the robots would be deconstructed and sent awa...