Python使用numpy序列化哈希表

I have a custom class InMemoryStorage(BaseStorage) defined as

class InMemoryStorage(BaseStorage):
    def __init__(self, config):
        self.name = 'dict'
        self.storage = dict()

    def keys(self):
        return self.storage.keys()

    def set_val(self, key, val):
        self.storage[key] = val

    def get_val(self, key):
        return self.storage[key]

    def append_val(self, key, val):
        self.storage.setdefault(key, []).append(val)

    def get_list(self, key):
        return self.storage.get(key, [])

that extends a "abstract" class class BaseStorage(object). This class holds a storage dict object:

self.storage = dict()

I store in this dict numpy ndarray and serialize to file:

np.savez_compressed(self.hashtable_filename,
                    *self.hash_tables)

要存储的对象值是类似的元组

((10, 12, 99, 1, 5, 31, 2, 3), 'vec1')

being the hash key a computed integer (based on the vector to store) like key 111110.

Before writing the hash table to file via savez_compressed, my hash looks like:

for i, table in enumerate(self.hash_tables):
                    print(table)
                    for key in table.keys():
                        val = table.get_val(key)
                        print(key,val)

<lshash.storage.InMemoryStorage object at 0x10bfd4d90>
100010 [((10, 12, 99, 1, 5, 31, 2, 3), 'vec1'), ((10, 11, 94, 1, 4, 31, 2, 3), 'vec2')]
<lshash.storage.InMemoryStorage object at 0x10bfd4c90>
111000 [((10, 12, 99, 1, 5, 31, 2, 3), 'vec1'), ((10, 11, 94, 1, 4, 31, 2, 3), 'vec2')]

但是,如果我尝试重新加载值,似乎它没有以正确的方式保存:

npzfiles = np.load(self.hashtable_filename)
npzfiles = sorted(npzfiles.items(), key=lambda x: x[0])
hash_tables = [t[1] for t in npzfiles]

for i, table in enumerate(hash_tables):
      print("stored",table)
      for key in table.keys():
      val = table.get_val(key)
          print(key,val)

In fact I get a numpy.ndarray object rather than the InMemoryStorage that I should have serialized:

stored <lshash.storage.InMemoryStorage object at 0x110dba210>
Traceback (most recent call last):
  File "example.py", line 46, in <module>
    lsh.save()
  File "/Users/loretoparisi/Documents/MyProjects/lshash/lshash/lshash.py", line 281, in save
    for key in table.keys():
AttributeError: 'numpy.ndarray' object has no attribute 'keys'

thus giving an error when trying to access key() method for the InMemoryStorage object instance.

The code to reproduce this error is here. Just install and run example.py to get the error.

评论