From 015fc18764dbfd7aa657704786e2f584950e93a5 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Sat, 1 Oct 2016 10:16:08 +0100 Subject: [PATCH 01/10] fix forecast.now() not working self.date and datetime were in different formats so never evaluating to TRUE --- datapoint/Forecast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 197c028..0f803f5 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -21,7 +21,7 @@ def now(self): now = None d = datetime.datetime.utcnow() msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 - if self.days[0].date == d.strftime("%Y-%m-%dZ"): + if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: if timestep.name > msm: break From b0512ac02395b7b6db4aa3d638011d3e91b6d431 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Sat, 1 Oct 2016 10:38:55 +0100 Subject: [PATCH 02/10] print stuff for debugging --- datapoint/Forecast.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 0f803f5..0295498 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -20,6 +20,8 @@ def now(self): """ now = None d = datetime.datetime.utcnow() + print(d) + print(self.days[0].date.strftime("%Y-%m-%dZ")) msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: From b2c9672cf1768de252952e09075df3e1a644d20b Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Sat, 1 Oct 2016 10:42:19 +0100 Subject: [PATCH 03/10] different return --- datapoint/Forecast.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 0295498..983fa91 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -20,8 +20,6 @@ def now(self): """ now = None d = datetime.datetime.utcnow() - print(d) - print(self.days[0].date.strftime("%Y-%m-%dZ")) msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: @@ -30,4 +28,5 @@ def now(self): now = timestep return now else: - return False +# return False + return(d, self.days[0].date.strftime("%Y-%m-%dZ")) From 7bb4e137c45405a26d5bc3c1fe1c060eec376292 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Sat, 1 Oct 2016 10:50:36 +0100 Subject: [PATCH 04/10] Revert "fix forecast.now() not working" This reverts commit 015fc18764dbfd7aa657704786e2f584950e93a5. --- datapoint/Forecast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 983fa91..df308c8 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -21,7 +21,7 @@ def now(self): now = None d = datetime.datetime.utcnow() msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 - if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): + if self.days[0].date == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: if timestep.name > msm: break From 3fc8fe6b121f088803dfaf88862467f449e62f57 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Sat, 1 Oct 2016 10:55:50 +0100 Subject: [PATCH 05/10] Fix forecast.now() not working --- datapoint/Forecast.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index df308c8..0f803f5 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -21,12 +21,11 @@ def now(self): now = None d = datetime.datetime.utcnow() msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 - if self.days[0].date == d.strftime("%Y-%m-%dZ"): + if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: if timestep.name > msm: break now = timestep return now else: -# return False - return(d, self.days[0].date.strftime("%Y-%m-%dZ")) + return False From 526513ec67f569e369f4f0d78d92b3f7ef972d92 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Sun, 2 Oct 2016 17:40:05 +0100 Subject: [PATCH 06/10] Unit test to expose forecast.now() bug --- tests/unit/forecast_test.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/unit/forecast_test.py diff --git a/tests/unit/forecast_test.py b/tests/unit/forecast_test.py new file mode 100644 index 0000000..31c8a43 --- /dev/null +++ b/tests/unit/forecast_test.py @@ -0,0 +1,25 @@ +from types import * +from nose.tools import * + +import datetime + +import datapoint + + + +class TestForecast: + + def __init__(self): + self.forecast = datapoint.Forecast.Forecast() + + def test_forecast_now_works(self): + test_day = datapoint.Day.Day() + test_day.date = datetime.datetime.utcnow() + + test_timestep = datapoint.Timestep.Timestep() + test_timestep.name = 1 + + test_day.timesteps.append(test_timestep) + + self.forecast.days.append(test_day) + assert self.forecast.now() From f61e36a4883615054901408b8f10f75553a99f5f Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Tue, 15 Nov 2016 20:23:59 +0000 Subject: [PATCH 07/10] add blank timedelta test --- tests/unit/forecast_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/unit/forecast_test.py b/tests/unit/forecast_test.py index 31c8a43..ea8a207 100644 --- a/tests/unit/forecast_test.py +++ b/tests/unit/forecast_test.py @@ -23,3 +23,6 @@ def test_forecast_now_works(self): self.forecast.days.append(test_day) assert self.forecast.now() + + def test_timedelta_works(self): + return \ No newline at end of file From 7886619c29627fc76021c969ebcc00481d251ce4 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Tue, 15 Nov 2016 20:57:30 +0000 Subject: [PATCH 08/10] Remove timedelta test --- tests/unit/forecast_test.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit/forecast_test.py b/tests/unit/forecast_test.py index ea8a207..31c8a43 100644 --- a/tests/unit/forecast_test.py +++ b/tests/unit/forecast_test.py @@ -23,6 +23,3 @@ def test_forecast_now_works(self): self.forecast.days.append(test_day) assert self.forecast.now() - - def test_timedelta_works(self): - return \ No newline at end of file From d6128f976b6607b9c711e41d71890787bdd28a08 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Tue, 15 Nov 2016 20:59:53 +0000 Subject: [PATCH 09/10] Add fix for python 2.6 --- datapoint/Forecast.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 0f803f5..4a997e4 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -1,4 +1,5 @@ import datetime +import sys class Forecast(object): def __init__(self, api_key=""): @@ -14,13 +15,23 @@ def __init__(self, api_key=""): self.elevation = None self.days = [] + def timedelta_total_seconds(self, timedelta): + return ( + timedelta.microseconds + 0.0 + + (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6 + def now(self): """ Function to return just the current timestep from this forecast """ now = None d = datetime.datetime.utcnow() - msm = (d - d.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 60 + for_total_seconds = d - d.replace(hour=0, minute=0, second=0, microsecond=0) + # python 2.6 does not have timedelta.total_seconds() + if sys.version_info < (2,7): + msm = timedelta_total_seconds(for_total_seconds) / 60 + else: + msm = for_total_seconds.total_seconds() / 60 if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"): for timestep in self.days[0].timesteps: if timestep.name > msm: From 51beab58dc9b26fa9d640978d863e5783dab2501 Mon Sep 17 00:00:00 2001 From: Duncan Parkes Date: Tue, 15 Nov 2016 21:01:46 +0000 Subject: [PATCH 10/10] Fix self.timedelta_total_seconds --- datapoint/Forecast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datapoint/Forecast.py b/datapoint/Forecast.py index 4a997e4..6ca6e4e 100644 --- a/datapoint/Forecast.py +++ b/datapoint/Forecast.py @@ -29,7 +29,7 @@ def now(self): for_total_seconds = d - d.replace(hour=0, minute=0, second=0, microsecond=0) # python 2.6 does not have timedelta.total_seconds() if sys.version_info < (2,7): - msm = timedelta_total_seconds(for_total_seconds) / 60 + msm = self.timedelta_total_seconds(for_total_seconds) / 60 else: msm = for_total_seconds.total_seconds() / 60 if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"):