Skip to content
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

Printing/repr improvements #154

Merged
merged 3 commits into from
Sep 15, 2017
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
4 changes: 4 additions & 0 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def filter_time_range(self, start, end, regex=None):
return [item[-1] for item in self._get_datasets_with_times(regex)
if start <= item[0] <= end]

def __str__(self):
"""Return a string representation of the collection."""
return str(list(self))


class TDSCatalog(object):
"""
Expand Down
6 changes: 6 additions & 0 deletions siphon/cdmr/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def __str__(self):
print_groups.append('\t{}: {}'.format(att, getattr(self, att)))
return '\n'.join(print_groups)

__repr__ = __str__


class Dataset(Group):
"""Abstract away access to the remote dataset."""
Expand All @@ -144,6 +146,8 @@ def __str__(self):
"""Return a string representation of the Dataset and all contained members."""
return self.url + '\n' + super(Dataset, self).__str__()

__repr__ = __str__


class Variable(AttributeContainer):
"""Hold information about a data variable and provide access to the underlying data."""
Expand Down Expand Up @@ -372,3 +376,5 @@ def __str__(self):
grps.append(', size = {0}'.format(self.size))

return ''.join(grps)

__repr__ = __str__
8 changes: 5 additions & 3 deletions siphon/cdmr/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from numpy.testing import assert_almost_equal, assert_array_almost_equal, assert_array_equal
import pytest

from siphon.cdmr import Dataset
from siphon.testing import get_recorder
Expand Down Expand Up @@ -318,10 +319,11 @@ def test_unsigned_var():


@recorder.use_cassette('nc4_groups')
def test_print():
"""Test that __str__ (or printing) a dataset works."""
@pytest.mark.parametrize('func', [str, repr])
def test_print(func):
"""Test that str and repr of a dataset work."""
ds = Dataset('http://localhost:8080/thredds/cdmremote/nc4/tst/tst_groups.nc')
s = str(ds)
s = func(ds)
truth = """http://localhost:8080/thredds/cdmremote/nc4/tst/tst_groups.nc
Groups:
g1
Expand Down
11 changes: 11 additions & 0 deletions siphon/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ def test_datasets_get_by_index():
assert cat.datasets[2].name == 'Latest Collection for NAM CONUS 20km'


@recorder.use_cassette('top_level_20km_rap_catalog')
def test_datasets_str():
"""Test that datasets are printed as expected."""
url = ('http://thredds.ucar.edu/thredds/catalog/grib/NCEP/NAM/'
'CONUS_20km/noaaport/catalog.xml')
cat = TDSCatalog(url)
assert str(cat.datasets) == ("['Full Collection (Reference / Forecast Time) Dataset', "
"'Best NAM CONUS 20km Time Series', "
"'Latest Collection for NAM CONUS 20km']")


@recorder.use_cassette('top_level_20km_rap_catalog')
def test_datasets_nearest_time():
"""Test getting dataset by time using filenames."""
Expand Down
2 changes: 1 addition & 1 deletion siphon/tests/test_ncss.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_vertical_level(self):
self.nq.accept('csv').vertical_level(50000)
csv_data = self.ncss.get_data(self.nq)

assert str(csv_data['Temperature_isobaric'])[:6] == '263.39'
np.testing.assert_almost_equal(csv_data['Temperature_isobaric'], np.array([263.40]), 2)

@recorder.use_cassette('ncss_gfs_csv_point')
def test_raw_csv(self):
Expand Down