Skip to content

Commit 3cbf04e

Browse files
committed
Add tests bolilerplate
1 parent 55762c2 commit 3cbf04e

File tree

4 files changed

+119
-39
lines changed

4 files changed

+119
-39
lines changed

.travis.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# http://lint.travis-ci.org/
2+
3+
language: python
4+
5+
sudo: false
6+
7+
matrix:
8+
fast_finish: true
9+
include:
10+
- python: 2.7
11+
env: TEST_TARGET=default
12+
- python: 3.6
13+
env: TEST_TARGET=default
14+
15+
before_install:
16+
- wget http://bit.ly/miniconda -O miniconda.sh
17+
- bash miniconda.sh -b -p $HOME/miniconda
18+
- export PATH="$HOME/miniconda/bin:$PATH"
19+
- conda config --set always_yes yes --set changeps1 no --set show_channel_urls true
20+
- conda update conda
21+
- conda config --add channels conda-forge --force
22+
- conda create --name TEST python=$TRAVIS_PYTHON_VERSION --file requirements.txt --file requirements-dev.txt
23+
- source activate TEST
24+
25+
install:
26+
- python setup.py sdist && version=$(python setup.py --version) && pushd dist && pip install erddapy-${version}.tar.gz && popd
27+
28+
script:
29+
- if [[ $TEST_TARGET != 'docs' ]]; then
30+
cp -r tests/ /tmp && cd /tmp ;
31+
fi
32+
33+
- if [[ $TEST_TARGET == 'default' ]]; then
34+
py.test -vv tests ;
35+
fi

erddapy/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import (absolute_import, division, print_function)
4+
5+
from erddapy.erddapy import ERDDAP
6+
7+
__all__ = ['ERDDAP']
18

29
from ._version import get_versions
310
__version__ = get_versions()['version']

erddapy/erddapy.py

+69-39
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,34 @@ class ERDDAP(object):
2222
The user must pass the endpoint explicitly!!
2323
2424
Examples:
25-
>>> e = ERDDAP(server_url='https://data.ioos.us/gliders/erddap')
25+
>>> e = ERDDAP(
26+
... server_url='https://data.ioos.us/gliders/erddap',
27+
... server_type='tabledap'
28+
... )
2629
2730
"""
28-
def __init__(self, server_url):
31+
def __init__(self, server_url, server_type='tabledap'):
2932
self.server_url = server_url
30-
# Caching the last resquest for multiple access,
31-
# will be overriden when a new variable is requested.
33+
self.server_type = server_type
34+
# Caching the last request for quicker multiple access,
35+
# will be overridden when making a new requested.
3236
self._nc = None
3337
self._dataset_id = None
34-
self.search_url = None
35-
self._search_options = {}
36-
self.info_url = None
37-
self._info_options = {}
3838
self._variables = {}
3939

40-
def search(self, items_per_page=1000, page=1, response='csv', **kwargs):
41-
"""Compose the search URL for `server_url` endpoint.
40+
def search_url(self, items_per_page=1000, page=1, response='csv', **kwargs):
41+
"""Compose the search URL for the `server_url` endpoint.
4242
4343
Args:
4444
items_per_page (int): how many items per page in the return,
4545
default is 1000.
4646
page (int): which page to display, defatul is the first page (1).
47-
response (str): check https://data.ioos.us/gliders/erddap/tabledap/documentation.html#fileType
48-
for all the options, default is a Comma Separated Value ('csv').
47+
response (str): default is a Comma Separated Value ('csv').
48+
See ERDDAP docs for all the options,
49+
tabledap: http://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html
50+
griddap: http://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html
4951
Returns:
5052
search_url (str): the search URL for the `response` chosen.
51-
The `search_url` is cached as an instance property,
52-
but will be overriden when performing a new search.
5353
"""
5454
base = (
5555
'{server_url}/search/advanced.{response}'
@@ -94,22 +94,19 @@ def search(self, items_per_page=1000, page=1, response='csv', **kwargs):
9494
'minTime': kwargs.get('min_time', default),
9595
'maxTime': kwargs.get('max_time', default),
9696
}
97-
if not self.search_url or (self._search_options.items() != search_options):
98-
self.search_url = base(**search_options)
99-
self._search_options = search_options
100-
return self.search_url
97+
return base(**search_options)
10198

102-
def info(self, dataset_id, response='csv'):
103-
"""Compose the info URL for `server_url` endpoint.
99+
def info_url(self, dataset_id, response='csv'):
100+
"""Compose the info URL for the `server_url` endpoint.
104101
105102
Args:
106-
dataset_id (str): Use the data id found using the the search.
107-
response (str): check https://data.ioos.us/gliders/erddap/tabledap/documentation.html#fileType
108-
for all the options, default is a Comma Separated Value ('csv').
103+
dataset_id (str): a dataset unique id.
104+
response (str): default is a Comma Separated Value ('csv').
105+
See ERDDAP docs for all the options,
106+
tabledap: http://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html
107+
griddap: http://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html
109108
Returns:
110109
info_url (str): the info URL for the `response` chosen.
111-
The `info_url` is cached as an instance property,
112-
but will be overriden when performing a new info request.
113110
"""
114111
response = _clean_response(response)
115112
base = '{server_url}/info/{dataset_id}/index.{response}'.format
@@ -118,15 +115,47 @@ def info(self, dataset_id, response='csv'):
118115
'dataset_id': dataset_id,
119116
'response': response
120117
}
121-
if not self.info_url or (self._info_options.items() != info_options):
122-
self.info_url = base(**info_options)
123-
self._info_options = info_options
124-
return self.info_url
118+
return base(**info_options)
125119

126-
def opendap(self, dataset_id):
127-
base = '{server_url}/tabledap/{dataset_id}'.format
128-
self.opendap_url = base(server_url=self.server_url, dataset_id=dataset_id)
129-
return self.opendap_url
120+
def download_url(self, dataset_id, variables, response='csv', **kwargs):
121+
"""Compose the download URL for the `server_url` endpoint.
122+
123+
Args:
124+
dataset_id (str): a dataset unique id.
125+
variables (list/tuple): a list of the variables to download.
126+
response (str): default is a Comma Separated Value ('csv').
127+
See ERDDAP docs for all the options,
128+
tabledap: http://coastwatch.pfeg.noaa.gov/erddap/tabledap/documentation.html
129+
griddap: http://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html
130+
Returns:
131+
download_url (str): the download URL for the `response` chosen.
132+
"""
133+
variables = ','.join(variables)
134+
base = (
135+
'{server_url}/{server_type}/{dataset_id}.{response}'
136+
'?{variables}'
137+
'{kwargs}'
138+
).format
139+
140+
kwargs = ''.join(['&{}{}'.format(k, v) for k, v in kwargs.items()])
141+
return base(server_url=self.server_url, server_type=self.server_type,
142+
dataset_id=dataset_id, response=response, variables=variables,
143+
kwargs=kwargs)
144+
145+
def opendap_url(self, dataset_id):
146+
"""Compose the opendap URL for the `server_url` the endpoint.
147+
148+
Args:
149+
dataset_id (str): a dataset unique id.
150+
Returns:
151+
download_url (str): the download URL for the `response` chosen.
152+
"""
153+
base = '{server_url}/{server_type}/{dataset_id}'.format
154+
return base(
155+
server_url=self.server_url,
156+
server_type=self.server_type,
157+
dataset_id=dataset_id
158+
)
130159

131160
def get_var_by_attr(self, dataset_id, **kwargs):
132161
"""Similar to netCDF4-python `get_variables_by_attributes` for a ERDDAP
@@ -151,20 +180,21 @@ def get_var_by_attr(self, dataset_id, **kwargs):
151180
>>> # Get variables that have a "grid_mapping" attribute
152181
>>> vs = nc.get_variables_by_attributes(grid_mapping=lambda v: v is not None)
153182
"""
154-
info_url = self.info(dataset_id, response='csv')
155-
df = pd.read_csv(info_url)
183+
info_url = self.info_url(dataset_id, response='csv')
156184

157185
# Creates the variables dictionary for the `get_var_by_attr` lookup.
158186
if not self._variables or self._dataset_id != dataset_id:
187+
_df = pd.read_csv(info_url)
188+
self._dataset_id = dataset_id
159189
variables = {}
160-
# Virtually the same code as the netCDF4 counterpart.
161-
for variable in set(df['Variable Name']):
162-
attributes = df.loc[
163-
df['Variable Name'] == variable,
190+
for variable in set(_df['Variable Name']):
191+
attributes = _df.loc[
192+
_df['Variable Name'] == variable,
164193
['Attribute Name', 'Value']
165194
].set_index('Attribute Name').to_dict()['Value']
166195
variables.update({variable: attributes})
167196
self._variables = variables
197+
# Virtually the same code as the netCDF4 counterpart.
168198
vs = []
169199
has_value_flag = False
170200
for vname in self._variables:

requirements-dev.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
flake8
2+
flake8-builtins
3+
flake8-comprehensions
4+
flake8-import-order
5+
flake8-mutable
6+
flake8-print
7+
flake8-quotes
8+
netcdf4

0 commit comments

Comments
 (0)