Skip to content

Partial fix for #2841 to improve formatting. #2906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2019
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
43 changes: 23 additions & 20 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def format_timestamp(t):
if time_str == '00:00:00':
return date_str
else:
return '%sT%s' % (date_str, time_str)
return '{}T{}'.format(date_str, time_str)


def format_timedelta(t, timedelta_format=None):
Expand Down Expand Up @@ -212,12 +212,12 @@ def summarize_variable(name, var, col_width, show_values=True,
marker=' ', max_width=None):
if max_width is None:
max_width = OPTIONS['display_width']
first_col = pretty_print(' %s %s ' % (marker, name), col_width)
first_col = pretty_print(' {} {} '.format(marker, name), col_width)
if var.dims:
dims_str = '(%s) ' % ', '.join(map(str, var.dims))
dims_str = '({}) '.format(', '.join(map(str, var.dims)))
else:
dims_str = ''
front_str = '%s%s%s ' % (first_col, dims_str, var.dtype)
front_str = '{}{}{} '.format(first_col, dims_str, var.dtype)
if show_values:
values_str = format_array_flat(var, max_width - len(front_str))
elif isinstance(var._data, dask_array_type):
Expand All @@ -229,8 +229,9 @@ def summarize_variable(name, var, col_width, show_values=True,


def _summarize_coord_multiindex(coord, col_width, marker):
first_col = pretty_print(' %s %s ' % (marker, coord.name), col_width)
return '%s(%s) MultiIndex' % (first_col, str(coord.dims[0]))
first_col = pretty_print(' {} {} '.format(
marker, coord.name), col_width)
return '{}({}) MultiIndex'.format(first_col, str(coord.dims[0]))


def _summarize_coord_levels(coord, col_width, marker='-'):
Expand Down Expand Up @@ -264,13 +265,14 @@ def summarize_coord(name, var, col_width):
def summarize_attr(key, value, col_width=None):
"""Summary for __repr__ - use ``X.attrs[key]`` for full value."""
# Indent key and add ':', then right-pad if col_width is not None
k_str = ' %s:' % key
k_str = ' {}:'.format(key)
if col_width is not None:
k_str = pretty_print(k_str, col_width)
# Replace tabs and newlines, so we print on one line in known width
v_str = str(value).replace('\t', '\\t').replace('\n', '\\n')
# Finally, truncate to the desired display width
return maybe_truncate('%s %s' % (k_str, v_str), OPTIONS['display_width'])
return maybe_truncate('{} {}'.format(k_str, v_str),
OPTIONS['display_width'])


EMPTY_REPR = ' *empty*'
Expand Down Expand Up @@ -303,7 +305,7 @@ def _calculate_col_width(col_items):
def _mapping_repr(mapping, title, summarizer, col_width=None):
if col_width is None:
col_width = _calculate_col_width(mapping)
summary = ['%s:' % title]
summary = ['{}:'.format(title)]
if mapping:
summary += [summarizer(k, v, col_width) for k, v in mapping.items()]
else:
Expand All @@ -329,19 +331,19 @@ def coords_repr(coords, col_width=None):
def indexes_repr(indexes):
summary = []
for k, v in indexes.items():
summary.append(wrap_indent(repr(v), '%s: ' % k))
summary.append(wrap_indent(repr(v), '{}: '.format(k)))
return '\n'.join(summary)


def dim_summary(obj):
elements = ['%s: %s' % (k, v) for k, v in obj.sizes.items()]
elements = ['{}: {}'.format(k, v) for k, v in obj.sizes.items()]
return ', '.join(elements)


def unindexed_dims_repr(dims, coords):
unindexed_dims = [d for d in dims if d not in coords]
if unindexed_dims:
dims_str = ', '.join('%s' % d for d in unindexed_dims)
dims_str = ', '.join('{}'.format(d) for d in unindexed_dims)
return 'Dimensions without coordinates: ' + dims_str
else:
return None
Expand Down Expand Up @@ -382,10 +384,11 @@ def short_dask_repr(array, show_dtype=True):
"""
chunksize = tuple(c[0] for c in array.chunks)
if show_dtype:
return 'dask.array<shape=%s, dtype=%s, chunksize=%s>' % (
return 'dask.array<shape={}, dtype={}, chunksize={}>'.format(
array.shape, array.dtype, chunksize)
else:
return 'dask.array<shape=%s, chunksize=%s>' % (array.shape, chunksize)
return 'dask.array<shape={}, chunksize={}>'.format(
array.shape, chunksize)


def short_data_repr(array):
Expand All @@ -394,18 +397,18 @@ def short_data_repr(array):
elif array._in_memory or array.size < 1e5:
return short_array_repr(array.values)
else:
return u'[%s values with dtype=%s]' % (array.size, array.dtype)
return u'[{} values with dtype={}]'.format(array.size, array.dtype)


def array_repr(arr):
# used for DataArray, Variable and IndexVariable
if hasattr(arr, 'name') and arr.name is not None:
name_str = '%r ' % arr.name
name_str = '{!r} '.format(arr.name)
else:
name_str = ''

summary = ['<xarray.%s %s(%s)>'
% (type(arr).__name__, name_str, dim_summary(arr))]
summary = ['<xarray.{} {}({})>'.format(
type(arr).__name__, name_str, dim_summary(arr))]

summary.append(short_data_repr(arr))

Expand All @@ -424,12 +427,12 @@ def array_repr(arr):


def dataset_repr(ds):
summary = ['<xarray.%s>' % type(ds).__name__]
summary = ['<xarray.{}>'.format(type(ds).__name__)]

col_width = _calculate_col_width(_get_col_items(ds.variables))

dims_start = pretty_print('Dimensions:', col_width)
summary.append('%s(%s)' % (dims_start, dim_summary(ds)))
summary.append('{}({})'.format(dims_start, dim_summary(ds)))

if ds.coords:
summary.append(coords_repr(ds.coords, col_width=col_width))
Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ def test_diff_dataset_repr(self):
actual = formatting.diff_dataset_repr(ds_a, ds_b, 'identical')
assert actual == expected

def test_array_repr(self):
ds = xr.Dataset(coords={'foo': [1, 2, 3], 'bar': [1, 2, 3]})
ds[(1, 2)] = xr.DataArray([0], dims='test')
actual = formatting.array_repr(ds[(1, 2)])
expected = dedent("""\
<xarray.DataArray (1, 2) (test: 1)>
array([0])
Dimensions without coordinates: test""")

assert actual == expected


def test_set_numpy_options():
original_options = np.get_printoptions()
Expand Down