本文整理汇总了Python中yaml.Node方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.Node方法的具体用法?Python yaml.Node怎么用?Python yaml.Node使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.Node方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_dataclass
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def is_dataclass(obj: Any) -> bool:
from omegaconf.base import Node
if dataclasses is None or isinstance(obj, Node):
return False
return dataclasses.is_dataclass(obj)
示例2: is_attr_class
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def is_attr_class(obj: Any) -> bool:
from omegaconf.base import Node
if attr is None or isinstance(obj, Node):
return False
return attr.has(obj)
示例3: __init__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def __init__(self, node: Node, mode: ViewMode) -> None:
self.node = node
self.mode = mode
示例4: node
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def node(value: Any) -> Node:
return COERCIONS[type(value)](value)
示例5: __call__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def __call__(self, loader: SafeLoader, node: Node) -> str:
""" Implement the tag constructor interface.
:param loader: YAML loader
:param node: YAML node to process
:return: final value
"""
value = loader.construct_scalar(node)
return Template(value).substitute(self._params)
示例6: construct_include
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def construct_include(loader: Loader, node: yaml.Node) -> Any:
"""Include file referenced at node."""
relative_path = os.path.join(loader._root, loader.construct_scalar(node))
full_path = os.path.abspath(relative_path)
extension = os.path.splitext(full_path)[1].lstrip(".")
with open(full_path, "r") as f:
if extension in ("yaml", "yml"):
return yaml.load(f, Loader)
elif extension in ("json",):
return json.load(f)
else:
raise FileFormatNotSupportedError(f".{extension}", relative_path)
示例7: get_yaml_loader
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def get_yaml_loader() -> Any:
# Custom constructor that checks for duplicate keys
# (from https://gist.github.com/pypt/94d747fe5180851196eb)
def no_duplicates_constructor(
loader: yaml.Loader, node: yaml.Node, deep: bool = False
) -> Any:
mapping: Dict[str, Any] = {}
for key_node, value_node in node.value:
key = loader.construct_object(key_node, deep=deep)
value = loader.construct_object(value_node, deep=deep)
if key in mapping:
raise yaml.constructor.ConstructorError(
"while constructing a mapping",
node.start_mark,
f"found duplicate key {key}",
key_node.start_mark,
)
mapping[key] = value
return loader.construct_mapping(node, deep)
loader = yaml.SafeLoader
loader.add_implicit_resolver(
"tag:yaml.org,2002:float",
re.compile(
"""^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$""",
re.X,
),
list("-+0123456789."),
) # type : ignore
loader.yaml_implicit_resolvers = {
key: [
(tag, regexp)
for tag, regexp in resolvers
if tag != "tag:yaml.org,2002:timestamp"
]
for key, resolvers in loader.yaml_implicit_resolvers.items()
}
loader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor
)
return loader
示例8: get_ref_type
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Node [as 别名]
def get_ref_type(obj: Any, key: Any = None) -> Optional[Type[Any]]:
from omegaconf import DictConfig, ListConfig
from omegaconf.base import Container, Node
from omegaconf.nodes import ValueNode
def none_as_any(t: Optional[Type[Any]]) -> Union[Type[Any], Any]:
if t is None:
return Any
else:
return t
if isinstance(obj, Container) and key is not None:
obj = obj._get_node(key)
is_optional = True
ref_type = None
if isinstance(obj, ValueNode):
is_optional = obj._is_optional()
ref_type = obj._metadata.ref_type
elif isinstance(obj, Container):
if isinstance(obj, Node):
ref_type = obj._metadata.ref_type
if ref_type is Any:
pass
elif not is_structured_config(ref_type):
kt = none_as_any(obj._metadata.key_type)
vt = none_as_any(obj._metadata.element_type)
if isinstance(obj, DictConfig):
ref_type = Dict[kt, vt] # type: ignore
elif isinstance(obj, ListConfig):
ref_type = List[vt] # type: ignore
is_optional = obj._is_optional()
else:
if isinstance(obj, dict):
ref_type = Dict[Any, Any]
elif isinstance(obj, (list, tuple)):
ref_type = List[Any]
else:
ref_type = get_type_of(obj)
ref_type = none_as_any(ref_type)
if is_optional and ref_type is not Any:
ref_type = Optional[ref_type] # type: ignore
return ref_type