From 03a0ad238c70622fb0a3aec557986e8c44c218da Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Wed, 4 Feb 2026 11:28:41 +0000 Subject: [PATCH] =?UTF-8?q?Generate=20property=20reference=20to=20uprn=20m?= =?UTF-8?q?ap=20from=20csv=20bytes=20=F0=9F=9F=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/condition/lookups/uprn_lookup.py | 6 ++++-- backend/condition/lookups/uprn_lookup_csv.py | 19 +++++++++++++++++-- .../tests/lookups/test_uprn_lookup_csv.py | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/backend/condition/lookups/uprn_lookup.py b/backend/condition/lookups/uprn_lookup.py index 87cc54fa..b817445d 100644 --- a/backend/condition/lookups/uprn_lookup.py +++ b/backend/condition/lookups/uprn_lookup.py @@ -1,7 +1,9 @@ from abc import ABC, abstractmethod -from typing import Dict +from typing import BinaryIO, Dict class UprnLookup(ABC): - def get_location_ref_to_uprn_map(self, lookup_filepath: str) -> Dict[str, int]: + def get_location_ref_to_uprn_map( + self, lookup_file_stream: BinaryIO + ) -> Dict[str, int]: pass diff --git a/backend/condition/lookups/uprn_lookup_csv.py b/backend/condition/lookups/uprn_lookup_csv.py index 5f84587a..43544ccd 100644 --- a/backend/condition/lookups/uprn_lookup_csv.py +++ b/backend/condition/lookups/uprn_lookup_csv.py @@ -1,4 +1,6 @@ -from typing import BinaryIO, Dict +import csv +from io import TextIOWrapper +from typing import BinaryIO, Dict, TextIO from backend.condition.lookups.uprn_lookup import UprnLookup @@ -6,4 +8,17 @@ class UprnLookupCsv(UprnLookup): def get_location_ref_to_uprn_map( self, lookup_file_stream: BinaryIO ) -> Dict[str, int]: - raise NotImplementedError + text_stream: TextIO = TextIOWrapper(lookup_file_stream, encoding="utf-8") + location_ref_to_uprn_map: Dict[str, int] = {} + + reader = csv.DictReader(text_stream) + for row in reader: + if not row["reference"] or not row["out_uprn"]: + # skip empty rows + continue + + ref = row["reference"].strip() + uprn = int(row["out_uprn"].strip()) + location_ref_to_uprn_map[ref] = uprn + + return location_ref_to_uprn_map diff --git a/backend/condition/tests/lookups/test_uprn_lookup_csv.py b/backend/condition/tests/lookups/test_uprn_lookup_csv.py index 67cb80b9..a2d66a94 100644 --- a/backend/condition/tests/lookups/test_uprn_lookup_csv.py +++ b/backend/condition/tests/lookups/test_uprn_lookup_csv.py @@ -7,7 +7,7 @@ from backend.condition.lookups.uprn_lookup_csv import UprnLookupCsv @pytest.fixture def prop_ref_uprn_csv_bytes() -> BytesIO: - csv_bytes = b"""reference, out_uprn + csv_bytes = b"""reference,out_uprn ABC123,10000000001 DEF456,10000000002 GHI789,10000000003