Skip to content

Commit c206716

Browse files
Merge pull request #40 from ocefpaf/simpler_constructor
Simplify the erddapy object constructor
2 parents c40c70c + c39734a commit c206716

File tree

3 files changed

+158
-153
lines changed

3 files changed

+158
-153
lines changed

CHANGES.txt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
0.1.5
2-
~~~~~
1+
# Changelog
2+
3+
## 0.1.6
4+
5+
- Simplify the `erddapy` object constructor.
6+
Everything but the `server` and `protocol` should be passed as properties
7+
after the object creation.
8+
9+
## 0.1.5
310

411
- Re-implemented caching in `get_var_by_attr()`.
512

6-
0.1.3
7-
~~~~~
13+
## 0.1.3
814

915
- Avoid a caching bug in `get_var_by_attr()` by not caching at all.
1016

11-
0.1.3
12-
~~~~~
17+
## 0.1.3
1318

1419
- New API to make it easier to handle multiple requests from the same server.
1520

16-
0.1.2
17-
~~~~~
21+
## 0.1.2
1822

1923
- Added a workaround to access authenticated ERDDAP servers.
2024

21-
0.1.1
22-
~~~~~
25+
## 0.1.1
2326

2427
- Mostly boilerplate updates for a GitHub/PyPI release.
2528

26-
0.1.0
27-
~~~~~
29+
## 0.1.0
2830

2931
- First version!

erddapy/erddapy.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,21 @@ class ERDDAP(object):
7979
'UBC': 'https://salishsea.eos.ubc.ca/erddap'}
8080
8181
"""
82-
def __init__(self, server, dataset_id=None, protocol=None, variables='',
83-
response='html', constraints=None, params=None, requests_kwargs=None):
82+
def __init__(self, server, protocol=None, response='html'):
8483
if server in servers.keys():
8584
server = servers[server].url
8685
self.server = _check_url_response(server)
87-
self.dataset_id = dataset_id
8886
self.protocol = protocol
89-
self.variables = variables
87+
88+
# Initialized only via properties.
89+
self.constraints = None
90+
self.dataset_id = None
91+
self.params = None
92+
self.requests_kwargs = {}
9093
self.response = response
91-
self.constraints = constraints
92-
self.params = params
93-
self.requests_kwargs = requests_kwargs if requests_kwargs else {}
94+
self.variables = ''
9495

95-
# Caching the last `dataset_id` request for quicker multiple accesses,
96+
# Caching the last `dataset_id` and `variables` list request for quicker multiple accesses,
9697
# will be overridden when requesting a new `dataset_id`.
9798
self._dataset_id = None
9899
self._variables = {}
@@ -113,7 +114,7 @@ def get_info_url(self, dataset_id=None, response=None):
113114
response = response if response else self.response
114115

115116
if not dataset_id:
116-
raise ValueError('You must specify a valid dataset_id, got {}'.format(self.dataset_id))
117+
raise ValueError(f'You must specify a valid dataset_id, got {dataset_id}')
117118

118119
return info_url(
119120
server=self.server,
@@ -130,10 +131,10 @@ def get_download_url(self, dataset_id=None, protocol=None,
130131
constraints = constraints if constraints else self.constraints
131132

132133
if not dataset_id:
133-
raise ValueError('Please specify a valid `dataset_id`, got {}'.format(self.dataset_id))
134+
raise ValueError(f'Please specify a valid `dataset_id`, got {dataset_id}')
134135

135136
if not protocol:
136-
raise ValueError('Please specify a valid `protocol`, got {}'.format(self.protocol))
137+
raise ValueError(f'Please specify a valid `protocol`, got {protocol}')
137138

138139
return download_url(
139140
server=self.server,
@@ -196,6 +197,10 @@ def get_var_by_attr(self, dataset_id=None, **kwargs):
196197
"""
197198
if not dataset_id:
198199
dataset_id = self.dataset_id
200+
201+
if dataset_id is None:
202+
raise ValueError(f'You must specify a valid dataset_id, got {dataset_id}')
203+
199204
url = info_url(self.server, dataset_id=dataset_id, response='csv')
200205

201206
# Creates the variables dictionary for the `get_var_by_attr` lookup.

0 commit comments

Comments
 (0)