From 7c3fcc33804d68c793098e3a35afc36337dde2db Mon Sep 17 00:00:00 2001 From: Boya Yu Date: Wed, 16 Aug 2017 13:44:57 -0400 Subject: [PATCH 1/2] Raise error when bill is less than one year, remove redundancy --- bpeng/bill/disaggregate.py | 8 ++++++-- bpeng/weather/weather.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bpeng/bill/disaggregate.py b/bpeng/bill/disaggregate.py index 29b414b..94e5845 100644 --- a/bpeng/bill/disaggregate.py +++ b/bpeng/bill/disaggregate.py @@ -273,8 +273,12 @@ class BillDisaggregation(): if (last_date - timedelta(365)).day != last_date.day: days_in_recent_year = 366 days_cumsum = np.array(output['Days In Bill']).cumsum() - periods_in_recent_year = \ - next(i for i, v in enumerate(days_cumsum) if v >= days_in_recent_year) + try: + periods_in_recent_year = \ + next(i for i, v in enumerate(days_cumsum) if v >= days_in_recent_year) + except Exception: + raise ArithmeticError('Days in bill less than one whole year.') + return None bill_in_recent_year = output.iloc[:(periods_in_recent_year + 1)] values_in_recent_year = bill_in_recent_year.iloc[:, 2:].values values_in_recent_year[-1] *= \ diff --git a/bpeng/weather/weather.py b/bpeng/weather/weather.py index 6c9c382..a7e0994 100644 --- a/bpeng/weather/weather.py +++ b/bpeng/weather/weather.py @@ -392,5 +392,5 @@ class WeatherUnderground: dataframe_detail.columns = ['temperature', 'humidity', 'wind', 'rain'] dataframe_detail.index = self.day_list return dataframe_detail - except Exception as _: + except Exception: raise ValueError("Get detail weather first") -- GitLab From 5f462efaea7541ba6e137d7e02a92db503d9d0ee Mon Sep 17 00:00:00 2001 From: Boya Yu Date: Mon, 21 Aug 2017 11:09:08 -0400 Subject: [PATCH 2/2] 'Catch specific exception, remove redundancy' --- bpeng/bill/disaggregate.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bpeng/bill/disaggregate.py b/bpeng/bill/disaggregate.py index 94e5845..b440941 100644 --- a/bpeng/bill/disaggregate.py +++ b/bpeng/bill/disaggregate.py @@ -276,9 +276,8 @@ class BillDisaggregation(): try: periods_in_recent_year = \ next(i for i, v in enumerate(days_cumsum) if v >= days_in_recent_year) - except Exception: + except StopIteration: raise ArithmeticError('Days in bill less than one whole year.') - return None bill_in_recent_year = output.iloc[:(periods_in_recent_year + 1)] values_in_recent_year = bill_in_recent_year.iloc[:, 2:].values values_in_recent_year[-1] *= \ -- GitLab