Skip to content
Snippets Groups Projects
Commit a7ccd1fa authored by Guido Sterbini's avatar Guido Sterbini
Browse files

Adding the py_to_yaml

parent 793561c7
No related branches found
No related tags found
2 merge requests!2Dev v0.0.1,!1Dev v0.0.1
Pipeline #2790146 passed
...@@ -7,6 +7,11 @@ NodeJob.py ...@@ -7,6 +7,11 @@ NodeJob.py
.. autoclass:: tree_maker.NodeJob.NodeJob .. autoclass:: tree_maker.NodeJob.NodeJob
:members: :members:
general.py
------
.. automodule:: tree_maker.general
:members:
tag.py tag.py
------ ------
.. automodule:: tree_maker.tag .. automodule:: tree_maker.tag
......
...@@ -11,6 +11,7 @@ from .general import tree_from_yaml ...@@ -11,6 +11,7 @@ from .general import tree_from_yaml
from .general import tree_from_json from .general import tree_from_json
from .general import from_yaml from .general import from_yaml
from .general import from_json from .general import from_json
from .general import py_to_yaml
from .tag import * from .tag import *
from .tag_json import * from .tag_json import *
......
...@@ -10,14 +10,17 @@ import orjson ...@@ -10,14 +10,17 @@ import orjson
ryaml = ruamel.yaml.YAML() ryaml = ruamel.yaml.YAML()
def tree_from_yaml(filename='tree.yaml'): def tree_from_yaml(filename='tree.yaml'):
'''Import the tree structure from the yaml formatted *filename*'''
with open(filename, "r") as file: with open(filename, "r") as file:
return DictImporter(nodecls=NodeJob).import_(yaml.load(file, Loader=yaml.FullLoader)) return DictImporter(nodecls=NodeJob).import_(yaml.load(file, Loader=yaml.FullLoader))
def tree_from_json(filename='tree.json'): def tree_from_json(filename='tree.json'):
'''Import the tree structure from the json formatted *filename*'''
with open(filename, "r") as file: with open(filename, "r") as file:
return DictImporter(nodecls=NodeJob).import_(orjson.loads(file.read())) return DictImporter(nodecls=NodeJob).import_(orjson.loads(file.read()))
def from_yaml(filename): def from_yaml(filename):
'''Load the *filename* yaml file'''
try: try:
with open(filename, 'r') as file: with open(filename, 'r') as file:
return ryaml.load(file) return ryaml.load(file)
...@@ -26,9 +29,17 @@ def from_yaml(filename): ...@@ -26,9 +29,17 @@ def from_yaml(filename):
return {} return {}
def from_json(filename, verbose=False): def from_json(filename, verbose=False):
'''Load the *filename* json file'''
try: try:
with open(filename, 'r') as file: with open(filename, 'r') as file:
return orjson.loads(file.read()) return orjson.loads(file.read())
except Exception as e: except Exception as e:
if verbose: print(e) if verbose: print(e)
return {} 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment