config.basic.environment

环境变量配置数据实现

在 0.2.0 版本加入.

Classes

Difference

与初始化数据的差异

EnvironmentConfigData

环境变量配置数据

Module Contents

class Difference

与初始化数据的差异

__bool__() bool
__iadd__(other: Any) Self
__isub__(other: Any) Self
clear() None

清空差异

removed: set[str]

删除的键

updated: set[str]

修改/新增的键

备注

实现的不是很完美,如果一个键更改为了另一个值再改回来仍然会被认为是被修改过的键

class EnvironmentConfigData(data: collections.abc.MutableMapping[str, str] | None = None)

Bases: config.basic.mapping.MappingConfigData[collections.abc.MutableMapping[str, str]]

环境变量配置数据

内部维护了与初始化参数的键差异

备注

OSEnvSL 在保存时会重置差异数据

警告

当前实现 不会验证值的类型 ,当值不是字符串类型时,在实际写入 os.environ 时会抛出错误, 请确保传入的值是字符串类型

参数:

data (MutableMapping[str, str] | None) -- 环境变量数据

__contains__(key)
__delitem__(index: str) None
__eq__(other)
__getattr__(item: Any) Self | Any
abstractmethod __getitem__(key)
__ior__(other: collections.abc.MutableMapping[str, str]) Self
abstractmethod __iter__()
abstractmethod __len__()
__or__(other: Any) Self
__ror__(other: Any) Self
__setitem__(index: str, value: str) None
classmethod __subclasshook__(C)
clear() None

D.clear() -> None. Remove all items from D.

delete(path: config.abc.PathLike) Self
get(key, default=None)

D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

items(*, return_raw_value: bool = False) collections.abc.ItemsView[str, Any]

获取所有键值对

参数:

return_raw_value (bool) -- 是否获取原始数据

返回:

所有键值对

返回类型:

ItemsView[str, Any]

在 0.2.0 版本发生变更: 重命名参数 get_rawreturn_raw_value

keys(*, recursive: bool = False, strict: bool = True, end_point_only: bool = False) collections.abc.KeysView[Any]

获取所有键

不为 Mapping 默认行为时键必须为 str 且返回值会被转换为 配置数据路径字符串

参数:
  • recursive (bool) -- 是否递归获取

  • strict (bool) -- 是否严格检查循环引用数据,为真时提前抛出错误,否则静默忽略

  • end_point_only (bool) -- 是否只获取叶子节点

返回:

所有键

返回类型:

KeysView[str]

抛出:

例子

>>> from c41811.config import MappingConfigData
>>> data = MappingConfigData({"foo": {"bar": {"baz": "value"}, "bar1": "value1"}, "foo1": "value2"})

不带参数行为与普通字典一样

>>> data.keys()
dict_keys(['foo', 'foo1'])

参数 end_point_only 会滤掉非 叶子节点 的键

>>> data.keys(end_point_only=True)  # 内部计算为保留顺序采用了OrderedDict所以返回值是odict_keys
odict_keys(['foo1'])

参数 recursive 用于获取所有的 路径

>>> data.keys(recursive=True)
odict_keys(['foo\\.bar\\.baz', 'foo\\.bar', 'foo\\.bar1', 'foo', 'foo1'])

同时提供 recursiveend_point_only 会产出所有 叶子节点 的路径

>>> data.keys(recursive=True, end_point_only=True)
odict_keys(['foo\\.bar\\.baz', 'foo\\.bar1', 'foo1'])

为严格模式时会检查循环引用并提前引发错误

>>> cyclic: dict[str, Any] = {"cyclic": None, "key": "value"}
>>> cyclic["cyclic"] = cyclic
>>> cyclic: MappingConfigData[dict[str, Any]] = MappingConfigData(cyclic)
>>> cyclic.keys(recursive=True)  # 默认为严格模式
Traceback (most recent call last):
    ...
c41811.config.errors.CyclicReferenceError: Cyclic reference detected at \.cyclic -> \.cyclic (1/1)

否则静默跳过循环引用

>>> cyclic.keys(recursive=True, strict=False)
odict_keys(['cyclic', 'key'])
>>> cyclic.keys(recursive=True, strict=False, end_point_only=True)
odict_keys(['key'])

在 0.2.0 版本发生变更: 添加参数 strict

modify(path: config.abc.PathLike, value: str, *, allow_create: bool = True) Self
pop(path: config.abc.PathLike, /, default: Any = Unset) Any

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() Any

D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.

setdefault[V](path: config.abc.PathLike, default: V | None = None, *, return_raw_value: bool = False) V | Any

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

unset(path: config.abc.PathLike) Self
update(m: Any | None = None, /, **kwargs: str) None

D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. If E present and has a .keys() method, does:

for k in E.keys(): D[k] = E[k]

If E present and lacks .keys() method, does:

for (k, v) in E: D[k] = v

In either case, this is followed by:

for k, v in F.items(): D[k] = v

values(return_raw_value: bool = False) collections.abc.ValuesView[Any]

获取所有值

参数:

return_raw_value (bool) -- 是否获取原始数据

返回:

所有键值对

返回类型:

ValuesView[Any]

在 0.2.0 版本发生变更: 重命名参数 get_rawreturn_raw_value

__abc_tpflags__ = 64
__class_getitem__
__marker
__reversed__ = None
__slots__ = ()
_data: D
data: D
property data_read_only: bool
difference