Skip to content

Instantly share code, notes, and snippets.

@tolitius
Created February 27, 2012 06:20
Show Gist options
  • Save tolitius/1921881 to your computer and use it in GitHub Desktop.
Save tolitius/1921881 to your computer and use it in GitHub Desktop.
Redis 'zunionstore' example for Chariot Day
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.zadd('votes:east', **{'riak': 1, 'couchdb': 1, 'cassandra': 1})
r.zadd('votes:north', **{'redis': 1, 'onetick': 1, 'couchdb': 1})
r.zadd('votes:west', **{'redis': 1, 'riak': 1, 'couchdb': 1})
r.zadd('votes:south', **{'voltdb': 1, 'mongodb': 1, 'hazelcast': 1})
r.zadd('votes:north:east', **{'hazelcast': 1, 'riak': 1, 'redis': 1})
r.zadd('votes:north:west', **{'redis': 1, 'cassandra': 1, 'onetick': 1})
r.zunionstore('topdogs', ['votes:east', 'votes:north', 'votes:west', 'votes:south', 'votes:north:east', 'votes:south:west'])
print "\nand the top dogs are..."
print "-----------------------"
# iterate here simply to print them vertically, otherwise can simply do:
# print r.zrevrange('topdogs', 0, -1, 'withscores')
for topdog in r.zrevrange('topdogs', 0, -1, 'withscores'):
print topdog
"""
and the top dogs are...
-----------------------
('redis', 4.0)
('riak', 3.0)
('couchdb', 3.0)
('onetick', 2.0)
('hazelcast', 2.0)
('cassandra', 2.0)
('voltdb', 1.0)
('mongodb', 1.0)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment