本文整理汇总了Python中yaml.constructor方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.constructor方法的具体用法?Python yaml.constructor怎么用?Python yaml.constructor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.constructor方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __repr__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def __repr__(self, config=None):
class_name = self.__class__.__name__
config = config or self._get_config()
# attributes (including properties) that are in config
attr_dict = {k: getattr(self, k) for k in dir(self) if k in config}
# order attributes based in the order in which they appear in the constructor
ordered_attr_pairs = []
for arg_name in get_signature_args(self.__init__):
try:
ordered_attr_pairs.append((arg_name, attr_dict.pop(arg_name)))
except KeyError:
pass
# add remaining attributes that doesn't appear in the constructor
ordered_attr_pairs += list(attr_dict.items())
kwargs = ', '.join(['%s=%r' % (k, v) for (k, v) in ordered_attr_pairs])
return "%s(%s)" % (class_name, kwargs)
示例2: construct_mapping
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(None, None,
'expected a mapping node, but found %s' % node.id, node.start_mark)
mapping = OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as exc:
raise yaml.constructor.ConstructorError('while constructing a mapping',
node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
示例3: initialize
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def initialize():
"""
Initialize the configuration system by installing YAML handlers.
Automatically done on first call to load() specified in this file.
"""
global is_initialized
# Add the custom multi-constructor
yaml.add_multi_constructor('!obj:', multi_constructor_obj)
yaml.add_multi_constructor('!pkl:', multi_constructor_pkl)
yaml.add_multi_constructor('!import:', multi_constructor_import)
yaml.add_constructor('!import', constructor_import)
yaml.add_constructor("!float", constructor_float)
pattern = re.compile(SCIENTIFIC_NOTATION_REGEXP)
yaml.add_implicit_resolver('!float', pattern)
is_initialized = True
###############################################################################
# Callbacks used by PyYAML
示例4: construct_mapping
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
self.flatten_mapping(node)
else:
raise yaml.constructor.ConstructorError(None, None,
'expected a mapping node, but found %s' %
node.id, node.start_mark)
mapping = OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
try:
hash(key)
except TypeError as exc:
raise yaml.constructor.ConstructorError('while constructing a mapping',
node.start_mark,
'found unacceptable key (%s)' % exc,
key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
示例5: testIfTableIsAMap
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def testIfTableIsAMap(self):
from yaml.constructor import ConstructorError
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
# Credit https://gist.github.com/pypt/94d747fe5180851196eb
def no_duplicates_constructor(loader, node, deep=False):
"""Check for duplicate keys."""
mapping = {}
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 ConstructorError(
"while constructing a mapping", node.start_mark,
"found duplicate key (%s)" % key, key_node.start_mark)
mapping[key] = value
return loader.construct_mapping(node, deep)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor)
file_path = '../translationtable/GLOBAL_TERMS.yaml'
if os.path.exists(os.path.join(os.path.dirname(__file__), file_path)):
tt_file = open(os.path.join(os.path.dirname(__file__), file_path), 'r')
try:
translation_table = yaml.safe_load(tt_file)
except yaml.constructor.ConstructorError as e:
tt_file.close()
self.assertTrue(False)
print(e)
tt_file.close()
示例6: construct_mapping
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def construct_mapping(node, deep=False):
# This is a modified version of yaml.BaseConstructor.construct_mapping
# in which a repeated key raises a ConstructorError
if not isinstance(node, yaml.nodes.MappingNode):
const = yaml.constructor
message = "expected a mapping node, but found"
raise const.ConstructorError(None, None,
"%s %s " % (message, node.id),
node.start_mark)
mapping = {}
constructor = yaml.constructor.BaseConstructor()
for key_node, value_node in node.value:
key = constructor.construct_object(key_node, deep=False)
try:
hash(key)
except TypeError as exc:
const = yaml.constructor
reraise_as(const.ConstructorError("while constructing a mapping",
node.start_mark,
"found unacceptable key (%s)" %
(exc, key_node.start_mark)))
if key in mapping:
const = yaml.constructor
raise const.ConstructorError("while constructing a mapping",
node.start_mark,
"found duplicate key (%s)" % key)
value = constructor.construct_object(value_node, deep=False)
mapping[key] = value
return mapping
示例7: constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def constructor(_):
raise NotImplementedError
示例8: __str__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def __str__(self):
return "<Tavern YAML sentinel for {}>".format(self.constructor)
示例9: from_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import constructor [as 别名]
def from_yaml(cls, loader, node):
value = loader.construct_scalar(node)
try:
# See if it's already a valid value (eg, if we do `!int "2"`)
converted = cls.constructor(value)
except ValueError:
# If not (eg, `!int "{int_value:d}"`)
return cls(value)
else:
return converted