Python yaml.dump() Examples
The following are 30
code examples of yaml.dump().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
yaml
, or try the search function
.
Example #1
Source File: files.py From glazier with Apache License 2.0 | 8 votes |
def Dump(path: Text, data: Any, mode: Text = 'w'): """Write a config file containing some data. Args: path: The filesystem path to the destination file. data: Data to be written to the file as yaml. mode: Mode to use for writing the file (default: w) """ file_util.CreateDirectories(path) tmp_f = path + '.tmp' # Write to a .tmp file to avoid corrupting the original if aborted mid-way. try: with open(tmp_f, mode) as handle: handle.write(yaml.dump(data)) except IOError as e: raise Error('Could not save data to yaml file %s: %s' % (path, str(e))) # Replace the original with the tmp. try: file_util.Move(tmp_f, path) except file_util.Error as e: raise Error('Could not replace config file. (%s)' % str(e))
Example #2
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def toYAML(self, **options): """ Serializes this Munch to YAML, using `yaml.safe_dump()` if no `Dumper` is provided. See the PyYAML documentation for more info. >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42) >>> import yaml >>> yaml.safe_dump(b, default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> b.toYAML(default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> yaml.dump(b, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' >>> b.toYAML(Dumper=yaml.Dumper, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' """ opts = dict(indent=4, default_flow_style=False) opts.update(options) if 'Dumper' not in opts: return yaml.safe_dump(self, **opts) else: return yaml.dump(self, **opts)
Example #3
Source File: prepare_model_yaml.py From models with MIT License | 6 votes |
def make_secondary_dl_yaml(template_yaml, model_json, output_yaml_path): with open(template_yaml, 'r') as f: model_yaml = yaml.load(f) # # get the model config: json_file = open(model_json, 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = keras.models.model_from_json(loaded_model_json) # model_yaml["output_schema"]["targets"] = [] for oname, oshape in zip(loaded_model.output_names, loaded_model.output_shape): append_el ={"name":oname , "shape":str(oshape)#replace("None,", "") , "doc":"Methylation probability for %s"%oname} model_yaml["output_schema"]["targets"].append(append_el) # with open(output_yaml_path, 'w') as f: yaml.dump(model_yaml, f, default_flow_style=False)
Example #4
Source File: prepare_model_yaml.py From models with MIT License | 6 votes |
def make_model_yaml(template_yaml, model_json, output_yaml_path): # with open(template_yaml, 'r') as f: model_yaml = yaml.load(f) # # get the model config: json_file = open(model_json, 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = keras.models.model_from_json(loaded_model_json) # model_yaml["schema"]["targets"] = [] for oname, oshape in zip(loaded_model.output_names, loaded_model.output_shape): append_el ={"name":oname , "shape":str(oshape)#replace("None,", "") , "doc":"Methylation probability for %s"%oname} model_yaml["schema"]["targets"].append(append_el) # with open(output_yaml_path, 'w') as f: yaml.dump(model_yaml, f, default_flow_style=False)
Example #5
Source File: test_cli_networks.py From brownie with MIT License | 6 votes |
def test_import_id_exists(tmp_path): path = tmp_path.joinpath("exported.yaml") with path.open("w") as fp: yaml.dump( { "live": [ { "name": "FooChain", "networks": [{"id": "mainnet", "host": "127.0.0.1", "chainid": 42}], } ] }, fp, ) with pytest.raises(ValueError): cli_networks._import(path.as_posix())
Example #6
Source File: test_cli_networks.py From brownie with MIT License | 6 votes |
def test_import_from_nothing(tmp_path, config): path = tmp_path.joinpath("exported.yaml") with _get_data_folder().joinpath("network-config.yaml").open() as fp: networks = yaml.safe_load(fp) with path.open("w") as fp: yaml.dump(networks, fp) for key in config.networks.keys(): cli_networks._delete(key) config.networks = {} cli_networks._import(path.as_posix()) with _get_data_folder().joinpath("network-config.yaml").open() as fp: networks = yaml.safe_load(fp) with path.open() as fp: exported = yaml.safe_load(fp) assert networks == exported
Example #7
Source File: test_project_structure.py From brownie with MIT License | 6 votes |
def test_compiles(project, tmp_path): with tmp_path.joinpath("brownie-config.yaml").open("w") as fp: yaml.dump(structure, fp) tmp_path.joinpath("sources").mkdir() with tmp_path.joinpath("sources/Foo.vy").open("w") as fp: fp.write( """ @external def foo() -> int128: return 2 """ ) tmp_path.joinpath("abi").mkdir() with tmp_path.joinpath("abi/Bar.json").open("w") as fp: fp.write("[]") proj = project.load(tmp_path) assert "Foo" in proj._containers assert proj._path.joinpath("artifacts/contracts/Foo.json").exists() assert hasattr(proj.interface, "Bar")
Example #8
Source File: networks.py From brownie with MIT License | 6 votes |
def _delete(id_): if id_ not in CONFIG.networks: raise ValueError(f"Network '{color('bright magenta')}{id_}{color}' does not exist") with _get_data_folder().joinpath("network-config.yaml").open() as fp: networks = yaml.safe_load(fp) if "cmd" in CONFIG.networks[id_]: networks["development"] = [i for i in networks["development"] if i["id"] != id_] else: target = next(i for i in networks["live"] for x in i["networks"] if x["id"] == id_) target["networks"] = [i for i in target["networks"] if i["id"] != id_] networks["live"] = [i for i in networks["live"] if i["networks"]] with _get_data_folder().joinpath("network-config.yaml").open("w") as fp: yaml.dump(networks, fp) notify("SUCCESS", f"Network '{color('bright magenta')}{id_}{color}' has been deleted")
Example #9
Source File: subprocess.py From Parsing-R-CNN with MIT License | 6 votes |
def log_subprocess_output(i, p, ckpt_path, tag, start, end): """Capture the output of each subprocess and log it in the parent process. The first subprocess's output is logged in realtime. The output from the other subprocesses is buffered and then printed all at once (in order) when subprocesses finish. """ outfile = os.path.join(ckpt_path, 'test', '%s_range_%s_%s.stdout' % (tag, start, end)) logging_rank('# ' + '-' * 76 + ' #') logging_rank('stdout of subprocess %s with range [%s, %s]' % (i, start + 1, end)) logging_rank('# ' + '-' * 76 + ' #') if i == 0: # Stream the piped stdout from the first subprocess in realtime with open(outfile, 'w') as f: for line in iter(p.stdout.readline, b''): print(line.rstrip().decode('ascii')) f.write(str(line, encoding='ascii')) p.stdout.close() ret = p.wait() else: # For subprocesses >= 1, wait and dump their log file ret = p.wait() with open(outfile, 'r') as f: print(''.join(f.readlines())) assert ret == 0, 'Range subprocess failed (exit code: {})'.format(ret)
Example #10
Source File: build.py From operator-courier with Apache License 2.0 | 6 votes |
def _updateBundle(self, operatorBundle, file_name, yaml_string): # Determine which operator file type the yaml is operator_artifact = identify.get_operator_artifact_type(yaml_string) # If the file isn't one of our special types, we ignore it and return if operator_artifact == identify.UNKNOWN_FILE: return operatorBundle # Get the array name expected by the dictionary for the given file type op_artifact_plural = operator_artifact[0:1].lower() + operator_artifact[1:] + 's' # Marshal the yaml into a dictionary yaml_data = yaml.safe_load(yaml_string) # Add the data dictionary to the correct list operatorBundle["data"][op_artifact_plural].append(yaml_data) # Encode the dictionary into a string, then use that as a key to reference # the file name associated with that yaml file. Then add it to the metadata. if file_name != "": unencoded_yaml = yaml.dump(yaml_data) relative_path = self._get_relative_path(file_name) operatorBundle["metadata"]["filenames"][hash(unencoded_yaml)] = relative_path return operatorBundle
Example #11
Source File: test_config.py From esmlab with Apache License 2.0 | 6 votes |
def test_collect_yaml_permission_errors(tmpdir, kind): a = {'x': 1, 'y': 2} b = {'y': 3, 'z': 4} dir_path = str(tmpdir) a_path = os.path.join(dir_path, 'a.yaml') b_path = os.path.join(dir_path, 'b.yaml') with open(a_path, mode='w') as f: yaml.dump(a, f) with open(b_path, mode='w') as f: yaml.dump(b, f) if kind == 'directory': cant_read = dir_path expected = {} else: cant_read = a_path expected = b with no_read_permissions(cant_read): config = merge(*collect_yaml(paths=[dir_path])) assert config == expected
Example #12
Source File: test_config.py From esmlab with Apache License 2.0 | 6 votes |
def test_ensure_file_defaults_to_ESMLAB_CONFIG_directory(tmpdir): a = {'x': 1, 'y': {'a': 1}} source = os.path.join(str(tmpdir), 'source.yaml') with open(source, 'w') as f: yaml.dump(a, f) destination = os.path.join(str(tmpdir), 'esmlab') PATH = esmlab.config.PATH try: esmlab.config.PATH = destination ensure_file(source=source) finally: esmlab.config.PATH = PATH assert os.path.isdir(destination) [fn] = os.listdir(destination) assert os.path.split(fn)[1] == os.path.split(source)[1]
Example #13
Source File: regional_multi_vm.py From google-compute-engine-dse with Apache License 2.0 | 6 votes |
def GenerateConfig(context): config = {'resources': []} # A zonal vm_multiple_instances resource for each zone in the properties list. for zone in context.properties['zones']: new_properties = copy.deepcopy(context.properties) new_properties['zone'] = zone service = { 'name': context.env['deployment'] + '-' + zone, 'type': 'vm_multiple_instances.py', 'properties': new_properties } config['resources'].append(service) return yaml.dump(config)
Example #14
Source File: arg_helper.py From LanczosNetwork with MIT License | 6 votes |
def get_config(config_file, exp_dir=None): """ Construct and snapshot hyper parameters """ config = edict(yaml.load(open(config_file, 'r'))) # create hyper parameters config.run_id = str(os.getpid()) config.exp_name = '_'.join([ config.model.name, config.dataset.name, time.strftime('%Y-%b-%d-%H-%M-%S'), config.run_id ]) if exp_dir is not None: config.exp_dir = exp_dir config.save_dir = os.path.join(config.exp_dir, config.exp_name) # snapshot hyperparameters mkdir(config.exp_dir) mkdir(config.save_dir) save_name = os.path.join(config.save_dir, 'config.yaml') yaml.dump(edict2dict(config), open(save_name, 'w'), default_flow_style=False) return config
Example #15
Source File: template.py From landmarkerio-server with BSD 3-Clause "New" or "Revised" License | 6 votes |
def convert_legacy_template(path): with open(path) as f: ta = f.read().strip().split('\n\n') groups = [parse_group(g) for g in ta] data = {'groups': [group_to_dict(g) for g in groups]} new_path = path[:-3] + 'yml' warning = '' if p.isfile(new_path): new_path = path[:-4] + '-converted.yml' warning = '(appended -converted to avoid collision)' with open(new_path, 'w') as nf: yaml.dump(data, nf, indent=4, default_flow_style=False) os.remove(path) print " - {} > {} {}".format(path, new_path, warning)
Example #16
Source File: generate_from_openapi.py From normandy with Mozilla Public License 2.0 | 6 votes |
def output_yaml(links, prefix=""): test_dict = {} for test_name in links.keys(): default_name = get_name(prefix, test_name, links[test_name].action, links[test_name].url) test_dict["test_name"] = default_name request = {"url": links[test_name].url, "method": str.upper(links[test_name].action)} if links[test_name].encoding: request["headers"] = {"content-type": links[test_name].encoding} json = get_request_placeholders(links[test_name].fields) if json and request["method"] != "GET": request["json"] = json response = {"strict": False, "status_code": 200} inner_dict = {"name": default_name, "request": request, "response": response} test_dict["stages"] = [inner_dict] print(yaml.dump(test_dict, explicit_start=True, default_flow_style=False))
Example #17
Source File: ex1_run_config_chg.py From python_course with Apache License 2.0 | 6 votes |
def save_objects_to_file(file_name, data_dict): """Write the network devices out to a file.""" # Determine whether .pkl, .yml, or .json file if file_name.count(".") == 1: _, out_format = file_name.split(".") else: raise ValueError("Invalid file name: {}".format(file_name)) if out_format == 'pkl': with open(file_name, 'wb') as f: pickle.dump(data_dict, f) elif out_format == 'yml': with open(file_name, 'w') as f: f.write(yaml.dump(data_dict, default_flow_style=False)) elif out_format == 'json': with open(file_name, 'w') as f: json.dump(data_dict, f)
Example #18
Source File: request_to_yaml.py From ftw with Apache License 2.0 | 6 votes |
def generate_yaml(self): data = dict( meta=dict( author="Zack", enabled=True, name="EXAMPLE.yaml", description="Description" ), tests=[dict( rule_id=1234, stages=[dict( stage=dict( input=self.input, output=dict( status=200 ) ) )] )] ) return yaml.dump(data, default_flow_style=False)
Example #19
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def toYAML(self, **options): """ Serializes this Munch to YAML, using `yaml.safe_dump()` if no `Dumper` is provided. See the PyYAML documentation for more info. >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42) >>> import yaml >>> yaml.safe_dump(b, default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> b.toYAML(default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> yaml.dump(b, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' >>> b.toYAML(Dumper=yaml.Dumper, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' """ opts = dict(indent=4, default_flow_style=False) opts.update(options) if 'Dumper' not in opts: return yaml.safe_dump(self, **opts) else: return yaml.dump(self, **opts)
Example #20
Source File: test_config.py From esmlab with Apache License 2.0 | 6 votes |
def test_collect(): a = {'x': 1, 'y': {'a': 1}} b = {'x': 2, 'z': 3, 'y': {'b': 2}} env = {'ESMLAB_W': 4} expected = {'w': 4, 'x': 2, 'y': {'a': 1, 'b': 2}, 'z': 3} with tmpfile(extension='yaml') as fn1: with tmpfile(extension='yaml') as fn2: with open(fn1, 'w') as f: yaml.dump(a, f) with open(fn2, 'w') as f: yaml.dump(b, f) config = collect([fn1, fn2], env=env) assert config == expected
Example #21
Source File: util.py From derplearning with MIT License | 5 votes |
def dump_config(config, config_path): """ Write a configuration file """ with open(str(config_path), 'w') as config_fd: yaml.dump(config, config_fd)
Example #22
Source File: utils.py From qutebrowser with GNU General Public License v3.0 | 5 votes |
def yaml_dump(data: typing.Any, f: typing.IO[str] = None) -> typing.Optional[str]: """Wrapper over yaml.dump using the C dumper if possible. Also returns a str instead of bytes. """ yaml_data = yaml.dump(data, f, Dumper=YamlDumper, default_flow_style=False, encoding='utf-8', allow_unicode=True) if yaml_data is None: return None else: return yaml_data.decode('utf-8')
Example #23
Source File: train.py From hierarchical_loc with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _cli_train(config, output_dir): assert 'train_iter' in config with open(os.path.join(output_dir, 'config.yml'), 'w') as f: yaml.dump(config, f, default_flow_style=False) train(config, config['train_iter'], output_dir)
Example #24
Source File: common.py From loaner with Apache License 2.0 | 5 votes |
def write(self): """Writes the project config for the provided key to disk.""" with open(self.path) as config_file: configs = yaml.safe_load(config_file) config = configs.get(self.key) or {} config['project_id'] = self.project config['client_id'] = self.client_id config['client_secret'] = self.client_secret config['bucket'] = self.bucket configs[self.key] = config with open(self.path, 'w') as config_file: yaml.dump(configs, config_file, default_flow_style=False)
Example #25
Source File: main.py From schema.data.gouv.fr with MIT License | 5 votes |
def save_schemas(self): # Save in YAML with open("data/schemas.yml", "w") as f: yaml.dump(self.get(), f, allow_unicode=True) # Save in JSON with open("data/schemas.json", "w") as f: json.dump(self.generate_json(), f, ensure_ascii=False)
Example #26
Source File: config.py From kubeshift with GNU Lesser General Public License v3.0 | 5 votes |
def write_file(self): """Save kubeconfig content to disk.""" config_path_dir = os.path.dirname(self.filepath) if not os.path.exists(config_path_dir): os.makedirs(config_path_dir) yaml.dump(self.content, open(self.filepath, 'w'))
Example #27
Source File: __init__.py From ripe-atlas-tools with GNU General Public License v3.0 | 5 votes |
def write(aliases): if not os.path.exists(AliasesDB.USER_CONFIG_DIR): os.makedirs(AliasesDB.USER_CONFIG_DIR) payload = yaml.dump( aliases, default_flow_style=False ) with open(AliasesDB.USER_RC, "w") as rc: rc.write(payload)
Example #28
Source File: networks.py From brownie with MIT License | 5 votes |
def _add(env, id_, *args): if id_ in CONFIG.networks: raise ValueError(f"Network '{color('bright magenta')}{id_}{color}' already exists") args = _parse_args(args) if "name" not in args: args["name"] = id_ with _get_data_folder().joinpath("network-config.yaml").open() as fp: networks = yaml.safe_load(fp) if env.lower() == "development": new = { "name": args.pop("name"), "id": id_, "cmd": args.pop("cmd"), "host": args.pop("host"), } if "timeout" in args: new["timeout"] = args.pop("timeout") new["cmd_settings"] = args _validate_network(new, DEV_REQUIRED) networks["development"].append(new) else: target = next( (i["networks"] for i in networks["live"] if i["name"].lower() == env.lower()), None ) if target is None: networks["live"].append({"name": env, "networks": []}) target = networks["live"][-1]["networks"] new = {"id": id_, **args} _validate_network(new, PROD_REQUIRED) target.append(new) with _get_data_folder().joinpath("network-config.yaml").open("w") as fp: yaml.dump(networks, fp) notify( "SUCCESS", f"A new network '{color('bright magenta')}{new['name']}{color}' has been added" ) _print_verbose_network_description(new, True)
Example #29
Source File: local_mode.py From sagemaker-xgboost-container with Apache License 2.0 | 5 votes |
def write_json_file(filename, content): with open(filename, 'w') as f: json.dump(content, f)
Example #30
Source File: ex6_yaml_json_write.py From python_course with Apache License 2.0 | 5 votes |
def main(): """ Write a Python program that creates a list. One of the elements of the list should be a dictionary with at least two keys. Write this list out to a file using both YAML and JSON formats. The YAML file should be in the expanded form. """ yaml_file = 'my_test.yml' json_file = 'my_test.json' my_dict = { 'ip_addr': '172.31.200.1', 'platform': 'cisco_ios', 'vendor': 'cisco', 'model': '1921' } my_list = [ 'some string', 99, 18, my_dict, 'another string', 'final string' ] with open(yaml_file, "w") as f: f.write(yaml.dump(my_list, default_flow_style=False)) with open(json_file, "w") as f: json.dump(my_list, f)