Skip to content

Commit

Permalink
-remove duplicated code into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-harder committed Nov 13, 2024
1 parent a4ea3ba commit 8b0bed3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
16 changes: 16 additions & 0 deletions assume/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,19 @@ def suppress_output():
os.close(saved_stdout_fd)
os.close(saved_stderr_fd)
os.close(devnull)


def str_to_bool(val):
"""Convert a string representation of truth to True or False.
True values are 'y', 'yes', 't', 'true', 'on', and '1';
false values are 'n', 'no', 'f', 'false', 'off', and '0'.
Raises ValueError if 'val' is anything else.
"""
val = val.lower()
if val in {"y", "yes", "t", "true", "on", "1"}:
return True
elif val in {"n", "no", "f", "false", "off", "0"}:
return False
else:
raise ValueError(f"Invalid truth value: {val!r}")

Check warning on line 692 in assume/common/utils.py

View check run for this annotation

Codecov / codecov/patch

assume/common/utils.py#L692

Added line #L692 was not covered by tests
29 changes: 12 additions & 17 deletions assume/units/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)

from assume.common.base import SupportsMinMax
from assume.common.utils import str_to_bool
from assume.units.dsm_load_shift import DSMFlex

SOLVERS = ["appsi_highs", "gurobi", "glpk", "cbc", "cplex"]
Expand Down Expand Up @@ -134,28 +135,22 @@ def __init__(

# Configuration for electric vehicles selling energy to the market
if self.has_ev:
self.ev_sells_energy_to_market = self.components["electric_vehicle"].get(
"sells_energy_to_market", "true"
).lower() in {"y", "yes", "t", "true", "on", "1"}

self.ev_sells_energy_to_market = str_to_bool(
self.components["electric_vehicle"].get(
"sells_energy_to_market", "true"
)
)
# Configuration for battery storage selling energy to the market
if self.has_battery_storage:
self.battery_sells_energy_to_market = self.components[
"generic_storage"
].get("sells_energy_to_market", "true").lower() in {
"y",
"yes",
"t",
"true",
"on",
"1",
}
self.battery_sells_energy_to_market = str_to_bool(
self.components["generic_storage"].get("sells_energy_to_market", "true")
)

# Configure PV plant power profile based on availability
if self.has_pv:
uses_power_profile = self.components["pv_plant"].get(
"uses_power_profile", "false"
).lower() in {"y", "yes", "t", "true", "on", "1"}
uses_power_profile = str_to_bool(
self.components["pv_plant"].get("uses_power_profile", "false")
)
profile_key = (
f"{self.id}_pv_power_profile"
if not uses_power_profile
Expand Down

0 comments on commit 8b0bed3

Please sign in to comment.