Skip to content

Commit

Permalink
DB writer: avoid instantiating multiple instances of the same propert…
Browse files Browse the repository at this point in the history
…y class
  • Loading branch information
apontzen committed Dec 28, 2023
1 parent 18228c0 commit cd816c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tangos/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,15 @@ def providing_classes(property_name_list, handler_class, silent_fail=False, expl
def instantiate_classes(simulation, property_name_list, silent_fail=False, explain=False):
"""Instantiate appropriate property calculation classes for a given simulation and list of property names."""
instances = []
already_instantiated_classes = []

handler_class = type(simulation.get_output_handler())

for property_identifier in property_name_list:
instances.append(providing_class(property_identifier, handler_class, silent_fail, explain)(simulation))
cl = providing_class(property_identifier, handler_class, silent_fail, explain)
if cl not in already_instantiated_classes:
already_instantiated_classes.append(cl)
instances.append(cl(simulation))

return instances

Expand Down
18 changes: 18 additions & 0 deletions tests/test_db_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,21 @@ def test_writer_reports_aggregates(fresh_database):

assert "Succeeded: 15 property calculations" in res
assert "myPropertyTakingTime 1.5s" in res


class MoreThanOneDummyProperty(properties.PropertyCalculation):
names = "dummy_property_t1", "dummy_property_t2"
requires_particle_data = True

def requires_property(self):
return []

def calculate(self, data, entry):
return data.time*data.halo, data.time*data.halo+1

def test_writer_doesnt_duplicate_property_classes(fresh_database):
res = run_writer_with_args("dummy_property_t1", "dummy_property_t2")
assert "Succeeded: 15" in res
assert "Already exists: 0" in res
assert db.get_halo("dummy_sim_1/step.1/1")['dummy_property_t1'] == 1.0
assert db.get_halo("dummy_sim_1/step.1/1")['dummy_property_t2'] == 2.0

0 comments on commit cd816c3

Please sign in to comment.