Skip to content

Commit

Permalink
use cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed May 1, 2024
1 parent ad6a385 commit ead2b25
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions buildingmotif/database/table_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,9 @@ def check_template_dependency_relationship(self, dep: DepsAssociation):
from buildingmotif.dataclasses import Template

templ = Template.load(template_id)
params = templ.transitive_parameters()
params = templ.transitive_parameters
dep_templ = Template.load(dependency_id)
dep_params = dep_templ.transitive_parameters()
dep_params = dep_templ.transitive_parameters

# check parameters are valid
if not set(args.values()).issubset(params):
Expand Down
12 changes: 7 additions & 5 deletions buildingmotif/dataclasses/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections import Counter
from copy import copy
from dataclasses import dataclass
from functools import cached_property
from io import BytesIO, StringIO
from itertools import chain
from os import PathLike
Expand Down Expand Up @@ -139,7 +140,7 @@ def remove_dependency(self, dependency: "Template") -> None:
"""
self._bm.table_connection.delete_template_dependency(self.id, dependency.id)

@property
@cached_property
def all_parameters(self) -> Set[str]:
"""The set of all parameters used in this template *including* its
dependencies. Includes optional parameters.
Expand All @@ -155,7 +156,7 @@ def all_parameters(self) -> Set[str]:
params.update(dep.template.parameters)
return params

@property
@cached_property
def parameters(self) -> Set[str]:
"""The set of all parameters used in this template *excluding* its
dependencies. Includes optional parameters.
Expand All @@ -168,7 +169,7 @@ def parameters(self) -> Set[str]:
params = {str(p)[len(PARAM) :] for p in nodes if str(p).startswith(PARAM)}
return params

@property
@cached_property
def dependency_parameters(self) -> Set[str]:
"""The set of all parameters used in this template's dependencies, including
optional parameters.
Expand All @@ -181,7 +182,7 @@ def dependency_parameters(self) -> Set[str]:
params = params.union(dep.template.parameters)
return params

@property
@cached_property
def parameter_counts(self) -> Counter:
"""An addressable histogram of the parameter name counts in this
template and all of its transitive dependencies.
Expand Down Expand Up @@ -238,6 +239,7 @@ def to_inline(self, preserve_args: Optional[List[str]] = None) -> "Template":
replace_nodes(templ.body, to_replace)
return templ

@cached_property
def transitive_parameters(self) -> Set[str]:
"""Get all parameters used in this template and its dependencies.
Expand All @@ -246,7 +248,7 @@ def transitive_parameters(self) -> Set[str]:
"""
params = set(self.parameters)
for dep in self.get_dependencies():
transitive_params = dep.template.transitive_parameters()
transitive_params = dep.template.transitive_parameters
rename_params: Dict[str, str] = {
ours: theirs for ours, theirs in dep.args.items()
}
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_template_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_template_inline_dependencies(bm: BuildingMOTIF):
templ = lib.get_template_by_name("single-zone-vav-ahu")
assert len(templ.get_dependencies()) == 2
inlined = templ.inline_dependencies()
transitive_params = templ.transitive_parameters()
transitive_params = templ.transitive_parameters
assert len(inlined.get_dependencies()) == 0
preserved_params = {
"name",
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_template_inline_dependencies(bm: BuildingMOTIF):
assert set(templ.optional_args) == {"d"}
assert len(templ.get_dependencies()) == 3
inlined = templ.inline_dependencies()
transitive_params = templ.transitive_parameters()
transitive_params = templ.transitive_parameters
assert len(inlined.get_dependencies()) == 0
assert inlined.parameters == {"name", "b", "c", "b-bp", "c-cp", "d", "d-dp"}
assert transitive_params == inlined.parameters
Expand All @@ -140,7 +140,7 @@ def test_template_inline_dependencies(bm: BuildingMOTIF):
assert set(templ.optional_args) == {"b-bp"}
assert len(templ.get_dependencies()) == 1
inlined = templ.inline_dependencies()
transitive_params = templ.transitive_parameters()
transitive_params = templ.transitive_parameters
assert len(inlined.get_dependencies()) == 0
assert inlined.parameters == {"name", "b", "b-bp"}
assert transitive_params == inlined.parameters
Expand All @@ -150,7 +150,7 @@ def test_template_inline_dependencies(bm: BuildingMOTIF):
parent = lib.get_template_by_name("Parent")
assert parent.parameters == {"name", "level1"}
inlined = parent.inline_dependencies()
transitive_params = parent.transitive_parameters()
transitive_params = parent.transitive_parameters
assert inlined.parameters == {
"name",
"level1",
Expand All @@ -164,7 +164,7 @@ def test_template_inline_dependencies(bm: BuildingMOTIF):
parent = lib.get_template_by_name("Parent-opt")
assert parent.parameters == {"name", "level1"}
inlined = parent.inline_dependencies()
transitive_params = parent.transitive_parameters()
transitive_params = parent.transitive_parameters
assert inlined.parameters == {
"name",
"level1",
Expand Down

0 comments on commit ead2b25

Please sign in to comment.