pixelogik / NearPy

Python framework for fast (approximated) nearest neighbour search in large, high-dimensional data sets using different locality-sensitive hashes.
MIT License
759 stars 152 forks source link

Fix load_hash_configuration() to handle non-existent hash config #32

Closed toogle closed 9 years ago

toogle commented 9 years ago

Currently an exception occurs when we trying to get a non-existent hash configuration from the storage (no matters Redis or memory), e.g.

>>> redis_object = Redis(host='redis', port=6379, db=0)
>>> redis_storage = RedisStorage(redis_object)
>>> config = redis_storage.load_hash_configuration('MyHash')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nearpy/storage/storage_redis.py", line 157, in load_hash_configuration
    return pickle.loads(self.redis_object.get(hash_name+'_conf'))
  File "/usr/lib/python2.7/pickle.py", line 1381, in loads
    file = StringIO(str)
TypeError: expected read buffer, NoneType found
>>> memory_storage = MemoryStorage()
>>> config = memory_storage.load_hash_configuration('MyHash')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nearpy/storage/storage_memory.py", line 83, in load_hash_configuration
    return self.hash_configs[hash_name]
KeyError: 'MyHash'

This PR fixes such behavior and adds additional tests.

pixelogik commented 9 years ago

Also great! Thx!