本文整理汇总了Python中ruamel.yaml.safe_dump方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.safe_dump方法的具体用法?Python yaml.safe_dump怎么用?Python yaml.safe_dump使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ruamel.yaml
的用法示例。
在下文中一共展示了yaml.safe_dump方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_tmp_file_from_contents
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def create_tmp_file_from_contents(rname, data, ftype='yaml'):
''' create a file in tmp with name and contents'''
tmp = Utils.create_tmpfile(prefix=rname)
if ftype == 'yaml':
# AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
# pylint: disable=no-member
if hasattr(yaml, 'RoundTripDumper'):
Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
else:
Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))
elif ftype == 'json':
Utils._write(tmp, json.dumps(data))
else:
Utils._write(tmp, data)
# Register cleanup when module is done
atexit.register(Utils.cleanup, [tmp])
return tmp
示例2: create
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def create(self, files=None, content=None):
'''
Create a config
NOTE: This creates the first file OR the first conent.
TODO: Handle all files and content passed in
'''
if files:
return self._create(files[0])
# pylint: disable=no-member
# The purpose of this change is twofold:
# - we need a check to only use the ruamel specific dumper if ruamel is loaded
# - the dumper or the flow style change is needed so openshift is able to parse
# the resulting yaml, at least until gopkg.in/yaml.v2 is updated
if hasattr(yaml, 'RoundTripDumper'):
content['data'] = yaml.dump(content['data'], Dumper=yaml.RoundTripDumper)
else:
content['data'] = yaml.safe_dump(content['data'], default_flow_style=False)
content_file = Utils.create_tmp_files_from_contents(content)[0]
return self._create(content_file['path'])
# pylint: disable=too-many-function-args
示例3: write
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def write(self):
''' write to file '''
if not self.filename:
raise YeditException('Please specify a filename.')
if self.backup and self.file_exists():
shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))
# Try to set format attributes if supported
try:
self.yaml_dict.fa.set_block_style()
except AttributeError:
pass
# Try to use RoundTripDumper if supported.
if self.content_type == 'yaml':
try:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
except AttributeError:
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
elif self.content_type == 'json':
Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
else:
raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
'Please specify a content_type of yaml or json.')
return (True, self.yaml_dict)
示例4: handleFile
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def handleFile(self, f, *args, **kwargs):
super().handleFile(f)
if self.audio_file and self.audio_file.info and self.audio_file.tag:
print(yaml.safe_dump(audioFileToJson(self.audio_file),
indent=2, default_flow_style=False,
explicit_start=True))
示例5: dump_yaml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def dump_yaml(data, *path):
def convert(obj):
if isinstance(obj, dict):
obj = {k: v for k, v in obj.items() if not k.startswith('_')}
return {convert(k): convert(v) for k, v in obj.items()}
if isinstance(obj, list):
return [convert(x) for x in obj]
if isinstance(obj, type):
return obj.__name__
return obj
filename = os.path.join(*path)
ensure_directory(os.path.dirname(filename))
with open(filename, 'w') as file_:
yaml.safe_dump(convert(data), file_, default_flow_style=False)
示例6: _write_yaml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def _write_yaml(self, config_dict, dest):
with io.open(dest, 'w', encoding='UTF-8') as f:
yaml.safe_dump(config_dict, f, default_flow_style=False)
# Metadata gen
示例7: test_model_hot_reloading
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def test_model_hot_reloading(app, rasa_default_train_data):
query = "http://dummy-uri/parse?q=hello&project=my_keyword_model"
response = yield app.get(query)
assert response.code == 404, "Project should not exist yet"
train_u = "http://dummy-uri/train?project=my_keyword_model"
model_config = {"pipeline": "keyword", "data": rasa_default_train_data}
model_str = yaml.safe_dump(model_config, default_flow_style=False,
allow_unicode=True)
response = app.post(train_u,
headers={b"Content-Type": b"application/x-yml"},
data=model_str)
time.sleep(3)
app.flush()
response = yield response
assert response.code == 200, "Training should end successfully"
response = app.post(train_u,
headers={b"Content-Type": b"application/json"},
data=json.dumps(model_config))
time.sleep(3)
app.flush()
response = yield response
assert response.code == 200, "Training should end successfully"
response = yield app.get(query)
assert response.code == 200, "Project should now exist after it got trained"
示例8: write_file_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def write_file_config(file_config):
with tempfile.NamedTemporaryFile("w+",
suffix="_tmp_config_file.yml",
delete=False) as f:
f.write(yaml.safe_dump(file_config))
f.flush()
return f
示例9: write_file_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import safe_dump [as 别名]
def write_file_config(file_config):
with tempfile.NamedTemporaryFile(
"w+", suffix="_tmp_config_file.yml", delete=False
) as f:
f.write(yaml.safe_dump(file_config))
f.flush()
return f