Skip to content

Commit b29b0e1

Browse files
tests passes
- improved geocoder + performance tests - fixes extension helper
1 parent 7af806b commit b29b0e1

11 files changed

+45596
-1171
lines changed

pystac_monty/extension.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class MontyImpactExposureCategory(StringEnum):
124124
HOUSEHOLDS = "households"
125125

126126

127-
class MontyImpactExposureCatgoryLabel(Mapping):
127+
class MontyImpactExposureCatgoryLabel(Mapping[MontyImpactExposureCategory, str]):
128128
def __init__(self) -> None:
129129
self._data = {
130130
MontyImpactExposureCategory.ALL_PEOPLE: "People (All Demographics)",
@@ -173,7 +173,7 @@ def __getitem__(self, key: MontyImpactExposureCategory) -> str:
173173
def __len__(self) -> int:
174174
return len(self._data)
175175

176-
def __iter__(self) -> Iterator:
176+
def __iter__(self) -> Iterator[MontyImpactExposureCategory]:
177177
return iter(self._data)
178178

179179

@@ -208,7 +208,7 @@ class MontyImpactType(StringEnum):
208208
HIGHEST_RISK = "highest_risk"
209209

210210

211-
class MontyImpactTypeLabel(Mapping):
211+
class MontyImpactTypeLabel(Mapping[MontyImpactType, str]):
212212
def __init__(self) -> None:
213213
self._data = {
214214
MontyImpactType.UNSPECIFIED: "Unspecified",
@@ -247,7 +247,7 @@ def __getitem__(self, key: MontyImpactType) -> str:
247247
def __len__(self) -> int:
248248
return len(self._data)
249249

250-
def __iter__(self) -> Iterator:
250+
def __iter__(self) -> Iterator[MontyImpactType]:
251251
return iter(self._data)
252252

253253

@@ -378,7 +378,7 @@ def __init__(
378378
type: MontyImpactType,
379379
value: float,
380380
unit: str | None = None,
381-
estimate_type: MontyEstimateType = None,
381+
estimate_type: MontyEstimateType | None = None,
382382
) -> None:
383383
self.properties = {}
384384
self.category = category
@@ -451,11 +451,20 @@ def to_dict(self) -> dict[str, Any]:
451451

452452
@staticmethod
453453
def from_dict(d: dict[str, Any]) -> ImpactDetail:
454-
category: str = get_required(d.get(IMPDET_CATEGORY_PROP), "impact_detail", IMPDET_CATEGORY_PROP)
455-
type: str = get_required(d.get(IMPDET_TYPE_PROP), "impact_detail", IMPDET_TYPE_PROP)
454+
category_str: str = get_required(d.get(IMPDET_CATEGORY_PROP), "impact_detail", IMPDET_CATEGORY_PROP)
455+
type_str: str = get_required(d.get(IMPDET_TYPE_PROP), "impact_detail", IMPDET_TYPE_PROP)
456456
value: float = get_required(d.get(IMPDET_VALUE_PROP), "impact_detail", IMPDET_VALUE_PROP)
457+
458+
# Convert strings to enum values
459+
category = MontyImpactExposureCategory(category_str)
460+
impact_type = MontyImpactType(type_str)
461+
462+
# Get optional properties
463+
unit = d.get(IMPDET_UNIT_PROP)
464+
estimate_type_str = d.get(IMPDET_ESTIMATE_TYPE_PROP)
465+
estimate_type = MontyEstimateType(estimate_type_str) if estimate_type_str else None
457466

458-
return ImpactDetail(category, type, value)
467+
return ImpactDetail(category, impact_type, value, unit, estimate_type)
459468

460469

461470
class MontyExtension(
@@ -513,8 +522,8 @@ def correlation_id(self, v: str) -> None:
513522
self._set_property(ITEM_CORR_ID_PROP, v)
514523

515524
def compute_and_set_correlation_id(self, hazard_profiles: HazardProfiles = MontyHazardProfiles()) -> None:
516-
# if the object is an Item, we can generate the correlation id
517-
if isinstance(self.item, pystac.Item):
525+
# if the object is an ItemMontyExtension, we can generate the correlation id
526+
if hasattr(self, 'item') and isinstance(self.item, pystac.Item):
518527
self.correlation_id = self.pairing.generate_correlation_id(self.item, hazard_profiles)
519528
else:
520529
raise ValueError("Correlation ID can only be computed for Items")
@@ -547,7 +556,7 @@ def hazard_codes(self, v: list[str] | None) -> None:
547556
@property
548557
def hazard_detail(self) -> HazardDetail | None:
549558
"""The details of the hazard."""
550-
result = map_opt(self._get_property(ITEM_HAZARD_DETAIL_PROP, dict), HazardDetail)
559+
result = map_opt(HazardDetail.from_dict, self._get_property(ITEM_HAZARD_DETAIL_PROP, dict))
551560
return result
552561

553562
@hazard_detail.setter
@@ -557,7 +566,7 @@ def hazard_detail(self, v: HazardDetail | None) -> None:
557566
@property
558567
def impact_detail(self) -> ImpactDetail | None:
559568
"""The details of the impact."""
560-
result = map_opt(self._get_property(ITEM_IMPACT_DETAIL_PROP, dict), ImpactDetail)
569+
result = map_opt(ImpactDetail.from_dict, self._get_property(ITEM_IMPACT_DETAIL_PROP, dict))
561570
return result
562571

563572
@impact_detail.setter
@@ -567,7 +576,7 @@ def impact_detail(self, v: ImpactDetail | None) -> None:
567576
@property
568577
def episode_number(self) -> int:
569578
"""The episode number."""
570-
return self.properties.get(ITEM_EPISODE_NUMBER_PROP, 0)
579+
return self.properties.get(ITEM_EPISODE_NUMBER_PROP, 0) or 0
571580

572581
@episode_number.setter
573582
def episode_number(self, v: int) -> None:
@@ -603,7 +612,8 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> MontyExtension[T]:
603612

604613
@staticmethod
605614
def enable_extension() -> None:
606-
pystac.extensions.ext.ItemExt.monty = property(lambda self: MontyExtension.ext(self))
615+
# Add monty property to ItemExt
616+
setattr(pystac.extensions.ext.ItemExt, 'monty', property(lambda self: MontyExtension.ext(self)))
607617

608618

609619
class CollectionMontyExtension(MontyExtension[pystac.Collection]):

0 commit comments

Comments
 (0)