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