Coverage for src/usaspending/models/district_spending.py: 100%
31 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 17:15 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 17:15 -0700
1"""District spending model for USASpending spending by district data."""
3from __future__ import annotations
5from typing import Optional, TYPE_CHECKING
7from .spending import Spending
9if TYPE_CHECKING:
10 from ..client import USASpending
13class DistrictSpending(Spending):
14 """Model for spending by congressional district data.
16 Represents spending data grouped by congressional district with
17 district-specific parsing and display logic.
18 """
20 def __init__(self, data: dict, client: Optional["USASpending"] = None):
21 """Initialize DistrictSpending model.
23 Args:
24 data: Raw district spending data from API
25 client: USASpending client instance
26 """
27 super().__init__(data, client)
29 @property
30 def district_code(self) -> Optional[str]:
31 """Congressional district code."""
32 return self.code
34 @property
35 def state_code(self) -> Optional[str]:
36 """Extract state code from district name if available.
38 District names typically follow format like 'TX-12' or 'MS-MULTIPLE DISTRICTS'.
39 """
40 if self.name:
41 # Names like "TX-12" or "MS-MULTIPLE DISTRICTS"
42 parts = self.name.split("-", 1)
43 if len(parts) >= 2:
44 return parts[0]
45 return None
47 @property
48 def district_number(self) -> Optional[str]:
49 """Extract district number from district name if available.
51 Returns the numeric part or special designation like 'MULTIPLE DISTRICTS'.
52 """
53 if self.name:
54 # Names like "TX-12" or "MS-MULTIPLE DISTRICTS"
55 parts = self.name.split("-", 1)
56 if len(parts) >= 2:
57 return parts[1]
58 return None
60 @property
61 def is_multiple_districts(self) -> bool:
62 """Check if this represents multiple districts in a state."""
63 district_num = self.district_number
64 return district_num is not None and "MULTIPLE" in district_num.upper()
66 def __repr__(self) -> str:
67 """String representation of DistrictSpending."""
68 name = self.name or "Unknown District"
69 amount = self.amount or 0
70 return f"<DistrictSpending {name}: ${amount:,.2f}>"