Skip to content

Commit

Permalink
drop lazy opening/caching and use partial function for open
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Hamman committed May 15, 2018
1 parent 8b286c0 commit 6669035
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import traceback
import warnings
from collections import Mapping, OrderedDict
from functools import partial

import numpy as np

Expand Down Expand Up @@ -507,36 +508,24 @@ def assert_open(self):

class PickleByReconstructionWrapper(object):

def __init__(self, opener, *args, **kwargs):
def __init__(self, opener, file, mode='r', **kwargs):

self.opener = opener
self.open_args = args
self.open_kwargs = kwargs

self._ds = None
self._isopen = False
self.opener = partial(opener, file, mode=mode, **kwargs)
self.mode = mode

@property
def value(self):
if self._ds is not None and self._isopen:
return self._ds
self._ds = self.opener(*self.open_args, **self.open_kwargs)
self._isopen = True
return self._ds
return self.opener()

def __getstate__(self):
state = self.__dict__.copy()
del state['_ds']
del state['_isopen']
if self.open_kwargs.get('mode', None) == 'w':
if self.mode == 'w':
# file has already been created, don't override when restoring
state['open_kwargs']['mode'] = 'a'
state['mode'] = 'a'
return state

def __setstate__(self, state):
self.__dict__.update(state)
self._ds = None
self._isopen = False

def __getitem__(self, key):
return self.value[key]
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2650,7 +2650,7 @@ def test_chunks(self):
ex = expected.sel(band=1).mean(dim='x')
assert_allclose(ac, ex)

def test_pickle_rio(self):
def test_pickle_rasterio(self):
# regression test for https://github.com/pydata/xarray/issues/2121
with create_tmp_geotiff() as (tmp_file, expected):
with xr.open_rasterio(tmp_file) as rioda:
Expand Down

0 comments on commit 6669035

Please sign in to comment.