Skip to main content

使用字典的易于使用的数据库

项目描述

字典数据库

下载 下载 下载

DictDataBase 是一个简单但快速且安全的数据库,用于处理 dicts(或 PathDicts 以获得更高级的功能),它使用 json 文件作为底层存储机制。由于采用了锁定机制,它也是多进程和多线程安全的。

进口

	import DictDataBase as DDB

配置

有 3 个配置选项。将 storage_directory 设置为将包含数据库文件的目录的路径:

	DDB.config.storage_directory = "./ddb_storage" # Default value

如果要使用压缩文件,请将 use_compression 设置为 True。这将使 db 文件显着变小,如果您的磁盘速度较慢,可能会提高性能。但是,这些文件将不是人类可读的。

	DDB.config.use_compression = False # Default value

如果您将 pretty_json_files 设置为 True,则 json db 文件将缩进并且键将被排序。它不会影响压缩文件,因为无论如何它们都不是人类可读的。

	DDB.config.pretty_json_files = True # Default value

创建字典

在您可以访问字典之前,您需要显式地创建它们。

一定要创建已经存在的,这会引发异常。也不要访问不存在的,这也会引发异常。

	user_data_dict = {
		"users": {
			"Ben": {
				"age": 30,
				"job": "Software Engineer"
			},
			"Sue": {
				"age": 21:
				"job": "Student"
			},
			"Joe": {
				"age": 50,
				"job": "Influencer"
			}
		},
		"follows": [["Ben", "Sue"], ["Joe", "Ben"]]
	})
	DDB.create("user_data", db=user_data_dict)
	# There is now a file called user_data.json (or user_data.ddb if you use compression)
	# in your specified storage directory.

阅读听写

	d = DDB.read("user_data")
	# You now have a copy of the dict named "user_data"
	print(d == user_data_dict) # True

写听写

	import DictDataBase as DDB
	with DDB.session("user_data") as (session, d):
		# You now have a handle on the dict named "user_data"
		# Inside the with statement, the file of user_data will be locked, and no other
		# processes will be able to interfere.
		d["follows"].append(["Sue", "Ben"])
		session.save_changes()
		# Now the changes to d are written to the database

	print(DDB.read("user_data")["follows"])
	# -> [["Ben", "Sue"], ["Joe", "Ben"], ["Sue", "Ben"]]

如果不调用 session.save_changes(),则不会修改数据库文件。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

DictDataBase-1.1.4.tar.gz (7.7 kB 查看哈希

已上传 source

内置分布

DictDataBase-1.1.4-py3-none-any.whl (8.5 kB 查看哈希

已上传 py3