本文整理匯總了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)