Skip to content

Commit

Permalink
Merge pull request #24 from zoltanszocs/list-properties-for-a-process
Browse files Browse the repository at this point in the history
list_available_properties can execute checks now also for a target process only
  • Loading branch information
cmutel authored Sep 19, 2024
2 parents ab1abd8 + b4277ea commit c200ec1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
11 changes: 9 additions & 2 deletions multifunctional/custom_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ def _get_unified_properties(edge: Exchange):
return properties


def list_available_properties(database_label: str):
def list_available_properties(database_label: str, target_process: Optional[Node] = None):
"""
Get a list of all properties in a database, and check their suitability for use.
`database_label`: String label of an existing database.
`target_process`: Optional. If provided, property checks are done only for this process.
If not provided, checks are done for the whole database.
Returns a list of tuples like `(label: str, message: MessageType)`. Note that
`NONNUMERIC_PROPERTY` is worse than `MISSING_PROPERTY` as missing properties can be assumed to
be zero, but non-numeric ones break everything.
"""
if database_label not in databases:
raise ValueError(f"Database `{database_label}` not defined in this project")
if target_process is not None and target_process.get("database") != database_label:
raise ValueError(f"Target process must be also in database `{database_label}`")

results = []
all_properties = set()
Expand All @@ -69,7 +73,10 @@ def list_available_properties(database_label: str):
all_properties.add(key)

for label in all_properties:
check_results = check_property_for_allocation(database_label, label)
if target_process is None:
check_results = check_property_for_allocation(database_label, label)
else:
check_results = check_property_for_process_allocation(target_process, label)
if check_results is True:
results.append((label, MessageType.ALL_VALID))
elif any(
Expand Down
29 changes: 29 additions & 0 deletions tests/fixtures/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,33 @@
"type": "emission",
"categories": ("air",),
},
("errors", "3"): {
"name": "process - 1",
"type": "multifunctional",
"exchanges": [
{
"functional": True,
"type": "production",
"input": ("errors", "c"),
"amount": 4,
},
{
"functional": True,
"type": "production",
"desired_code": "first one here",
"name": "first product - 1.1",
"unit": "kg",
"amount": 4,
"properties": {
"price": 7,
"mass": 9,
},
},
{
"type": "biosphere",
"amount": 12,
"input": ("errors", "2"),
},
],
},
}
27 changes: 27 additions & 0 deletions tests/test_custom_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,30 @@ def test_list_available_properties_errors(errors):
]
for obj in list_available_properties("errors"):
assert obj in expected


def test_list_available_properties_for_process_basic(basic):
basic.metadata["default_allocation"] = "price"
basic.process()
expected = [
("price", MessageType.ALL_VALID),
("mass", MessageType.ALL_VALID),
("manual_allocation", MessageType.ALL_VALID),
]
for obj in list_available_properties("basic", get_node(code="1")):
assert obj in expected


def test_list_available_properties_for_process_errors(errors):
expected_1 = [
("price", MessageType.ALL_VALID),
("mass", MessageType.NONNUMERIC_PROPERTY),
]
for obj in list_available_properties("errors", get_node(code="1")):
assert obj in expected_1
expected_3 = [
("price", MessageType.ALL_VALID),
("mass", MessageType.ALL_VALID),
]
for obj in list_available_properties("errors", get_node(code="3")):
assert obj in expected_3

0 comments on commit c200ec1

Please sign in to comment.