public
Last active

python sets and tags in redis

  • Download Gist
redis_sets_tags.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# Adapted from: http://rediscookbook.org/implement_tags_and_search_them.html
import redis
 
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
red = redis.Redis(connection_pool=pool)
 
red.set('book:1', {'title': 'Diving into Python', 'author': 'Mark Pilgrim'})
red.set('book:2', {'title': 'Programing Erlang', 'author': 'Joe Armstrong'})
red.set('book:3', {'title': 'Programing in Haskell', 'author': 'Graham Hutton'})
 
red.sadd('tag:python', 1)
red.sadd('tag:erlang', 2)
red.sadd('tag:haskell', 3)
red.sadd('tag:programming', 1)
red.sadd('tag:programming', 2)
red.sadd('tag:programming', 3)
red.sadd('tag:computing', 1)
red.sadd('tag:computing', 2)
red.sadd('tag:computing', 3)
red.sadd('tag:distributedcomputing', 2)
red.sadd('tag:FP', 2)
red.sadd('tag:FP', 3)
 
# a) SINTER 'tag:erlang' 'tag:haskell'
# 0 results
red.sinter('tag:erlang', 'tag:haskell')
 
# b) SINTER 'tag:programming' 'tag:computing'
# 3 results: 1, 2, 3
red.sinter('tag:programming', 'tag:computing')
 
# c) SUNION 'tag:erlang' 'tag:haskell'
# 2 results: 2 and 3
red.sunion('tag:erlang', 'tag:haskell')
 
# d) SDIFF 'tag:programming' 'tag:haskell'
# 2 results: 1 and 2 (haskell is excluded)
red.sdiff('tag:programming', 'tag:haskell')
 
red.smembers('tag:programming')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.