diff --git a/docs/source/api.rst b/docs/source/api.rst index 7cd57268aa67b4f1a77c00d486933ec0736a219a..868ddb60099f5fe6ddd21a5998ffc12342df4130 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -7,6 +7,11 @@ NodeJob.py .. autoclass:: tree_maker.NodeJob.NodeJob :members: +general.py +------ +.. automodule:: tree_maker.general + :members: + tag.py ------ .. automodule:: tree_maker.tag diff --git a/tree_maker/__init__.py b/tree_maker/__init__.py index fa42a9e051012c1a45dfb5054efbdeee18d09f26..e88203331ce136deb4e2e098d848009f547d5567 100644 --- a/tree_maker/__init__.py +++ b/tree_maker/__init__.py @@ -11,6 +11,7 @@ from .general import tree_from_yaml from .general import tree_from_json from .general import from_yaml from .general import from_json +from .general import py_to_yaml from .tag import * from .tag_json import * diff --git a/tree_maker/general.py b/tree_maker/general.py index f7f8ce0e17b74a341b37a6aea4faa17c8b15f64a..bcdddb0288d0007fd3afe664f6212381a44dca3f 100644 --- a/tree_maker/general.py +++ b/tree_maker/general.py @@ -10,14 +10,17 @@ import orjson ryaml = ruamel.yaml.YAML() def tree_from_yaml(filename='tree.yaml'): + '''Import the tree structure from the yaml formatted *filename*''' with open(filename, "r") as file: return DictImporter(nodecls=NodeJob).import_(yaml.load(file, Loader=yaml.FullLoader)) def tree_from_json(filename='tree.json'): + '''Import the tree structure from the json formatted *filename*''' with open(filename, "r") as file: return DictImporter(nodecls=NodeJob).import_(orjson.loads(file.read())) def from_yaml(filename): + '''Load the *filename* yaml file''' try: with open(filename, 'r') as file: return ryaml.load(file) @@ -26,9 +29,17 @@ def from_yaml(filename): return {} def from_json(filename, verbose=False): + '''Load the *filename* json file''' try: with open(filename, 'r') as file: return orjson.loads(file.read()) except Exception as e: if verbose: print(e) return {} + +def py_to_yaml(filename): + '''Convert the *filename*.py (containg dictionaries) to an + equivalent *filename*.yaml''' + import filename as my_dict + with open(filename+'.yaml', 'w') as fid: + yaml.dump(my_dict, fid)