Skip to content

Commit 817a879

Browse files
committed
Forgive broken ecospold1 datasets
1 parent ba128ec commit 817a879

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

bw2io/extractors/ecospold1.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
from io import StringIO
55
from pathlib import Path
6-
from typing import Union
6+
from typing import Union, Any
77

88
import numpy as np
99
import pyecospold
@@ -18,14 +18,25 @@
1818
from tqdm import tqdm
1919

2020

21-
def robust_text(root: etree.ElementBase, attribute: str):
21+
def robust_text(root: etree.ElementBase, attribute: str) -> str | None:
2222
"""Just because the spec says it must be there doesn't mean it will be."""
2323
try:
2424
return getattr(root, attribute).text
2525
except AttributeError:
2626
return None
2727

2828

29+
def robust_nested_attribute(root: etree.ElementBase, attr1: str, attr2: str) -> Any:
30+
"""Try to get nested attribute, and fail gracefully."""
31+
try:
32+
first_level = getattr(root, attr1)
33+
if first_level is None:
34+
return None
35+
return getattr(first_level, attr2)
36+
except AttributeError:
37+
return None
38+
39+
2940
class Ecospold1DataExtractor:
3041
@classmethod
3142
def extract(
@@ -131,12 +142,12 @@ def process_dataset(
131142
"technology": "Technology: " + PI.technology.text,
132143
"timePeriod": "Time period: " + PI.timePeriod.text,
133144
"productionVolume": "Production volume: "
134-
+ (MV.representativeness.productionVolume or ""),
135-
"sampling": "Sampling: " + (MV.representativeness.samplingProcedure or ""),
145+
+ (robust_nested_attribute(MV, "representativeness", "productionVolume") or ""),
146+
"sampling": "Sampling: " + (robust_nested_attribute(MV, "representativeness", "samplingProcedure") or ""),
136147
"extrapolations": "Extrapolations: "
137-
+ (MV.representativeness.extrapolations or ""),
148+
+ (robust_nested_attribute(MV, "representativeness", "extrapolations") or ""),
138149
"uncertaintyAdjustments": "Uncertainty adjustments: "
139-
+ (MV.representativeness.uncertaintyAdjustments or ""),
150+
+ (robust_nested_attribute(MV, "representativeness", "uncertaintyAdjustments") or ""),
140151
}
141152

142153
def get_authors():

0 commit comments

Comments
 (0)