config.basic.mapping¶
映射类型配置数据实现
在 0.2.0 版本加入.
Classes¶
映射配置数据 |
Module Contents¶
- class MappingConfigData[D: collections.abc.Mapping[Any, Any]](data: D | None = None)¶
Bases:
config.basic.core.BasicIndexedConfigData[D],collections.abc.MutableMapping[Any,Any]映射配置数据
在 0.1.5 版本加入.
- 参数:
data (D | None) -- 映射数据
- __contains__(key)¶
- abstractmethod __delitem__(key)¶
- __eq__(other)¶
- __getattr__(item: Any) Self | Any¶
- abstractmethod __getitem__(key)¶
- abstractmethod __iter__()¶
- abstractmethod __len__()¶
- __or__(other: Any) Self¶
- __ror__(other: Any) Self¶
- abstractmethod __setitem__(key, value)¶
- classmethod __subclasshook__(C)¶
- 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
- 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(key, default=None)¶
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
- update(m: Any = None, /, **kwargs: Any) 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¶