@@ -67,16 +67,42 @@ def get(self, name, default=None, ktype=None):
67
67
except KeyError :
68
68
return default
69
69
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
72
76
73
77
Raises
74
78
------
79
+ TypeError
80
+ If arguments do not take one of the two expected forms
75
81
KeyError
76
82
If the key does not exist
83
+ ValueError
84
+ If the set value of one of the keys is not the expected value
77
85
"""
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 (f"Unsupported argument type. Expects two arguments consisting \
92
+ of key and value pair, or a dictionary of key value pairs" )
93
+
94
+ for name , value in key_values .items ():
95
+ with raise_keyerror (name ):
96
+ eccodes .codes_set (self ._handle , name , value )
97
+
98
+ # Check values just set
99
+ for name , value in key_values .items ():
100
+ saved_value = self .get (name )
101
+ cast_value = value
102
+ if not isinstance (value , type (saved_value )):
103
+ cast_value = type (saved_value )(value )
104
+ if saved_value != cast_value :
105
+ raise ValueError (f"Unexpected retrieved value { saved_value } . Expected { cast_value } " )
80
106
81
107
def get_array (self , name ):
82
108
"""Get the value of the given key as an array
0 commit comments