Skip to content

Commit 4d76ee4

Browse files
committed
Moved repeated strings to constants & tidied up docstrings
1 parent 752ab2d commit 4d76ee4

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

xarray/backends/api.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from ..core.utils import close_on_error, is_remote_uri
1313
from ..core.pycompat import basestring
1414

15+
DATAARRAY_NAME = '__xarray_dataarray_name__'
16+
DATAARRAY_VARIABLE = '__xarray_dataarray_variable__'
1517

1618
def _get_default_engine(path, allow_remote=False):
1719
if allow_remote and is_remote_uri(path): # pragma: no cover
@@ -314,21 +316,21 @@ def open_dataarray(filename_or_obj, group=None, decode_cf=True,
314316
chunks, lock, drop_variables)
315317

316318
if len(dataset.data_vars) != 1:
317-
raise ValueError('Given file dataset contains more than one variable. '
318-
'Please read with xarray.open_dataset and then select '
319-
'the variable you want.')
319+
raise ValueError('Given file dataset contains more than one data '
320+
'variable. Please read with xarray.open_dataset and '
321+
'then select the variable you want.')
320322
else:
321323
data_array, = dataset.data_vars.values()
322324

323325
data_array._file_obj = dataset._file_obj
324326

325327
# Reset names if they were changed during saving
326328
# to ensure that we can 'roundtrip' perfectly
327-
if '__xarray_dataarray_name__' in dataset.attrs:
328-
data_array.name = dataset.attrs['__xarray_dataarray_name__']
329-
del dataset.attrs['__xarray_dataarray_name__']
329+
if DATAARRAY_NAME in dataset.attrs:
330+
data_array.name = dataset.attrs[DATAARRAY_NAME]
331+
del dataset.attrs[DATAARRAY_NAME]
330332

331-
if data_array.name == '__xarray_dataarray_variable__':
333+
if data_array.name == DATAARRAY_VARIABLE:
332334
data_array.name = None
333335

334336
return data_array

xarray/core/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def where(self, cond, other=None, drop=False):
579579
return outobj._where(outcond)
580580

581581
def close(self):
582-
"""Close any files linked to this dataset
582+
"""Close any files linked to this object
583583
"""
584584
if self._file_obj is not None:
585585
self._file_obj.close()

xarray/core/dataarray.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,14 +1155,16 @@ def to_netcdf(self, *args, **kwargs):
11551155
11561156
All parameters are passed directly to `xarray.Dataset.to_netcdf`.
11571157
"""
1158+
from ..backends.api import DATAARRAY_NAME, DATAARRAY_VARIABLE
1159+
11581160
if not self.name:
11591161
# If no name is set then use a generic xarray name
1160-
dataset = self.to_dataset(name='__xarray_dataarray_variable__')
1162+
dataset = self.to_dataset(name=DATAARRAY_VARIABLE)
11611163
elif self.name in list(self.coords):
11621164
# The name is the same as one of the coords names, which netCDF
11631165
# doesn't support, so rename it but keep track of the old name
1164-
dataset = self.to_dataset(name='__xarray_dataarray_variable__')
1165-
dataset.attrs['__xarray_dataarray_name__'] = self.name
1166+
dataset = self.to_dataset(name=DATAARRAY_VARIABLE)
1167+
dataset.attrs[DATAARRAY_NAME] = self.name
11661168
else:
11671169
# No problems with the name - so we're fine!
11681170
dataset = self.to_dataset()

0 commit comments

Comments
 (0)