作为文件系统实现静态字典的python包。
py-filestore的Python项目详细描述
py文件存储
这是一个简单的包,用来创建一个类似于作为一组文件存在的字典的静态数据结构。
如果您使用了1.1.0之前的任何早期版本,则应在最后几次更新更改了项在索引中的保存方式和散列方式后进行更新。
示例用法
首先,使用pip下载它:
pip install py-filestore
然后:
fromfilestoreimportFilestoreimportrequests# You can pass your preferred encoding in:# store = Filestore(encoding='ascii') # It defaults to utf-8# You can also force overwriting on population with# store = Filestore(overwrite=True)store=Filestore()# You can treat this just like a python dictionary!# You can populate it in two ways:store['a']=(1,2,3)store.append(('b',"The alphabet is pretty cool"))store[1]=[13.23,321.0]# You can store any data that is supported by python's own# pickle package. store['res']=requests.get("https://api.github.com/")# And if you have data that is not supported, you can write# and set your own serializer and deserializer for it.print(store)# {'a': '(1, 2, 3)', 'b': 'The alphabet is pretty cool', 'res': '<Response [200]>', 1: [13.23, 321.0]}# You can get items out too!alpha=store['a']print(alpha)# (1, 2, 3)print(alpha[1])# 2# The dictionary is saved to file under the directory ./.store# which means you can close the session and return for it later# as long as the working directory is the same when the class is initialized.# However, this leaves residue on the file system. We can clean that up too!store.clean_up()# All the saved data is gone now.
工作原理:
创建类的实例将在系统上创建一个包含所有给定信息的目录。创建索引文件以允许在不同会话上重新填充。
当数据被添加到filestore时,该类使用一个名为FNV-1a的原始非加密散列算法(32位)散列密钥。此哈希成为文件名。如果发生碰撞,现在可以进行处理!
实际数据用python的pickle序列化,然后在写入磁盘之前编码为base64。如果数据与pickle不兼容,则可以在插入或删除数据之前使用store.set_serializer(my_serializer_function)
和store.set_deserializer(my_deserializer_function)
编写和分配自己的序列化程序/反序列化程序。
测试
目前,testing.py文件并不全面,它会测试特定长度的随机字符串以防止冲突。它可能需要一些工作,但它确实证明了FNV-1a如果你仔细观察的话会有冲突。