From 35d5bffa363a12a5e122c78b9572dc1e3235e713 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Mon, 19 Dec 2016 14:53:34 +0000 Subject: [PATCH] SUP-1861: High level Python API for GRIB in ecCodes --- examples/python/high_level_api.py | 4 ++-- python/eccodes/high_level/gribfile.py | 2 +- python/eccodes/high_level/gribindex.py | 6 +++--- python/eccodes/high_level/gribmessage.py | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/python/high_level_api.py b/examples/python/high_level_api.py index 3c83b30e4..cac39569b 100644 --- a/examples/python/high_level_api.py +++ b/examples/python/high_level_api.py @@ -79,12 +79,12 @@ class TestGribMessage(unittest.TestCase): """Metadata is read correctly from GribMessage.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) - key_count = 251 + key_count = 253 self.assertEqual(len(msg), key_count) self.assertEqual(msg.size(), 160219) self.assertEqual(len(msg.keys()), key_count) - def test_missing_message_behavior(self): + def test_missing_message_behaviour(self): """Missing messages are detected properly.""" with GribFile(TESTGRIB) as grib: msg = GribMessage(grib) diff --git a/python/eccodes/high_level/gribfile.py b/python/eccodes/high_level/gribfile.py index 6df183117..a6d4ab153 100644 --- a/python/eccodes/high_level/gribfile.py +++ b/python/eccodes/high_level/gribfile.py @@ -30,7 +30,7 @@ class GribFile(file): def __enter__(self): return self - def __exit__(self, type, value, traceback): + def __exit__(self, exception_type, exception_value, traceback): """Close all open messages, release GRIB file handle and close file.""" while self.open_messages: self.open_messages.pop().close() diff --git a/python/eccodes/high_level/gribindex.py b/python/eccodes/high_level/gribindex.py index b0bfabe1d..955095a1a 100644 --- a/python/eccodes/high_level/gribindex.py +++ b/python/eccodes/high_level/gribindex.py @@ -34,7 +34,7 @@ class GribIndex(object): def __enter__(self): return self - def __exit__(self, type, value, traceback): + def __exit__(self, exception_type, exception_value, traceback): """Release GRIB message handle and inform file of release.""" while self.open_messages: self.open_messages[0].close() @@ -79,9 +79,9 @@ class GribIndex(object): """Return number of distinct values for index key.""" return eccodes.codes_index_get_size(self.iid, key) - def values(self, key, type=str): + def values(self, key, ktype=str): """Return distinct values of index key.""" - return eccodes.codes_index_get(self.iid, key, type) + return eccodes.codes_index_get(self.iid, key, ktype) def add(self, filename): """Add ``filename`` to the ``GribIndex``.""" diff --git a/python/eccodes/high_level/gribmessage.py b/python/eccodes/high_level/gribmessage.py index 5c5976521..5a0ab2278 100644 --- a/python/eccodes/high_level/gribmessage.py +++ b/python/eccodes/high_level/gribmessage.py @@ -59,6 +59,7 @@ class GribMessage(object): ... # If desired, messages can be closed manually or used in with ... msg.close() """ + def __enter__(self): return self @@ -144,7 +145,7 @@ class GribMessage(object): if grib_file is not None: self.gid = eccodes.codes_grib_new_from_file(grib_file.file_handle) if self.gid is None: - raise IOError("Grib file %s is exhausted" % grib_file.name) + raise IOError("Grib file %s is exhausted." % grib_file.name) self.grib_file = grib_file self.grib_file.message += 1 self.grib_file.open_messages.append(self) @@ -175,7 +176,7 @@ class GribMessage(object): def get(self, key, ktype=None): """Get value of a given key as its native or specified type.""" if self.missing(key): - raise KeyError("Key is missing from message.") + raise KeyError("Value of key %s is MISSING." % key) if eccodes.codes_get_size(self.gid, key) > 1: ret = eccodes.codes_get_array(self.gid, key, ktype) else: