作为文件系统实现静态字典的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生成随机浮点,包括两个边界   java三层体系结构风格是如何工作的?一些简单的例子   多线程可以使用线程。在Java中,在循环中使用sleep(),以便定期执行某些操作?   读取循环上的java HibernateMysql异常   java使用带有Apache Ivy的自定义存储库,未找到解析程序   filenotfoundexception在读取时出现问题。Java中的txt文件   嵌入式tomcat 8.0.21中的java Spring websocket   java为什么我需要创建一个类的引用,然后创建一个B类的对象   java Splashscreen动画在Mac OS中更新时闪烁   JavaSpring3。名为“zoneManagerDelegate”的x Bean必须是[com.ms.adsp.delegate.sapi.zoneManagerDelegate]类型,但实际上是[$Proxy20]类型   java SQLite:没有这样的专栏;不明错误   java将JTable定位到JFrame中JPanel中的(x,y)位置   java在导入组织方面面临挑战。知道。xchart*   xml读取Java中的SVG元素并跳过某些包含文本的元素   java Spring Redis问题:Redis缓存中的GetAllCacheNames不能与RedisCacheManager一起使用   java Vertex Hazelcast:集群问题   java如何编辑osgi托管服务实现使用的属性文件?   java Android活动并行启动?   java AWS Lambda用于将excel转储到数据库中