From 8b92b06476d0be86559e501274dc3a739d0f7735 Mon Sep 17 00:00:00 2001 From: heyarne Date: Tue, 2 Mar 2021 14:25:06 +0100 Subject: [PATCH] Add link to python context manager --- sources/sentinel_helpers.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sources/sentinel_helpers.py b/sources/sentinel_helpers.py index 1dd57e3..ac1e3cd 100644 --- a/sources/sentinel_helpers.py +++ b/sources/sentinel_helpers.py @@ -192,7 +192,7 @@ def scihub_band_date(band): return parse_datetime(file_name.split('_')[-3]) -# TODO: This is documented somewhere in the python docs, we should link to it here +# See https://book.pythontips.com/en/latest/context_managers.html#implementing-a-context-manager-as-a-class class RasterReaderList(): ''' @@ -200,7 +200,6 @@ class RasterReaderList(): rasterio.open. They get automatically closed when the context created by the `with` block is left. ''' - def __init__(self, paths): self.open_files = [] self.paths = paths @@ -213,7 +212,12 @@ class RasterReaderList(): def __exit__(self, _type, _value, _traceback): for f in self.open_files: - f.close() + # wrapped in a block so we still close other fails if one fails + # to close + try: + f.close() + except: + pass def geodataframe_on_map(geodataframe):