Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Added
-----
* `psyplot.data.open_dataset` now decodes grid_mappings attributes,
see `#17 <https://github.com/psyplot/psyplot/pull/17>`__
* psyplot projects now support the with syntax, e.g. something like::

with psy.plot.mapplot('file.nc') as sp:
sp.export('output.png')

sp will be closed automatically (see commit `ee7415b <https://github.com/psyplot/psyplot/commit/ee7415befce61247b5a08d9cfafab96ceb06f6f8>`__)

Changed
-------
Expand Down
6 changes: 6 additions & 0 deletions psyplot/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ def __call__(self, *args, **kwargs):
ret.main = self.main
return ret

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close(True, True, True)

@_first_main
def extend(self, *args, **kwargs):
len0 = len(self)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ def tearDown(self):
pass
self._created_files.clear()

def test_with(self):
"""Test __enter__ and __exit__ methods"""
psy.register_plotter('test_plotter', import_plotter=True,
module='test_plotter', plotter_name='TestPlotter')
self.assertFalse(psy.gcp(True))
self.assertFalse(psy.gcp())
with psy.plot.test_plotter(bt.get_file('test-t2m-u-v.nc')) as sp:
self.assertTrue(psy.gcp(True))
self.assertTrue(psy.gcp())

self.assertFalse(psy.gcp(True))
self.assertFalse(psy.gcp())

def test_save_and_load_01_simple(self):
"""Test the saving and loading of a Project"""
psy.register_plotter('test_plotter', import_plotter=True,
Expand Down