本文整理匯總了Python中yaml.org方法的典型用法代碼示例。如果您正苦於以下問題:Python yaml.org方法的具體用法?Python yaml.org怎麽用?Python yaml.org使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類yaml
的用法示例。
在下文中一共展示了yaml.org方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: yaml_load
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [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)
示例2: make_loader
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def make_loader(*tags_to_remove: str) -> Type[yaml.SafeLoader]:
"""Create a YAML loader, that doesn't parse specific tokens into Python objects."""
cls: Type[yaml.SafeLoader] = type("YAMLLoader", (SafeLoader,), {})
cls.yaml_implicit_resolvers = {
key: [(tag, regexp) for tag, regexp in mapping if tag not in tags_to_remove]
for key, mapping in cls.yaml_implicit_resolvers.copy().items()
}
# Fix pyyaml scientific notation parse bug
# See PR: https://github.com/yaml/pyyaml/pull/174 for upstream fix
cls.add_implicit_resolver( # type: ignore
"tag:yaml.org,2002:float",
re.compile(
r"""^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\.[0-9_]+(?:[eE][-+]?[0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
|[-+]?\.(?:inf|Inf|INF)
|\.(?:nan|NaN|NAN))$""",
re.X,
),
list("-+0123456789."),
)
return cls
示例3: unicode_representer
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def unicode_representer(_, data):
has_wide_lines = False
for line in data.splitlines():
if len(line) > 80:
has_wide_lines = True
break
if has_wide_lines:
return yaml.ScalarNode(
u'tag:yaml.org,2002:str', data, style='>')
if "\n" in data:
return yaml.ScalarNode(
u'tag:yaml.org,2002:str', data, style='|')
return yaml.ScalarNode(
u'tag:yaml.org,2002:str', data, style='')
示例4: parse_args
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def parse_args(parser):
args = parser.parse_args()
if args.config_file:
loader = yaml.SafeLoader
loader.add_implicit_resolver(
u'tag:yaml.org,2002:float',
re.compile(u'''^(?:
[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
|\\.[0-9_]+(?:[eE][-+][0-9]+)?
|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
|[-+]?\\.(?:inf|Inf|INF)
|\\.(?:nan|NaN|NAN))$''', re.X),
list(u'-+0123456789.'))
data = yaml.load(args.config_file, Loader=loader)
delattr(args, 'config_file')
arg_dict = args.__dict__
# print(len(list(arg_dict.keys())))
# print(len(list(data.keys())))
for key, value in arg_dict.items():
default_arg = parser.get_default(key)
if arg_dict[key] == default_arg and key in data:
arg_dict[key] = data[key]
return args
示例5: get_dict_from_yaml
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def get_dict_from_yaml(config_file: Union[str, io.StringIO]) -> dict:
"""
Read a config file or file like object of YAML into a dict
"""
# We must override the default constructor for timestamp to ensure the result
# has tzinfo. Yaml loader never adds tzinfo, but converts to UTC.
yaml.FullLoader.add_constructor(
tag="tag:yaml.org,2002:timestamp", constructor=_timestamp_constructor
)
if hasattr(config_file, "read"):
yaml_content = yaml.load(config_file, Loader=yaml.FullLoader)
else:
try:
path_to_config_file = os.path.abspath(config_file) # type: ignore
with open(path_to_config_file, "r") as yamlfile: # type: ignore
yaml_content = yaml.load(yamlfile, Loader=yaml.FullLoader)
except FileNotFoundError:
raise FileNotFoundError(
f"Unable to find config file <{path_to_config_file}>"
)
# Handle multiple versions of workflow config structure
if "spec" in yaml_content:
yaml_content = yaml_content["spec"]["config"]
return yaml_content
示例6: extract_substructure_for_test
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def extract_substructure_for_test(test_case, substructure, config):
"""
Extract the keys from the config in substructure, which may be a nested
dictionary.
Raises a ``unittest.SkipTest`` if the substructure is not found in the
configuration.
This can be used to load credentials all at once for testing purposes.
"""
try:
return extract_substructure(config, substructure)
except MissingConfigError as e:
yaml.add_representer(
Optional,
lambda d, x: d.represent_scalar(u'tag:yaml.org,2002:str', repr(x)))
test_case.skip(
'Skipping test: could not get configuration: {}\n\n'
'In order to run this test, add ensure file at $ACCEPTANCE_YAML '
'has structure like:\n\n{}'.format(
e.message,
yaml.dump(substructure, default_flow_style=False))
)
示例7: test_compactdb
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def test_compactdb(setup):
bdb = BukuDb()
# adding bookmarks
for bookmark in TEST_BOOKMARKS:
bdb.add_rec(*bookmark)
# manually deleting 2nd index from db, calling compactdb
bdb.cur.execute('DELETE FROM bookmarks WHERE id = ?', (2,))
bdb.compactdb(2)
# asserting bookmarks have correct indices
assert bdb.get_rec_by_id(1) == (
1, 'http://slashdot.org', 'SLASHDOT', ',news,old,', "News for old nerds, stuff that doesn't matter", 0)
assert bdb.get_rec_by_id(2) == (
2, 'http://example.com/', 'test', ',es,est,tes,test,', 'a case for replace_tag test', 0)
assert bdb.get_rec_by_id(3) is None
示例8: load_config
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def load_config(file):
"""
Loads a config from the file
:param file: the file from which to load the config
:return: the loaded config represented as a dictionary, might be empty if config file was not found or empty
"""
import yaml
import os
def datetime_constructor(loader, node):
return dateutil.parser.parse(node.value)
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:timestamp', datetime_constructor)
config = None
if file is not None and os.path.exists(file) and os.path.isfile(file):
with open(file, "r") as f:
config = yaml.safe_load(f)
if config is None:
config = {}
return config
示例9: _get_entrypoint
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def _get_entrypoint(data):
# N.B. We can't use data.get() here, because that might return
# None, leading to ambiguity between entrypoint being absent or set
# to a null value.
#
# "Note that a null is different from an empty string and that a
# mapping entry with some key and a null value is valid and
# different from not having that key in the mapping."
# - http://yaml.org/type/null.html
key = 'entrypoint'
if not key in data:
return None
ep = data[key]
# We represent a null value as an empty string.
if ep is None:
ep = ''
if not isinstance(ep, str):
raise ConfigError("'{}' must be a string, not {}".format(
key, type(ep).__name__))
return ep
示例10: get_category_info_string
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def get_category_info_string():
header = '# Copyright (c) 2017 Sony Corporation. All Rights Reserved.\n' \
+ '#\n' \
+ '# Licensed under the Apache License, Version 2.0 (the "License");\n' \
+ '# you may not use this file except in compliance with the License.\n' \
+ '# You may obtain a copy of the License at\n' \
+ '#\n' \
+ '# http://www.apache.org/licenses/LICENSE-2.0\n' \
+ '#\n' \
+ '# Unless required by applicable law or agreed to in writing, software\n' \
+ '# distributed under the License is distributed on an "AS IS" BASIS,\n' \
+ '# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' \
+ '# See the License for the specific language governing permissions and\n' \
+ '# limitations under the License.\n' \
+ '#\n' \
+ '# DO NOT EDIT THIS FILE BY HAND\n' \
+ '# THIS FILE IS GENERATED FROM NNABLA.\n' \
+ '#\n' \
+ '\n'
return header + yaml.dump(_nnabla_func_info, default_flow_style=False)
示例11: yamlDump
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def yamlDump(s):
class YamlOrderedDumper(yaml.SafeDumper):
pass
def represent_ordereddict(dumper, data):
rep = []
for k,v in data.items():
k = dumper.represent_data(k)
v = dumper.represent_data(v)
rep.append((k, v))
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', rep)
YamlOrderedDumper.add_representer(OrderedDict, represent_ordereddict)
return yaml.dump(s, Dumper=YamlOrderedDumper)
示例12: _literal_presenter
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def _literal_presenter(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
示例13: __init__
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def __init__(self, *args, **kwargs):
yaml.Loader.__init__(self, *args, **kwargs)
self.add_constructor(
'tag:yaml.org,2002:map', type(self).construct_yaml_map)
self.add_constructor(
'tag:yaml.org,2002:omap', type(self).construct_yaml_map)
示例14: represent_ordereddict
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def represent_ordereddict(self, data):
return self.represent_mapping('tag:yaml.org,2002:map', data.items())
示例15: string_presenter
# 需要導入模塊: import yaml [as 別名]
# 或者: from yaml import org [as 別名]
def string_presenter(self, dumper, data):
"""Presenter to force yaml.dump to use multi-line string style."""
if '\n' in data:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
else:
return dumper.represent_scalar('tag:yaml.org,2002:str', data)