Skip to content

Commit 8af9b7b

Browse files
author
Andrew Yang
committed
Remove os cleanup, add read from db file
1 parent ac4d2ff commit 8af9b7b

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/diffpy/utils/parsers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818

1919
from .loaddata import loadData
20-
from .loadmetafile import load_PDF_into_db, markup_PDF, apply_schema_to_file, markup_oneline
20+
from .loadmetafile import load_PDF_into_db, load_from_db, markup_PDF, apply_schema_to_file, markup_oneline
2121
from .resample import resample
2222

2323
# silence the pyflakes syntax checker

src/diffpy/utils/parsers/loadmetafile.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ def load_PDF_into_db(dbname, pdfname, hddata: dict, rv: list, show_path=True):
8080
return pdfs
8181

8282

83+
def load_from_db(filename):
84+
"""Load a dictionary from a database file.
85+
86+
filename -- database file to load from.
87+
88+
Returns a dictionary of database information.
89+
"""
90+
91+
# check if supported type
92+
extension = pathlib.Path(filename).suffix
93+
if extension not in supported_formats:
94+
raise Exception(f"Format of {filename} is not supported.")
95+
96+
# json
97+
if extension == '.json':
98+
with open(filename, 'r') as json_file:
99+
j_dict = json.load(json_file)
100+
101+
return j_dict
102+
103+
83104
def markup_PDF(hddata: dict, rv: list, muname=None):
84105
# FIXME: may be better suited for REST API package, not diffpy.utils
85106
"""Put PDF file information into a dictionary.

src/diffpy/utils/tests/test_loadmetafile.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from diffpy.utils.parsers import load_PDF_into_db, markup_PDF, apply_schema_to_file, markup_oneline
1+
from diffpy.utils.parsers import load_PDF_into_db, load_from_db, markup_PDF, apply_schema_to_file, markup_oneline
22
from diffpy.utils.parsers import loadData
33
from diffpy.utils.tests.testhelpers import datafile
44

@@ -30,9 +30,8 @@ def test_load_gr(tmp_path):
3030
# compare to target
3131
# first compare if base data is same
3232
import json
33-
with open(targetjson, 'r') as target:
34-
target_db_data = json.load(target)
35-
assert target_db_data == db_data
33+
target_db_data = load_from_db(targetjson)
34+
assert target_db_data == db_data
3635
# then compare file structure/organization
3736
assert filecmp.cmp(generatedjson, targetjson)
3837

@@ -48,12 +47,7 @@ def test_markup_gr(tmp_path):
4847

4948
# check against target
5049
# first compare data is same
51-
import json
52-
with open(targetmu, 'r') as target:
53-
target_data = json.load(target)
54-
assert target_data == data
50+
target_data = load_from_db(targetmu)
51+
assert target_data == data
5552
# then compare structure
5653
assert filecmp.cmp(generatedmu, targetmu)
57-
58-
# cleanup
59-
os.remove(generatedmu)

0 commit comments

Comments
 (0)