Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_codes_get_array_errors():
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
with pytest.raises(bindings.GribInternalError) as err:
bindings.codes_get_array(grib, 'values', size=1) # too short
assert err.value.code == bindings.lib.GRIB_ARRAY_TOO_SMALL
with pytest.raises(bindings.GribInternalError) as err:
bindings.codes_get_array(grib, 'values', key_type=int) # wrong type
assert err.value.code == bindings.lib.GRIB_NOT_IMPLEMENTED
def test_codes_get_array(key, expected_value):
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
result = bindings.codes_get_array(grib, key)
assert result == expected_value
def test_codes_get_array_errors():
grib = bindings.codes_grib_new_from_file(open(TEST_DATA))
with pytest.raises(bindings.GribInternalError) as err:
bindings.codes_get_array(grib, 'values', size=1) # too short
assert err.value.code == bindings.lib.GRIB_ARRAY_TOO_SMALL
with pytest.raises(bindings.GribInternalError) as err:
bindings.codes_get_array(grib, 'values', key_type=int) # wrong type
assert err.value.code == bindings.lib.GRIB_NOT_IMPLEMENTED
def message_get(self, item, key_type=None, default=_MARKER):
# type: (str, type, T.Any) -> T.Any
"""Get value of a given key as its native or specified type."""
try:
values = eccodes.codes_get_array(self.codes_id, item, key_type)
if values is None:
values = ['unsupported_key_type']
except eccodes.KeyValueNotFoundError:
if default is _MARKER:
raise KeyError(item)
else:
return default
if len(values) == 1:
if isinstance(values, np.ndarray):
values = values.tolist()
return values[0]
return values