From a4a74e22e251b03a76e0a1173392e9fee1293096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Koles=C3=A1r?= Date: Tue, 5 Feb 2019 15:19:35 +0100 Subject: [PATCH] Make windiris source dates optional --- camille/source/windiris.py | 46 ++++++++++++++++++++--------------- tests/source/test_windiris.py | 32 +++++++++++++++++------- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/camille/source/windiris.py b/camille/source/windiris.py index 0954ba62..3a468b82 100644 --- a/camille/source/windiris.py +++ b/camille/source/windiris.py @@ -5,6 +5,7 @@ import re import pytz + def _to_string(x): x = re.sub('[^A-Za-z0-9,.]', '', str(x)) return '(' + x + ')' @@ -18,25 +19,31 @@ def _sqlite(start_date, los_id=None, distance=None, status=None): + + query_params = [] + if start_date is not None: + query_params.append(' Timestamp >= "{}" ' + .format(str(start_date.astimezone(pytz.utc) + .replace(tzinfo=None)))) + if end_date is not None: + query_params.append(' Timestamp < "{}" ' + .format(str(end_date.astimezone(pytz.utc) + .replace(tzinfo=None)))) + if los_id is not None: + query_params.append(' "LOS Index" IN {} '.format(_to_string(los_id))) + if distance is not None: + query_params.append(' Distance IN {} '.format(_to_string(distance))) + if status is not None: + query_params.append(' "RWS Status" IN {} '.format(_to_string(status))) + query = ( - 'SELECT * FROM ' + installation #nosec - + ' WHERE Timestamp >= :start AND Timestamp < :end' - + ( '' if los_id is None - else ' AND "LOS Index" IN {}'.format(_to_string(los_id)) ) - + ( '' if distance is None - else ' AND Distance IN {}'.format(_to_string(distance)) ) - + ( '' if status is None - else ' AND "RWS Status" IN {}'.format(_to_string(status)) ) + 'SELECT * FROM ' + _to_string(installation) #nosec + + ('' if len(query_params) == 0 + else ' WHERE ' + 'AND'.join(query_params)) + ';' ) df = pd.read_sql_query(query, connection, - params={ - 'start': str(start_date.astimezone(pytz.utc) - .replace(tzinfo=None)), - 'end': str(end_date.astimezone(pytz.utc) - .replace(tzinfo=None)) - }, index_col='Timestamp', parse_dates={ 'Timestamp': {'utc': True} @@ -107,15 +114,12 @@ def windiris(root, tzinfo=pytz.utc): raise ValueError('{} is not a directory'.format(root)) def windiris_internal(installation, - start_date, - end_date, + start_date=None, + end_date=None, los_id=None, distance=None, status=None): - if start_date.tzinfo is None or end_date.tzinfo is None: - raise ValueError('dates must be timezone aware') - f = os.path.join(root, installation, installation + '_rtd.db' ) if not os.path.isfile(f): @@ -123,6 +127,10 @@ def windiris_internal(installation, conn = sqlite3.connect(f) + if (start_date is not None and start_date.tzinfo is None) or \ + (end_date is not None and end_date.tzinfo is None): + raise ValueError('dates must be timezone aware') + return _sqlite(start_date, end_date, conn, diff --git a/tests/source/test_windiris.py b/tests/source/test_windiris.py index 55c80bd5..4c76f023 100644 --- a/tests/source/test_windiris.py +++ b/tests/source/test_windiris.py @@ -6,18 +6,17 @@ wi = windiris('tests/test_data/windiris') -def test_load_all_data(): - s = datetime(2017, 12, 17, tzinfo=utc) - e = datetime(2018, 10, 23, tzinfo=utc) +all_radial_windspeed = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3] +db_start_datetime = datetime(2017, 12, 17, tzinfo=utc) +db_end_datetime = datetime(2018, 10, 23, tzinfo=utc) - df = wi('inst2', s, e) +def test_load_all_data(): + df = wi('inst2', db_start_datetime, db_end_datetime) assert df.shape[0] == 27 - assert ( - df.radial_windspeed == [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 3, 3, 3, 3, 3, 3 ] - ).all() + assert (df.radial_windspeed == all_radial_windspeed).all() def test_load_one_day(): @@ -138,3 +137,18 @@ def test_no_time_zone(): with pytest.raises(ValueError) as exc: wi('inst2', ntz_date, tz_date) assert 'timezone aware' in str(exc) + +def test_load_all_data_by_no_date(): + df_no_dates = wi('inst2') + + assert (df_no_dates.radial_windspeed == all_radial_windspeed).all() + +def test_load_data_without_start_date(): + df_no_start_date = wi('inst2', end_date=db_end_datetime) + + assert (df_no_start_date.radial_windspeed == all_radial_windspeed).all() + +def test_load_data_without_end_date(): + df_no_end_date = wi('inst2', start_date=db_start_datetime) + + assert (df_no_end_date.radial_windspeed == all_radial_windspeed).all()