本文整理汇总了Python中yaml.add_implicit_resolver方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.add_implicit_resolver方法的具体用法?Python yaml.add_implicit_resolver怎么用?Python yaml.add_implicit_resolver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.add_implicit_resolver方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _config_loader
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_implicit_resolver [as 别名]
def _config_loader():
envvar_matcher = re.compile(r"\${([^}^{]+)\}")
def envvar_constructor(_loader, node): # pragma: no cover
"""Extract the matched value, expand env variable, and replace the match."""
node_value = node.value
match = envvar_matcher.match(node_value)
env_var = match.group()[2:-1]
# check for defaults
var_name, default_value = env_var.split(":")
var_name = var_name.strip()
default_value = default_value.strip()
var_value = os.getenv(var_name, default_value)
return var_value + node_value[match.end() :]
yaml.add_implicit_resolver("!envvar", envvar_matcher, None, SafeLoader)
yaml.add_constructor("!envvar", envvar_constructor, SafeLoader)
# TODO: instead of this, create custom loader and use it
# by wrapping yaml.safe_load to use it
示例2: initialize
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_implicit_resolver [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
示例3: read
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_implicit_resolver [as 别名]
def read(self, filepath):
stream = False
# Setup environment variables replacement
def env_var_single_replace(match):
return os.environ[match.group(1)] if match.group(1) in os.environ else match.group()
def env_var_multi_replacer(loader, node):
value = loader.construct_scalar(node)
return re.sub(ENV_VAR_REPLACER_PATTERN, env_var_single_replace, value)
yaml.add_implicit_resolver("!envvarreplacer", ENV_VAR_MATCHER_PATTERN)
yaml.add_constructor('!envvarreplacer', env_var_multi_replacer)
# Read file
try:
stream = open(filepath, "r")
self.config = yaml.load(stream)
# Handle an empty configuration file
if not self.config:
self.config = {}
finally:
if stream:
stream.close()
示例4: constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_implicit_resolver [as 别名]
def constructor(tag=None, pattern=None):
"""Register custom constructor with pyyaml."""
def decorator(f):
if tag is None or f is tag:
tag_ = '!{}'.format(f.__name__)
else:
tag_ = tag
yaml.add_constructor(tag_, f)
if pattern is not None:
yaml.add_implicit_resolver(tag_, re.compile(pattern))
return f
if callable(tag): # little convenience hack to avoid empty arg list
return decorator(tag)
return decorator