Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions backend/maharashtra_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__),
Expand All @@ -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)
Expand All @@ -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__),
Expand All @@ -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]]:
Expand All @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
Loading