diff --git a/backend/maharashtra_locator.py b/backend/maharashtra_locator.py index d69e6a7c..fd6e076d 100644 --- a/backend/maharashtra_locator.py +++ b/backend/maharashtra_locator.py @@ -55,7 +55,7 @@ def load_maharashtra_pincode_data() -> Dict[str, Dict[str, Any]]: Load and cache Maharashtra pincode to constituency mapping data. Returns: - dict: Dictionary mapping pincode to data + dict: Dictionary of pincode mapping dictionaries keyed by pincode """ file_path = os.path.join( os.path.dirname(__file__), @@ -65,8 +65,16 @@ def load_maharashtra_pincode_data() -> Dict[str, Dict[str, Any]]: with open(file_path, "r", encoding="utf-8") as f: data_list = json.load(f) - # Convert list to dictionary for O(1) lookup - return {item["pincode"]: item for item in data_list} + + # Convert list to dict for O(1) lookup + # Iterate forward and check existence to preserve first-match precedence + data_dict = {} + for entry in data_list: + pincode = entry.get("pincode") + if pincode and pincode not in data_dict: + data_dict[pincode] = entry + + return data_dict @lru_cache(maxsize=1) @@ -75,7 +83,7 @@ def load_maharashtra_mla_data() -> Dict[str, Dict[str, Any]]: Load and cache Maharashtra MLA information data. Returns: - dict: Dictionary mapping constituency to MLA data + dict: Dictionary of MLA information dictionaries keyed by assembly constituency """ file_path = os.path.join( os.path.dirname(__file__), @@ -85,37 +93,14 @@ def load_maharashtra_mla_data() -> Dict[str, Dict[str, Any]]: with open(file_path, "r", encoding="utf-8") as f: data_list = json.load(f) - # Convert list to dictionary for O(1) lookup - return {item["assembly_constituency"]: item for item in data_list} - - -def get_district_by_pincode_range(pincode: int) -> Optional[str]: - """ - Find district by checking pincode ranges. - This is an O(N) fallback where N is number of ranges (~35). - """ - for start, end, district in DISTRICT_RANGES: - if start <= pincode <= end: - return district - return None + data_dict = {} + for entry in data_list: + constituency = entry.get("assembly_constituency") + if constituency and constituency not in data_dict: + data_dict[constituency] = entry -@lru_cache(maxsize=1) -def _load_maharashtra_pincode_map() -> Dict[str, Dict[str, Any]]: - """ - Load and cache Maharashtra pincode to constituency mapping as a dict for O(1) lookup. - """ - pincode_data = load_maharashtra_pincode_data() - return {entry["pincode"]: entry for entry in pincode_data if "pincode" in entry} - - -@lru_cache(maxsize=1) -def _load_maharashtra_mla_map() -> Dict[str, Dict[str, Any]]: - """ - Load and cache Maharashtra MLA information as a dict for O(1) lookup. - """ - mla_data = load_maharashtra_mla_data() - return {entry["assembly_constituency"]: entry for entry in mla_data if "assembly_constituency" in entry} + return data_dict def find_constituency_by_pincode(pincode: str) -> Optional[Dict[str, Any]]: @@ -135,6 +120,7 @@ def find_constituency_by_pincode(pincode: str) -> Optional[Dict[str, Any]]: pincode_map = _load_maharashtra_pincode_map() entry = pincode_map.get(pincode) + entry = pincode_data.get(pincode) if entry: return { "district": entry.get("district"), @@ -162,6 +148,7 @@ def find_mla_by_constituency(constituency_name: str) -> Optional[Dict[str, Any]] mla_map = _load_maharashtra_mla_map() entry = mla_map.get(constituency_name) + entry = mla_data.get(constituency_name) if entry: return { "mla_name": entry.get("mla_name"),