Skip to content

Commit

Permalink
split NWIS from station, add a list of instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
mroberge committed Sep 27, 2016
1 parent 62d548b commit 1493ec2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 37 deletions.
Binary file modified .spyderproject
Binary file not shown.
60 changes: 46 additions & 14 deletions hydrofunctions/station.py
Expand Up @@ -11,23 +11,55 @@


class Station(object):
"""A class for organizing stream gauge data for a single site."""
"""A class for organizing stream gauge data for a single site.
Store copies of each station in a dictionary station_dict.
This dict will include descendant objects too.
The dict will be
Improvements:
make each subclass store its own dictionary, parent class can combine.
only store weakrefs to the objects, so that they can be garbage
collected. maybe weakvaluedictionary.
1) http://stackoverflow.com/a/18321898
2) http://stackoverflow.com/a/9460070
Future Feature:
only create new instance if its id is not already in the list.
if id in station_dict:
#just re-use already existing obj.
return station_dict[id]
#prob need to use a factory to do this.
"""
station_dict = {}

def __init__(self, id=None):
Station.station_dict[id] = self
self.id = id
# One option is to make it so that you can pass in a get_data function
# during the creation of an instance.
self.get_data = None

def fetch2(self):
"""Will request data from NWIS after checking input types."""
# raise TypeError if parameters are wrong
typing.check_NWIS_station_id(self.site)
typing.check_datestr(self.start_date)
typing.check_datestr(self.end_date)
hf.get_nwis(self.site, self.service, self.start_date, self.end_date)

def __init__(self, site=None, service="dv", start_date=None, end_date=None):
self.site = site
class NWIS(Station):
"""As class for working with data from the USGS NWIS service."""

def __init__(self,
id=None,
service="dv",
start_date=None,
end_date=None):
self.id = id
self.service = service
self.start_date = start_date
self.end_date = end_date
#self.fetch = hf.get_nwis(self.site, self.service, self.start_date, self.end_date)
self.fetch = self.fetch2


# self.get_data = hf.get_nwis(self.site, self.service, self.start_date, self.end_date)
self.get_data = self.fetch2

def fetch2(self):
"""Will request data from NWIS after checking input types."""
# raise TypeError if parameters are wrong
typing.check_NWIS_station_id(self.id)
typing.check_datestr(self.start_date)
typing.check_datestr(self.end_date)
hf.get_nwis(self.id, self.service, self.start_date, self.end_date)
76 changes: 53 additions & 23 deletions tests/test_station.py
Expand Up @@ -8,8 +8,8 @@
from __future__ import absolute_import, print_function
import unittest
from unittest import mock
from hydrofunctions import station

from hydrofunctions import station


class TestStation(unittest.TestCase):
Expand All @@ -18,49 +18,79 @@ def test_station_is_obj(self):
actual = station.Station()
self.assertIsInstance(actual, station.Station)

def test_station_site_defaults_to_None(self):
def test_station_id_defaults_to_None(self):
actual = station.Station()
self.assertIsNone(actual.site)
self.assertIsNone(actual.id)

def test_station_site_sets(self):
def test_station_id_sets(self):
expected = "0123"
actual = station.Station(expected)
another = station.Station("ABC")
self.assertEqual(actual.site, expected)
self.assertEqual(another.site, "ABC")

def test_station_service_defaults_to_dv(self):
actual = station.Station()
self.assertEqual(actual.id, expected)
self.assertEqual(another.id, "ABC")

def test_station_dict_returns_dict(self):
actual = station.Station('first')
self.assertIsInstance(actual.station_dict, dict)

def test_multiple_instances_only_one_list(self):
first = station.Station('first')
second = station.Station('second')
self.assertEqual(first.station_dict, second.station_dict)

def test_station_dict_keeps_keys(self):
first = station.Station('first')
second = station.Station('second')
actual = first.station_dict
self.assertIn('first', actual)
self.assertIn('second', actual)
self.assertEqual(len(actual), 2, "The dict length is not equal to the \
number of instances")

def test_station_dict_returns_instance(self):
first = station.Station('first')
second = station.Station('second')
expected = first
# Look at the station_dict; does it contain a ref to 'first'?
actual = second.station_dict['first']
self.assertEqual(actual, expected)


class TestNWIS(unittest.TestCase):
"""Testing the station.NWIS object."""

def test_NWIS_service_defaults_to_dv(self):
actual = station.NWIS()
service = "problem!"
self.assertEqual(actual.service, "dv")

def test_station_start_defaults_to_None(self):
actual = station.Station()
def test_NWIS_start_defaults_to_None(self):
actual = station.NWIS()
self.assertIsNone(actual.start_date)

def test_station_end_defaults_to_None(self):
actual = station.Station()
def test_NWIS_end_defaults_to_None(self):
actual = station.NWIS()
self.assertIsNone(actual.end_date)

def test_station_setters_work(self):
actual = station.Station("A", "B", "C", "D")
self.assertIsInstance(actual, station.Station)
self.assertEqual(actual.site, "A")
def test_NWIS_setters_work(self):
actual = station.NWIS("A", "B", "C", "D")
self.assertIsInstance(actual, station.NWIS)
self.assertEqual(actual.id, "A")
self.assertEqual(actual.service, "B")
self.assertEqual(actual.start_date, "C")
self.assertEqual(actual.end_date, "D")
# self.assertIs(type(actual.fetch), function)

@mock.patch("hydrofunctions.hydrofunctions.get_nwis")
def test_station_fetch_calls_get_nwis_correctly(self, mock_get_nwis):
actual = station.Station("A", "B", "1111-11-11", "1111-11-11")
try_it_out = actual.fetch()
def test_NWIS_fetch_calls_get_nwis_correctly(self, mock_get_nwis):
actual = station.NWIS("A", "B", "1111-11-11", "1111-11-11")
try_it_out = actual.get_data()
mock_get_nwis.assert_called_once_with("A", "B", "1111-11-11", "1111-11-11")

@mock.patch("hydrofunctions.hydrofunctions.get_nwis")
def test_station_fetch2_calls_get_nwis_correctly(self, mock_get_nwis):
actual = station.Station("A", "B", "2002-03-03", "2002-03-03")
try_it_out = actual.fetch()
def test_NWIS_fetch2_calls_get_nwis_correctly(self, mock_get_nwis):
actual = station.NWIS("A", "B", "2002-03-03", "2002-03-03")
try_it_out = actual.get_data()
# This won't raise TypeError for "C" not matching "yyyy-mm-dd"
mock_get_nwis.assert_called_once_with("A", "B", "2002-03-03", "2002-03-03")

Expand Down

0 comments on commit 1493ec2

Please sign in to comment.