Skip to content

Commit 42ad689

Browse files
save progress
1 parent 2fc3ddd commit 42ad689

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,6 @@
55

66
from diffpy.labpdfproc.tools import load_user_metadata, set_wavelength
77

8-
params5 = [
9-
([[]], []),
10-
([["toast=for breakfast"]], [["toast", "for breakfast"]]),
11-
([["mylist=[1,2,3.0]"]], [["mylist", "[1,2,3.0]"]]),
12-
([["weather=rainy", "day=tuesday"]], [["weather", "rainy"], ["day", "tuesday"]]),
13-
]
14-
15-
16-
@pytest.mark.parametrize("inputs, expected", params5)
17-
def test_load_user_metadata(inputs, expected):
18-
actual_parser = ArgumentParser()
19-
actual_parser.add_argument("-u", "--user-metadata", action="append", metavar="KEY=VALUE", nargs="+")
20-
actual_args = actual_parser.parse_args([])
21-
expected_parser = ArgumentParser()
22-
expected_args = expected_parser.parse_args([])
23-
24-
setattr(actual_args, "user_metadata", inputs[0])
25-
actual_args = load_user_metadata(actual_args)
26-
for expected_pair in expected:
27-
setattr(expected_args, expected_pair[0], expected_pair[1])
28-
assert actual_args == expected_args
29-
30-
318
params2 = [
329
([None, None], [0.71]),
3310
([None, "Ag"], [0.59]),
@@ -56,3 +33,26 @@ def test_set_wavelength_bad(inputs):
5633
with pytest.raises(ValueError):
5734
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
5835
actual_args.wavelength = set_wavelength(actual_args)
36+
37+
38+
params5 = [
39+
([[]], []),
40+
([["toast=for breakfast"]], [["toast", "for breakfast"]]),
41+
([["mylist=[1,2,3.0]"]], [["mylist", "[1,2,3.0]"]]),
42+
([["weather=rainy", "day=tuesday"]], [["weather", "rainy"], ["day", "tuesday"]]),
43+
]
44+
45+
46+
@pytest.mark.parametrize("inputs, expected", params5)
47+
def test_load_user_metadata(inputs, expected):
48+
actual_parser = ArgumentParser()
49+
actual_parser.add_argument("-u", "--user-metadata", action="append", metavar="KEY=VALUE", nargs="+")
50+
actual_args = actual_parser.parse_args([])
51+
expected_parser = ArgumentParser()
52+
expected_args = expected_parser.parse_args([])
53+
54+
setattr(actual_args, "user_metadata", inputs[0])
55+
actual_args = load_user_metadata(actual_args)
56+
for expected_pair in expected:
57+
setattr(expected_args, expected_pair[0], expected_pair[1])
58+
assert actual_args == expected_args

src/diffpy/labpdfproc/tools.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,6 @@
22
known_sources = [key for key in WAVELENGTHS.keys()]
33

44

5-
def load_key_value_pair(s):
6-
items = s.split("=")
7-
key = items[0].strip()
8-
if len(items) > 1:
9-
value = "=".join(items[1:])
10-
return (key, value)
11-
12-
13-
def load_user_metadata(args):
14-
if args.user_metadata:
15-
for item in args.user_metadata:
16-
if "=" not in item:
17-
raise ValueError("please provide key-value pairs in the format key=value.")
18-
key, value = load_key_value_pair(item)
19-
setattr(args, key, value)
20-
delattr(args, "user_metadata")
21-
return args
22-
23-
24-
# Notes, can ignore
25-
# ideal inputs: "key1=value1" "key2=value2"
26-
# (different pairs are separated by white spaces, key and value are separated by =)
27-
28-
# question 1: white spaces in key, value, both?
29-
# if value contains whitespace, you can specify key="value value" or "key=value value"
30-
# if key contains whitespace, for example, you have "key key=value",
31-
# then you can access like getattr(args, 'key key'), but we dont recommend this
32-
33-
# question 2: more than one =?
34-
# if i have key is hello, value is hello=world, then i can have hello=hello=world to process okay
35-
# if i have = in key then it's still processing as value => would this be an issue?
36-
37-
385
def set_wavelength(args):
396
"""
407
Set the wavelength based on the given input arguments
@@ -67,3 +34,36 @@ def set_wavelength(args):
6734
return WAVELENGTHS[args.anode_type]
6835
else:
6936
return WAVELENGTHS["Mo"]
37+
38+
39+
def _load_key_value_pair(s):
40+
items = s.split("=")
41+
key = items[0].strip()
42+
if len(items) > 1:
43+
value = "=".join(items[1:])
44+
return (key, value)
45+
46+
47+
def load_user_metadata(args):
48+
if args.user_metadata:
49+
for item in args.user_metadata:
50+
if "=" not in item:
51+
raise ValueError("please provide key-value pairs in the format key=value.")
52+
key, value = _load_key_value_pair(item)
53+
setattr(args, key, value)
54+
delattr(args, "user_metadata")
55+
return args
56+
57+
58+
# Notes, can ignore
59+
# ideal inputs: "key1=value1" "key2=value2"
60+
# (different pairs are separated by white spaces, key and value are separated by =)
61+
62+
# question 1: white spaces in key, value, both?
63+
# if value contains whitespace, you can specify key="value value" or "key=value value"
64+
# if key contains whitespace, for example, you have "key key=value",
65+
# then you can access like getattr(args, 'key key'), but we dont recommend this
66+
67+
# question 2: more than one =?
68+
# if i have key is hello, value is hello=world, then i can have hello=hello=world to process okay
69+
# if i have = in key then it's still processing as value => would this be an issue?

0 commit comments

Comments
 (0)