作为文件系统实现静态字典的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如果你仔细观察的话会有冲突。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何访问Struts2中的请求参数?   java将大图像设置为背景   java如何使用子查询。在里面   java我希望我的计算器重新启动,直到我按下“q”字母   将Java Http服务器的响应写入jQuery   java JPA/HSQLDB仍然在消耗我所有的内存   使用MySql结果集数据在Java Swing应用程序中创建图形和图表   java创建一个程序,从用户那里获取输入并将其写入输出文件   java Windows无法删除具有大路径的文件   java方法运行它3次   java如何仅为Http状态代码500配置RetryTemplate?   java读取,从JAR写入外部xml文件   如何在安卓 java中实现自定义导航栏,如图所示   java使用ApacheCamel框架将数据持久化为Oracle DB中的BLOB/CLOB数据类型