Skip to content

Commit 00906f5

Browse files
jinmannwongshahramn
authored andcommitted
Pull request #3: Feature/set multiple
Merge in ECCODES/eccodes-python from feature/set_multiple to develop * commit 'b523e5abd4dd0b810bfe32079d05e55b9906e12f': Black formatting Improve value error message Improve value error message Fix black formatting using v22.1.0 Formatting Modify set function in message to accept dictionary of key values and add check on values
2 parents 06adfa6 + b523e5a commit 00906f5

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

eccodes/highlevel/message.py

+34-4
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,46 @@ def get(self, name, default=None, ktype=None):
6767
except KeyError:
6868
return default
6969

70-
def set(self, name, value):
71-
"""Set the value of the given key
70+
def set(self, *args):
71+
"""If two arguments are given, assumes this takes form of a single key
72+
value pair and sets the value of the given key. If a dictionary is passed
73+
then sets the values of all keys in the dictionary. Note, ordering
74+
if the keys is important. Finally, checks if value
75+
has been set correctly
7276
7377
Raises
7478
------
79+
TypeError
80+
If arguments do not take one of the two expected forms
7581
KeyError
7682
If the key does not exist
83+
ValueError
84+
If the set value of one of the keys is not the expected value
7785
"""
78-
with raise_keyerror(name):
79-
return eccodes.codes_set(self._handle, name, value)
86+
if isinstance(args[0], str) and len(args) == 2:
87+
key_values = {args[0]: args[1]}
88+
elif isinstance(args[0], dict):
89+
key_values = args[0]
90+
else:
91+
raise TypeError(
92+
f"Unsupported argument type. Expects two arguments consisting \
93+
of key and value pair, or a dictionary of key-value pairs"
94+
)
95+
96+
for name, value in key_values.items():
97+
with raise_keyerror(name):
98+
eccodes.codes_set(self._handle, name, value)
99+
100+
# Check values just set
101+
for name, value in key_values.items():
102+
saved_value = self.get(name)
103+
cast_value = value
104+
if not isinstance(value, type(saved_value)):
105+
cast_value = type(saved_value)(value)
106+
if saved_value != cast_value:
107+
raise ValueError(
108+
f"Unexpected retrieved value {saved_value} for key {name}. Expected {cast_value}"
109+
)
80110

81111
def get_array(self, name):
82112
"""Get the value of the given key as an array

tests/test_highlevel.py

+17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ def test_message_set():
6565
assert message.is_missing(missing_key)
6666

6767

68+
def test_message_set_multiple():
69+
with eccodes.FileReader(TEST_GRIB_DATA) as reader:
70+
message = next(reader)
71+
message.set(
72+
{
73+
"centre": "ecmf",
74+
"numberOfValues": 10,
75+
"shortName": "z",
76+
}
77+
)
78+
with pytest.raises(TypeError):
79+
message.set("centre", "ecmwf", 2)
80+
with pytest.raises(ValueError):
81+
message.set("stepRange", "0-12")
82+
message.set({"stepType": "max", "stepRange": "0-12"})
83+
84+
6885
def test_message_iter():
6986
with eccodes.FileReader(TEST_GRIB_DATA2) as reader:
7087
message = next(reader)

0 commit comments

Comments
 (0)