Skip to content

Commit 4920def

Browse files
committed
Moved repeated strings to constants & tidied up docstrings
1 parent 9fde81f commit 4920def

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
@@ -15,6 +15,8 @@
1515
from ..core.utils import close_on_error, is_remote_uri
1616
from ..core.pycompat import basestring
1717

18+
DATAARRAY_NAME = '__xarray_dataarray_name__'
19+
DATAARRAY_VARIABLE = '__xarray_dataarray_variable__'
1820

1921
def _get_default_engine(path, allow_remote=False):
2022
if allow_remote and is_remote_uri(path): # pragma: no cover
@@ -348,21 +350,21 @@ def open_dataarray(filename_or_obj, group=None, decode_cf=True,
348350
chunks, lock, drop_variables)
349351

350352
if len(dataset.data_vars) != 1:
351-
raise ValueError('Given file dataset contains more than one variable. '
352-
'Please read with xarray.open_dataset and then select '
353-
'the variable you want.')
353+
raise ValueError('Given file dataset contains more than one data '
354+
'variable. Please read with xarray.open_dataset and '
355+
'then select the variable you want.')
354356
else:
355357
data_array, = dataset.data_vars.values()
356358

357359
data_array._file_obj = dataset._file_obj
358360

359361
# Reset names if they were changed during saving
360362
# to ensure that we can 'roundtrip' perfectly
361-
if '__xarray_dataarray_name__' in dataset.attrs:
362-
data_array.name = dataset.attrs['__xarray_dataarray_name__']
363-
del dataset.attrs['__xarray_dataarray_name__']
363+
if DATAARRAY_NAME in dataset.attrs:
364+
data_array.name = dataset.attrs[DATAARRAY_NAME]
365+
del dataset.attrs[DATAARRAY_NAME]
364366

365-
if data_array.name == '__xarray_dataarray_variable__':
367+
if data_array.name == DATAARRAY_VARIABLE:
366368
data_array.name = None
367369

368370
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
@@ -1152,14 +1152,16 @@ def to_netcdf(self, *args, **kwargs):
11521152
11531153
All parameters are passed directly to `xarray.Dataset.to_netcdf`.
11541154
"""
1155+
from ..backends.api import DATAARRAY_NAME, DATAARRAY_VARIABLE
1156+
11551157
if not self.name:
11561158
# If no name is set then use a generic xarray name
1157-
dataset = self.to_dataset(name='__xarray_dataarray_variable__')
1159+
dataset = self.to_dataset(name=DATAARRAY_VARIABLE)
11581160
elif self.name in list(self.coords):
11591161
# The name is the same as one of the coords names, which netCDF
11601162
# doesn't support, so rename it but keep track of the old name
1161-
dataset = self.to_dataset(name='__xarray_dataarray_variable__')
1162-
dataset.attrs['__xarray_dataarray_name__'] = self.name
1163+
dataset = self.to_dataset(name=DATAARRAY_VARIABLE)
1164+
dataset.attrs[DATAARRAY_NAME] = self.name
11631165
else:
11641166
# No problems with the name - so we're fine!
11651167
dataset = self.to_dataset()

0 commit comments

Comments
 (0)