config.basic.environment¶
环境变量配置数据实现
在 0.2.0 版本加入.
Classes¶
与初始化数据的差异 |
|
环境变量配置数据 |
Module Contents¶
- class EnvironmentConfigData(data: collections.abc.MutableMapping[str, str] | None = None)¶
Bases:
config.basic.mapping.MappingConfigData[collections.abc.MutableMapping[str,str]]环境变量配置数据
内部维护了与初始化参数的键差异
备注
OSEnvSL在保存时会重置差异数据警告
当前实现 不会验证值的类型 ,当值不是字符串类型时,在实际写入
os.environ时会抛出错误, 请确保传入的值是字符串类型- __contains__(key)¶
- __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¶
- classmethod __subclasshook__(C)¶
- 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]¶
获取所有键值对
在 0.2.0 版本发生变更: 重命名参数
get_raw为return_raw_value
- keys(*, recursive: bool = False, strict: bool = True, end_point_only: bool = False) collections.abc.KeysView[Any]¶
获取所有键
不为
Mapping默认行为时键必须为str且返回值会被转换为 配置数据路径字符串- 参数:
- 返回:
所有键
- 返回类型:
KeysView[str]
- 抛出:
TypeError -- 递归获取时键不为str时抛出
CyclicReferenceError -- 严格检查循环引用数据时发现循环引用抛出
例子¶
>>> 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'])
同时提供
recursive和end_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_raw为return_raw_value
- __abc_tpflags__ = 64¶
- __class_getitem__¶
- __marker¶
- __reversed__ = None¶
- __slots__ = ()¶
- _data: D¶
- data: D¶
- difference¶