tempjson
attribute
PathLike= str | os.PathLike[str]
attribute
VT= TypeVar('VT')
attribute
tempjson_rw_lock= Lock()
attribute
tempjson_pathsdict[PathLike, _jsonfile_status]
= {}
func
load_from_path(path, need_file_exists=True, default=None, unload_delay=None) -> _jsonfile_status
将 json 文件从磁盘加载到缓存区,以便快速读写. 在缓存文件已加载的情况下,再使用一次该方法不会有任何作用.
Args: path (str): 作为文件的磁盘内路径的同时也会作为在缓存区的虚拟路径 needFileExists (bool, optional): 默认为 True, 为 False 时,若文件路径不存在,就会自动创建一个文件 default (Any, optional): 默认为 None, 为 None 时,若文件路径不存在,就会自动创建一个文件,且写入默认值 null unload_delay (int, optional): 多久没有再进行读写操作时, 将其从缓存卸载
Raises: err: 文件不存在时
param
pathPathLike
param
need_file_existsbool
= True
param
defaultAny
= None
param
unload_delayint | None
= None
Returns
tooldelta.utils.tempjson._jsonfile_status
func
unload_to_path(path) -> bool
将 json 文件从缓存区卸载 (保存内容到磁盘), 之后不能再在缓存区对这个文件进行读写. 在缓存文件已卸载的情况下,再使用一次该方法不会有任何作用,但是可以通过其返回的值来知道存盘有没有成功.
Args: path (str): 文件的虚拟路径
Returns: bool: 存盘是否成功
param
pathPathLike
Returns
bool
func
read(path, deepcopy=True)
对缓存区的该虚拟路径的文件进行读操作,返回一个深拷贝的 JSON 对象
Args: path (str): 文件的虚拟路径
Raises: Exception: json 路径未初始化,不能进行读取和写入操作
Returns: list[Any] | dict[Any, Any] | Any: 该虚拟路径的 JSON
param
pathPathLike
param
deepcopybool
= True
Returns
None
func
get(path) -> Any
直接获取缓存区的该虚拟路径的 JSON, 不使用 copy
WARNING: 如果你不知道有什么后果,请老老实实使用read(...)
而不是get(...)
!
Args: path (str): 文件的虚拟路径
param
pathPathLike
Returns
typing.Any
func
write(path, obj) -> None
对缓存区的该虚拟路径的文件进行写操作,这将会覆盖之前的内容
Args: path (str): 文件的虚拟路径 obj (Any): 任何合法的 JSON 类型 例如 dict/list/str/bool/int/float
param
pathPathLike
param
objAny
Returns
None
func
load_and_read(path, need_file_exists=True, timeout=60, default={}) -> VT
读取 json 文件并将其从磁盘加载到缓存区,以便一段时间内能快速读写.
Args: path (str): 作为文件的磁盘内路径的同时也会作为在缓存区的虚拟路径 needFileExists (bool, optional): 默认为 True, 为 False 时,若文件路径不存在,就会自动创建一个文件,且写入默认值 null timeout (int, optional): 多久没有再进行读取操作时卸载缓存 default (() -> Any | Any, optional):
Returns: Any: 该虚拟路径的 JSON
param
pathPathLike
param
need_file_existsbool
= True
param
timeoutint
= 60
param
defaultCallable[[], VT] | VT
= {}
Returns
tooldelta.utils.tempjson.VT
func
load_and_write(path, obj, need_file_exists=True, timeout=60) -> None
写入 json 文件并将其从磁盘加载到缓存区,以便一段时间内能快速读写.
Args: path (str): 作为文件的磁盘内路径的同时也会作为在缓存区的虚拟路径 obj (Any): 任何合法的 JSON 类型 例如 dict/list/str/bool/int/float needFileExists (bool, optional): 默认为 True, 为 False 时,若文件路径不存在,就会自动创建一个文件,且写入默认值 null timeout (int, optional): 多久没有再进行读取操作时卸载缓存
param
pathPathLike
param
objAny
param
need_file_existsbool
= True
param
timeoutint
= 60
Returns
None
func
cancel_change(path)
取消缓存 json 所做的更改,非必要情况请勿调用,你不知道什么时候会自动保存所做更改
param
pathstr
Returns
None
func
flush(path=None)
刷新单个/全部JSON缓存区的缓存文件, 存入磁盘
Args: path (str | None, optional): 文件虚拟路径, 默认全部存盘
param
pathPathLike | None
= None
Returns
None
func
get_tmps() -> dict
获取缓存区中所有临时文件
Returns
dict
func
jsonfile_auto_save()
Returns
None
func
save_all()
Returns
None
func
reset()
Returns
None