From 8793de1f856f69a3f1806edc33dbabdba227b4c0 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 8 Sep 2021 06:42:47 -0700 Subject: [PATCH] add guards for missing data (#8) * update tests * fix test * fix test round 2 * add guards for missing data --- openeihttp/__init__.py | 86 +++++++++++++++++++++++------------------- setup.py | 2 +- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/openeihttp/__init__.py b/openeihttp/__init__.py index 76067a1..41a70cf 100644 --- a/openeihttp/__init__.py +++ b/openeihttp/__init__.py @@ -95,63 +95,71 @@ def update(self) -> None: self._data = data @property - def current_rate(self) -> float: + def current_rate(self) -> float | None: """Return the current rate.""" assert self._data is not None - weekend = False - now = datetime.datetime.today() - month = now.month - 1 - hour = now.hour - if now.weekday() > 4: - weekend = True - table = "energyweekdayschedule" - if weekend: - table = "energyweekendschedule" + if "energyratestructure" in self._data.keys(): + weekend = False + now = datetime.datetime.today() + month = now.month - 1 + hour = now.hour + if now.weekday() > 4: + weekend = True + table = "energyweekdayschedule" + if weekend: + table = "energyweekendschedule" - lookup_table = self._data[table] - rate_structure = lookup_table[month][hour] + lookup_table = self._data[table] + rate_structure = lookup_table[month][hour] - rate = self._data["energyratestructure"][rate_structure][0]["rate"] + rate = self._data["energyratestructure"][rate_structure][0]["rate"] - return rate + return rate + return None @property - def all_rates(self) -> list: + def all_rates(self) -> list | None: """Return the current rate.""" assert self._data is not None - rates = [] - rate_data = self._data["energyratestructure"] - for rate in rate_data: - rates.append(rate[0]["rate"]) + if "energyratestructure" in self._data.keys(): + rates = [] + rate_data = self._data["energyratestructure"] + for rate in rate_data: + rates.append(rate[0]["rate"]) - return rates + return rates + return None @property - def current_demand_rate(self) -> float: + def current_demand_rate(self) -> float | None: """Return the current rate.""" assert self._data is not None - weekend = False - now = datetime.datetime.today() - month = now.month - 1 - hour = now.hour - if now.weekday() > 4: - weekend = True - table = "demandweekdayschedule" - if weekend: - table = "demandweekendschedule" + if "demandratestructure" in self._data.keys(): + weekend = False + now = datetime.datetime.today() + month = now.month - 1 + hour = now.hour + if now.weekday() > 4: + weekend = True + table = "demandweekdayschedule" + if weekend: + table = "demandweekendschedule" - lookup_table = self._data[table] - rate_structure = lookup_table[month][hour] + lookup_table = self._data[table] + rate_structure = lookup_table[month][hour] - rate = self._data["demandratestructure"][rate_structure][0]["rate"] + rate = self._data["demandratestructure"][rate_structure][0]["rate"] - return rate + return rate + return None @property - def demand_unit(self) -> str: + def demand_unit(self) -> str | None: """Return the demand rate unit.""" assert self._data is not None - return self._data["demandrateunit"] + if "demandrateunit" in self._data.keys(): + return self._data["demandrateunit"] + return None @property def rate_name(self) -> str: @@ -166,7 +174,9 @@ def approval(self) -> bool: return self._data["approved"] @property - def distributed_generation(self) -> str: + def distributed_generation(self) -> str | None: """Return the distributed generation name.""" assert self._data is not None - return self._data["dgrules"] + if "dgrules" in self._data.keys(): + return self._data["dgrules"] + return None diff --git a/setup.py b/setup.py index 0650abe..8ad976f 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ PROJECT_DIR = Path(__file__).parent.resolve() README_FILE = PROJECT_DIR / "README.md" -VERSION = "0.1.7" +VERSION = "0.1.8" setup(