本文整理匯總了Python中yaml.Loader.add_constructor方法的典型用法代碼示例。如果您正苦於以下問題:Python Loader.add_constructor方法的具體用法?Python Loader.add_constructor怎麽用?Python Loader.add_constructor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類yaml.Loader
的用法示例。
在下文中一共展示了Loader.add_constructor方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: OrderedYaml
# 需要導入模塊: from yaml import Loader [as 別名]
# 或者: from yaml.Loader import add_constructor [as 別名]
def OrderedYaml():
'''yaml orderedDict support'''
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
def dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))
Dumper.add_representer(OrderedDict, dict_representer)
Loader.add_constructor(_mapping_tag, dict_constructor)
return Loader, Dumper
####################
# miscellaneous
####################
示例2: OrderedYaml
# 需要導入模塊: from yaml import Loader [as 別名]
# 或者: from yaml.Loader import add_constructor [as 別名]
def OrderedYaml():
'''yaml orderedDict support'''
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
def dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))
Dumper.add_representer(OrderedDict, dict_representer)
Loader.add_constructor(_mapping_tag, dict_constructor)
return Loader, Dumper
示例3: fix_yaml_loader
# 需要導入模塊: from yaml import Loader [as 別名]
# 或者: from yaml.Loader import add_constructor [as 別名]
def fix_yaml_loader():
"""Ensure that any string read by yaml is represented as unicode."""
from yaml import Loader, SafeLoader
def construct_yaml_str(self, node):
# Override the default string handling function
# to always return unicode objects
return self.construct_scalar(node)
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)