本文整理汇总了Python中ruamel.yaml.Loader方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.Loader方法的具体用法?Python yaml.Loader怎么用?Python yaml.Loader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ruamel.yaml
的用法示例。
在下文中一共展示了yaml.Loader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def load_config(logdir):
"""Load a configuration from the log directory.
Args:
logdir: The logging directory containing the configuration file.
Raises:
IOError: The logging directory does not contain a configuration file.
Returns:
Configuration object.
"""
config_path = logdir and os.path.join(logdir, 'config.yaml')
if not config_path or not tf.gfile.Exists(config_path):
message = (
'Cannot resume an existing run since the logging directory does not '
'contain a configuration file.')
raise IOError(message)
with tf.gfile.FastGFile(config_path, 'r') as file_:
config = yaml.load(file_, Loader=yaml.Loader)
message = 'Resume run and write summaries and checkpoints to {}.'
tf.logging.info(message.format(config.logdir))
return config
示例2: get_cfg
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def get_cfg(existing_cfg, _log):
"""
generates
"""
_sanity_check(existing_cfg, _log)
import ntpath, os, ruamel.yaml as yaml
with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])),
'r') as stream:
try:
ret = yaml.load(stream, Loader=yaml.Loader)
except yaml.YAMLError as exc:
assert "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])
#if ret["coma_critic_use_sampling"] and "coma_critic_sample_size" not in ret and hasattr(ret, "batch_size_run"):
# ret["coma_critic_sample_size"] = ret["batch_size_run"] * 50
return ret
示例3: get_cfg
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def get_cfg(existing_cfg, _log):
"""
generates
"""
_sanity_check(existing_cfg, _log)
import ntpath, os, ruamel.yaml as yaml
with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])),
'r') as stream:
try:
ret = yaml.load(stream, Loader=yaml.Loader)
except yaml.YAMLError as exc:
assert "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])
if ret["coma_critic_use_sampling"] and "coma_critic_sample_size" not in ret and hasattr(ret, "batch_size_run"):
ret["coma_critic_sample_size"] = ret["batch_size_run"] * 50
return ret
示例4: get_cfg
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def get_cfg(existing_cfg, _log):
"""
"""
_sanity_check(existing_cfg, _log)
import ntpath, os, ruamel.yaml as yaml
with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])), 'r') as stream:
try:
ret = yaml.load(stream, Loader=yaml.Loader)
except yaml.YAMLError as exc:
assert False, "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])
if not "batch_size_run" in ret:
ret["batch_size_run"] = ret["batch_size"]
if not "runner_test_batch_size" in ret:
ret["runner_test_batch_size"] = ret["batch_size_run"]
if not "name" in ret:
ret["name"] = ntpath.basename(__file__).split(".")[0]
return ret
示例5: _ordered_load
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def _ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
"""
Ordered yaml loader
Use this instead ot yaml.loader/yaml.saveloader to get an Ordereddict
:param stream: stream to read from
:param Loader: yaml-loader to use
:object_pairs_hook: ...
:return: OrderedDict structure
"""
# usage example: ordered_load(stream, yaml.SafeLoader)
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)
示例6: yaml_load_roundtrip
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def yaml_load_roundtrip(filename):
"""
Load contents of a yaml file into an dict structure for editing (using Roundtrip Loader)
:param filename: name of the yaml file to load
:return: data structure loaded from file
"""
if not EDITING_ENABLED:
return None
y = None
if not filename.lower().endswith('.yaml'):
filename += YAML_FILE
try:
with open(filename, 'r') as stream:
sdata = stream.read()
sdata = sdata.replace('\n', '\n\n')
y = yaml.load(sdata, yaml.RoundTripLoader)
except Exception as e:
logger.error("yaml_load_roundtrip: YAML-file load error: '%s'" % (e))
y = {}
return y
示例7: load_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def load_config(logdir):
"""Load a configuration from the log directory.
Args:
logdir: The logging directory containing the configuration file.
Raises:
IOError: The logging directory does not contain a configuration file.
Returns:
Configuration object.
"""
config_path = logdir and os.path.join(logdir, 'config.yaml')
if not config_path or not tf.gfile.Exists(config_path):
message = (
'Cannot resume an existing run since the logging directory does not '
'contain a configuration file.')
raise IOError(message)
with tf.gfile.GFile(config_path, 'r') as file_:
config = yaml.load(file_, yaml.Loader)
message = 'Resume run and write summaries and checkpoints to {}.'
tf.logging.info(message.format(config.logdir))
return config
示例8: load_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def load_config(logdir):
# pylint: disable=missing-raises-doc
"""Load a configuration from the log directory.
Args:
logdir: The logging directory containing the configuration file.
Raises:
IOError: The logging directory does not contain a configuration file.
Returns:
Configuration object.
"""
config_path = logdir and os.path.join(logdir, 'config.yaml')
if not config_path or not tf.gfile.Exists(config_path):
message = (
'Cannot resume an existing run since the logging directory does not '
'contain a configuration file.')
raise IOError(message)
with tf.gfile.FastGFile(config_path, 'r') as file_:
config = yaml.load(file_, Loader=yaml.Loader)
message = 'Resume run and write summaries and checkpoints to {}.'
tf.logging.info(message.format(config.logdir))
return config
示例9: read_core_files
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def read_core_files():
core_path = Path('../core')
file_metas = []
for item in core_path.glob('**/*'):
if item.is_dir() or item.name.startswith('.'):
continue
category, file_name = item.parts[-2:]
file = core_path / category / file_name
with file.open() as file:
data_info = yaml.load(file.read(), Loader=yaml.Loader)
file_metas.append({
'category': category,
'file_name': file_name,
'url': data_info['homepage'],
})
return file_metas
示例10: test_config_unsafe_object_creation
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def test_config_unsafe_object_creation():
from ruamel import yaml
import calendar
test_config_path = testing.relative_module_path(__file__, 'test_config_unsafe.yaml')
# Unsafe default loader allows any python object initialization
data = yaml.load(open(test_config_path), Loader=yaml.Loader)
assert 'time' in data
assert isinstance(data['time'], calendar.Calendar)
# With use safe version only to allow construct previously registered objects
with pytest.raises(config.ConfigLoadError, match='could not determine a constructor'):
config.load_config(test_config_path)
示例11: sync
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def sync(ctx, in_place=False, check=True):
with open('CITATION.cff', 'rb') as file:
citation = load(file.read(), Loader=Loader)
authors = [
Contributor.from_citation_author(**author)
for author in citation['authors']]
with open('contributors.yaml', 'rb') as file:
contributors = load(file.read(), Loader=Loader)['contributors']
contributors = [Contributor.from_citation_author(
**contributor) for contributor in contributors]
with open('.zenodo.json', 'rb') as file:
zenodo = json.loads(file.read())
zenodo_updated = zenodo.copy()
zenodo_updated['creators'] = [a.as_zenodo_creator() for a in authors]
zenodo_updated['contributors'] = [c.as_zenodo_creator()
for c in contributors if c not in authors]
for key in ('version', 'keywords'):
zenodo_updated[key] = citation[key]
modified = json.dumps(zenodo, sort_keys=True) != json.dumps(zenodo_updated, sort_keys=True)
if modified:
if in_place:
with open('.zenodo.json', 'wb') as file:
file.write(json.dumps(zenodo_updated, indent=4, sort_keys=True).encode('utf-8'))
else:
click.echo(json.dumps(zenodo_updated, indent=4, sort_keys=True))
if check:
ctx.exit(1)
else:
click.echo("No changes.", err=True)
示例12: load
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def load(cls, filename):
assert str(filename).endswith('.yaml')
with open(filename, 'r') as f:
return cls(yaml.load(f, Loader=yaml.Loader))
示例13: execute
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def execute(self, gapic_code_dir, grpc_code_dir, proto_code_dir, gapic_yaml):
with open(gapic_yaml) as f:
gapic_config = yaml.load(f, Loader=yaml.Loader)
package_name = gapic_config.get('language_settings').get('csharp').get('package_name')
package_root = '{0}/{1}'.format(gapic_code_dir, package_name)
prod_dir = '{0}/{1}'.format(package_root, package_name)
# Copy proto/grpc .cs files into prod directory
self.exec_command(['sh', '-c', 'cp {0}/*.cs {1}'.format(proto_code_dir, prod_dir)])
self.exec_command(['sh', '-c', 'cp {0}/*.cs {1}'.format(grpc_code_dir, prod_dir)])
示例14: __ordered_load
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def __ordered_load(self, stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
"""Load an ordered dictionary from a yaml file.
Note
----
Borrowed from John Schulman.
http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts/21048064#21048064"
"""
class OrderedLoader(Loader):
pass
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
lambda loader, node: object_pairs_hook(loader.construct_pairs(node)))
return yaml.load(stream, OrderedLoader)
示例15: load_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import Loader [as 别名]
def load_config(config_path, loader=yaml.Loader, verify_version=True):
if not os.path.exists(config_path):
system_log.error(_("config.yml not found in {config_path}").format(config_path))
return False
with codecs.open(config_path, encoding="utf-8") as stream:
config = yaml.load(stream, loader)
if verify_version:
config = config_version_verify(config, config_path)
return config