Python yaml.Node() Examples
The following are 8
code examples of yaml.Node().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
yaml
, or try the search function
.
Example #1
Source File: _utils.py From omegaconf with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #2
Source File: _utils.py From omegaconf with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #3
Source File: parser.py From ambassador with Apache License 2.0 | 5 votes |
def __init__(self, node: Node, mode: ViewMode) -> None: self.node = node self.mode = mode
Example #4
Source File: parser.py From ambassador with Apache License 2.0 | 5 votes |
def node(value: Any) -> Node: return COERCIONS[type(value)](value)
Example #5
Source File: config.py From cookiecutter-python-app with MIT License | 5 votes |
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)
Example #6
Source File: config_loader.py From scanapi with MIT License | 5 votes |
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)
Example #7
Source File: _utils.py From omegaconf with BSD 3-Clause "New" or "Revised" License | 4 votes |
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
Example #8
Source File: _utils.py From omegaconf with BSD 3-Clause "New" or "Revised" License | 4 votes |
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