Skip to content

Commit cc8290e

Browse files
committed
Add tests for setting database metadata when writing with Brightway
1 parent cc54704 commit cc8290e

5 files changed

Lines changed: 336 additions & 34 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.5 (2025-10-27)
44

5+
* Allow passing Brightway `Database` metadata on writing
56
* Revamp packaging with `pyproject.toml` and `src` layout
67
* Fix broken tests
78
* Move to absolute imports and `pathlib` instead of `os.path`

src/wurst/brightway/write_database.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def write_database(self, metadata: Optional[dict] = None) -> ProcessedDataStore:
2222
if metadata is None:
2323
metadata = {}
2424

25+
self.metadata.update(metadata or {})
26+
2527
assert not self.statistics()[2], "Not all exchanges are linked"
2628
assert self.db_name not in databases, "This database already exists"
2729
super().write_database()

src/wurst/brightway25/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Export inventories compatible with the dev release of Brightway 2.5. The functions in ``brightway/extract_database.py`` will work without modification."""
22
import datetime
33
from copy import copy
4-
from typing import List
4+
from typing import List, Optional
55

66
import bw2data as bd
77
import bw_processing as bwp
@@ -17,7 +17,8 @@ def __init__(self, db_name, data):
1717
self.db_name = db_name
1818
self.data = data
1919

20-
def write_database(self):
20+
def write_database(self, metadata: Optional[dict] = None):
21+
self.metadata.update(metadata or {})
2122
super().write_database(process=False)
2223

2324

@@ -30,7 +31,7 @@ def strip_exchanges(ds):
3031
PRODUCTION = ("production", "substitution", "generic production")
3132

3233

33-
def write_brightway25_database(data: List[dict], name: str) -> bd.Database:
34+
def write_brightway25_database(data: List[dict], name: str, metadata: Optional[dict] = None) -> bd.Database:
3435
"""Write a new database compatible with Brightway 2.5 functionality.
3536
3637
Instead of aggregating everything into a new database, we take a new approach. New activities are stored as a new database (``name``), but for exchanges that are modified, we use the 2.5 functionality to write processed arrays which override values in the original database. In other words, the previous approach was to write as much as possible; here we write as little as possible. The end calculation results are the same either way.
@@ -79,7 +80,7 @@ def write_brightway25_database(data: List[dict], name: str) -> bd.Database:
7980
check_duplicate_codes(new_activities)
8081

8182
new_activities = [strip_exchanges(x) for x in data if x.get("modified")]
82-
DeltaImporter(name, new_activities).write_database()
83+
DeltaImporter(name, new_activities).write_database(metadata)
8384

8485
# Exchanges in new activities don't have to have database yet
8586
dependents = (

0 commit comments

Comments
 (0)