Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Created September 5, 2011 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftriplett/1196204 to your computer and use it in GitHub Desktop.
Save jefftriplett/1196204 to your computer and use it in GitHub Desktop.
python sets and tags in redis
# 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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment