本文整理汇总了Python中yaml.resolver方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.resolver方法的具体用法?Python yaml.resolver怎么用?Python yaml.resolver使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.resolver方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_template
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import resolver [as 别名]
def parse_template(template_str):
"""
Parse the SAM template.
:param template_str: A packaged YAML or json CloudFormation template
:type template_str: str
:return: Dictionary with keys defined in the template
:rtype: dict
"""
try:
# PyYAML doesn't support json as well as it should, so if the input
# is actually just json it is better to parse it with the standard
# json parser.
return json.loads(template_str, object_pairs_hook=OrderedDict)
except ValueError:
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _dict_constructor)
yaml.SafeLoader.add_multi_constructor('!', intrinsics_multi_constructor)
return yaml.safe_load(template_str)
示例2: yaml_load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import resolver [as 别名]
def yaml_load(file_path: str):
import yaml
from yaml.resolver import BaseResolver
def ordered_load(stream, loader_cls):
class OrderedLoader(loader_cls):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return odict(loader.construct_pairs(node))
OrderedLoader.add_constructor(BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)
return yaml.load(stream, OrderedLoader)
return ordered_load(open(file_path, 'r'), yaml.SafeLoader)
示例3: yaml_load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import resolver [as 别名]
def yaml_load(stream, Loader=yaml.Loader, object_pairs_hook=UnsortableOrderedDict):
class MyLoader(Loader):
pass
# use UnsortableOrderedDict
MyLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
lambda loader, node: object_pairs_hook(loader.construct_pairs(node)))
# treat str as unicode
def construct_yaml_str(self, node):
return self.construct_scalar(node)
MyLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
return yaml.load(stream, MyLoader)
示例4: yaml_parse
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import resolver [as 别名]
def yaml_parse(yamlstr):
"""Parse a yaml string"""
try:
# PyYAML doesn't support json as well as it should, so if the input
# is actually just json it is better to parse it with the standard
# json parser.
return json.loads(yamlstr, object_pairs_hook=OrderedDict)
except ValueError:
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _dict_constructor)
yaml.SafeLoader.add_multi_constructor(
"!", intrinsics_multi_constructor)
return yaml.safe_load(yamlstr)