本文整理汇总了Python中yaml.Loader方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.Loader方法的具体用法?Python yaml.Loader怎么用?Python yaml.Loader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.Loader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_yaml_file
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load_yaml_file(filename, label='config file', loader=yaml.Loader):
"""Load a yaml config file.
"""
try:
with open(filename, "r") as file:
res = yaml.load(file.read(), Loader=loader) or {}
if not isinstance(res, dict):
msg = f"Please ensure that {label} consists of key value pairs."
raise VergeMLError(f"Invalid {label}: {filename}", msg)
return res
except yaml.YAMLError as err:
if hasattr(err, 'problem_mark'):
mark = getattr(err, 'problem_mark')
problem = getattr(err, 'problem')
message = f"Could not read {label} {filename}:"
message += "\n" + display_err_in_file(filename, mark.line, mark.column, problem)
elif hasattr(err, 'problem'):
problem = getattr(err, 'problem')
message = f"Could not read {label} {filename}: {problem}"
else:
message = f"Could not read {label} {filename}: YAML Error"
suggestion = f"There is a syntax error in your {label} - please fix it and try again."
raise VergeMLError(message, suggestion)
except OSError as err:
msg = "Please ensure the file exists and you have the required access privileges."
raise VergeMLError(f"Could not open {label} {filename}: {err.strerror}", msg)
示例2: yaml_load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def yaml_load(source, loader=yaml.Loader):
"""
Wrap PyYaml's loader so we can extend it to suit our needs.
Load all strings as Unicode: http://stackoverflow.com/a/2967461/3609487.
"""
def construct_yaml_str(self, node):
"""Override the default string handling function to always return Unicode objects."""
return self.construct_scalar(node)
class Loader(loader):
"""Define a custom loader to leave the global loader unaltered."""
# Attach our Unicode constructor to our custom loader ensuring all strings
# will be Unicode on translation.
Loader.add_constructor('tag:yaml.org,2002:str', construct_yaml_str)
return yaml.load(source, Loader)
示例3: _download_results
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def _download_results(config, _blob_client, out_path, count, ptrn=FOLD_FILE_PATTERN):
pathlib.Path(config.BATCH_DIRECTORY).mkdir(parents=True, exist_ok=True)
blob_names = [b.name for b in _blob_client.list_blobs(config.CONTAINER_NAME)]
results = []
for i in range(count):
blob_name = ptrn.format(i)
if not blob_name in blob_names:
raise RuntimeError("incomplete blob set: missing blob {}".format(blob_name))
out_path = os.path.join(config.BATCH_DIRECTORY, blob_name)
with _blob_client.get_blob_to_stream(
config.CONTAINER_NAME, blob_name, out_path
) as blob:
results[i] = load(blob, Loader=Loader)
return results
示例4: get_config
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def get_config(infile):
"""
read in the contents of the inputs yaml file
"""
with open(infile, "r") as fp:
config = load(fp, Loader=Loader)
try:
v_pen = tuple(config["v_pen"])
except TypeError:
v_pen = (config["v_pen"],)
try:
w_pen = tuple(config["w_pen"])
except TypeError:
w_pen = (config["w_pen"],)
return v_pen, w_pen, config
示例5: from_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def from_yaml(stream, cls=None, loader_cls=yaml.Loader,
object_pairs_hook=OrderedDict, **extras):
"""
Convert a YAML stream into a class via the OrderedLoader class.
"""
class OrderedLoader(loader_cls):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
yaml_dict = yaml.load(stream, OrderedLoader) or {}
yaml_dict.update(extras)
return cls(**yaml_dict) if cls else yaml_dict
示例6: ordered_load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
"""load mappings and ordered mappings
loader to load mappings and ordered mappings into the Python 2.7+ OrderedDict type,
instead of the vanilla dict and the list of pairs it currently uses.
"""
class OrderedLoader(Loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping
)
return yaml.load(stream, OrderedLoader)
示例7: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load(self, content):
"""Loads Command Definitions from the given YAML content into
into this Command Dictionary. Content may be either a
filename containing YAML content or a YAML string.
Load has no effect if this Command Dictionary was already
instantiated with a filename or YAML content.
"""
if self.filename is None:
if os.path.isfile(content):
self.filename = content
stream = open(self.filename, 'rb')
else:
stream = content
cmds = yaml.load(stream, Loader=yaml.Loader)
cmds = handle_includes(cmds)
for cmd in cmds:
self.add(cmd)
if isinstance(stream, IOBase):
stream.close()
示例8: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load(self, content):
"""Loads Limit Definitions from the given YAML content into this
Telemetry Dictionary. Content may be either a filename
containing YAML content or a YAML string.
Load has no effect if this Limits Dictionary was already
instantiated with a filename or YAML content.
"""
if self.filename is None:
if os.path.isfile(content):
self.filename = content
stream = open(self.filename, 'rb')
else:
stream = content
limits = yaml.load(stream, Loader=yaml.Loader)
for lmt in limits:
self.add(lmt)
if isinstance(stream, IOBase):
stream.close()
示例9: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load(self, content):
if self.filename:
log.warn('EVRDict: Skipping load() attempt after previous initialization')
return
if os.path.isfile(content):
self.filename = content
stream = open(self.filename, 'rb')
else:
stream = content
try:
evrs = yaml.load(stream, Loader=yaml.Loader)
except IOError as e:
msg = "Could not load EVR YAML '{}': '{}'".format(stream, str(e))
log.error(msg)
return
for e in evrs:
self.add(e)
示例10: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load(self, content):
"""Loads Packet Definitions from the given YAML content into this
Telemetry Dictionary. Content may be either a filename
containing YAML content or a YAML string.
Load has no effect if this Command Dictionary was already
instantiated with a filename or YAML content.
"""
if self.filename is None:
if os.path.isfile(content):
self.filename = content
stream = open(self.filename, 'rb')
else:
stream = content
pkts = yaml.load(stream, Loader=yaml.Loader)
pkts = handle_includes(pkts)
for pkt in pkts:
self.add(pkt)
if isinstance(stream, IOBase):
stream.close()
示例11: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load(self, ymlfile=None):
"""Load and process the YAML file"""
if ymlfile is not None:
self.ymlfile = ymlfile
try:
# If yaml should be 'cleaned' of document references
if self._clean:
self.data = self.process(self.ymlfile)
else:
with open(self.ymlfile, 'rb') as stream:
for data in yaml.load_all(stream, Loader=yaml.Loader):
self.data.append(data)
self.loaded = True
except ScannerError as e:
msg = "YAML formattting error - '" + self.ymlfile + ": '" + str(e) + "'"
raise util.YAMLError(msg)
示例12: read_yaml_env
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def read_yaml_env(fname):
# type: (str) -> Dict[str, Any]
"""Parse YAML file with environment variable substitution.
Parameters
----------
fname : str
yaml file name.
Returns
-------
table : Dict[str, Any]
the yaml file as a dictionary.
"""
content = read_file(fname)
# substitute environment variables
content = string.Template(content).substitute(os.environ)
return yaml.load(content, Loader=yaml.Loader)
示例13: read_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def read_yaml(fname):
"""Read the given file using YAML.
Parameters
----------
fname : string
the file name.
Returns
-------
content : Any
the object returned by YAML.
"""
with open_file(fname, 'r') as f:
content = yaml.load(f, Loader=yaml.Loader)
return content
示例14: load_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def load_yaml(config_file):
"""Load meta.yaml that is output when the model is converted.
Args:
config_file (str): Path of the configuration file.
Returns:
EasyDict: Dictionary object of loaded configuration file.
Examples:
>>> config = load_yaml("/path/of/meta.yaml")
"""
with open(config_file) as config_file_stream:
config = yaml.load(config_file_stream, Loader=yaml.Loader)
# use only upper key.
return EasyDict({k: v for k, v in config.items() if k.isupper()})
示例15: train
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import Loader [as 别名]
def train(config_file, experiment_id=None, recreate=False, profile_step=-1):
if not experiment_id:
# Default model_name will be taken from config file: {model_name}.yml.
model_name = os.path.splitext(os.path.basename(config_file))[0]
experiment_id = '{}_{:%Y%m%d%H%M%S}'.format(model_name, datetime.now())
run(config_file, experiment_id, recreate, profile_step)
output_dir = os.environ.get('OUTPUT_DIR', 'saved')
experiment_dir = os.path.join(output_dir, experiment_id)
checkpoint = os.path.join(experiment_dir, 'checkpoints', 'checkpoint')
if not tf.io.gfile.exists(checkpoint):
raise Exception('Checkpoints are not created in {}'.format(experiment_dir))
with tf.io.gfile.GFile(checkpoint) as stream:
data = yaml.load(stream, Loader=yaml.Loader)
checkpoint_name = os.path.basename(data['model_checkpoint_path'])
return experiment_id, checkpoint_name