Skip to content
Related Articles

Related Articles

NLP | Storing an ordered dictionary in Redis

Improve Article
Save Article
  • Last Updated : 12 Jun, 2019
Improve Article
Save Article

An ordered dictionary is like a normal dict, but the keys are ordered by an ordering function. In the case of Redis, it supports ordered dictionaries whose keys are strings and whose values are floating point scores. This structure can come in handy in cases where information gain has to be calculated and all the words and scores have to be stored for later use.
The RedisOrderedDict class in extends collections. MutableMapping to get a number of dict compatible methods for free. Then, it implements all the key methods that require Redis ordered set (also known as Zset) commands:
Code: Storing a frequency distribution in Redis.

class RedisOrderedDict(collections.MutableMapping):
    def __init__(self, r, name):
        self._r = r
        self._name = encode_key(name)
    def __iter__(self):
        return iter(self.items())
    def __len__(self):
        return self._r.zcard(self._name)
    def __getitem__(self, key):
        return self._r.zscore(self._name, encode_key(key))
    def __setitem__(self, key, score):
        self._r.zadd(self._name, encode_key(key), score)
    def __delitem__(self, key):
        self._r.zrem(self._name, encode_key(key))
    def keys(self, start = 0, end =-1):
        # we use zrevrange to get keys sorted
        # by high value instead of by
        return self._r.zrevrange(self._name, start, end)
    def values(self, start = 0, end =-1):
        return [v for (k, v) in self.items(start = start, end = end)]
    def items(self, start = 0, end =-1):
        return self._r.zrevrange(self._name, start, end, withscores = True)
    def get(self, key, default = 0):
        return self[key] or default
    def iteritems(self):
        return iter(self)
    def clear(self):

Code : Creating an instance of RedisOrderedDict by passing in a Redis connection and a unique name

from redis import Redis
from rediscollections import RedisOrderedDict
r = Redis()
rod = RedisOrderedDict(r, 'test')
rod['bar'] = 5.2
print (rod['bar'])
print (len(rod))
print (rod.items()) 


[(b'bar', 5.2)]

Much of the code may look similar to the RedisHashMap, which is to be expected since they both extend collections.MutableMapping. The main difference here is that RedisOrderedSet orders keys by floating point values, and so it is not suited for arbitrary key-value storage like the RedisHashMap.
Outline explaining each key method and how they work with Redis:

  • __len__(): This uses the zcard command to get the number of elements in the ordered set.
  • __getitem__(): This uses the zscore command to get the score of a key, and returns 0 if the key does not exist.
  • __setitem__(): This uses the zadd command to add a key to the ordered set with the given score, or updates the score if the key already exists.
  • __delitem__(): This uses the zrem command to remove a key from the ordered set.
  • keys(): This uses the zrevrange command to get all the keys in the ordered set, sorted by the highest score. It takes two optional keyword arguments, start and end, to more efficiently get a slice of the ordered keys.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!