From 09f3bc17a0a00b242fd04d2f991d7937dd50279d Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Wed, 24 Apr 2024 22:30:33 -0400 Subject: [PATCH] Fix #840 - pad .zip attribute with 0 and ensure proper loading from court list --- docassemble/AssemblyLine/al_courts.py | 17 +++++++++++++---- docassemble/AssemblyLine/al_general.py | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docassemble/AssemblyLine/al_courts.py b/docassemble/AssemblyLine/al_courts.py index 8c7d662e..412b79dd 100644 --- a/docassemble/AssemblyLine/al_courts.py +++ b/docassemble/AssemblyLine/al_courts.py @@ -3,7 +3,7 @@ """ import os -from typing import Any, Dict, List, Optional, Union, Set +from typing import Any, Callable, Dict, List, Optional, Union, Set import pandas as pd import docassemble.base.functions from docassemble.base.util import ( @@ -205,6 +205,7 @@ class ALCourtLoader(DAObject): Attributes: filename (str): Path to the file containing court information. + converters (Dict[str, Callable]): A dictionary of functions to apply to columns in the dataframe. """ def init(self, *pargs, **kwargs): @@ -413,6 +414,8 @@ def _load_courts(self) -> pd.DataFrame: The method determines the file type (.csv, .xlsx, or .json) based on its extension and reads it accordingly. + If the "callable" attribute is defined on the instance, it will be used to convert the data in the dataframe. + Returns: pd.DataFrame: A dataframe containing the list of courts. @@ -429,14 +432,20 @@ def _load_courts(self) -> pd.DataFrame: else: load_path = str(self.filename) + def convert_zip(z: Any) -> str: + return str(z).zfill(5) + + merged_converters = {'address_zip': convert_zip} + if hasattr(self, "converters") and self.converters: + merged_converters.update(self.converters) to_load = path_and_mimetype(load_path)[0] if self.filename.lower().endswith(".xlsx"): - df = pd.read_excel(to_load) + df = pd.read_excel(to_load, converters=merged_converters) elif self.filename.lower().endswith(".csv"): - df = pd.read_csv(to_load) + df = pd.read_csv(to_load, converters=merged_converters) elif self.filename.lower().endswith(".json"): # TODO: we may need to normalize a JSON file - df = pd.read_json(to_load) + df = pd.read_json(to_load, converters=merged_converters) else: raise Exception( "The datafile must be a CSV, XLSX, or JSON file. Unknown file type: " diff --git a/docassemble/AssemblyLine/al_general.py b/docassemble/AssemblyLine/al_general.py index ea76a8ab..3298956e 100644 --- a/docassemble/AssemblyLine/al_general.py +++ b/docassemble/AssemblyLine/al_general.py @@ -425,7 +425,7 @@ def block( else: output += ", " + str(self.state) if hasattr(self, "zip") and self.zip: - output += " " + str(self.zip) + output += " " + str(self.zip).zfill(5) elif hasattr(self, "postal_code") and self.postal_code: output += " " + str(self.postal_code) if ( @@ -508,7 +508,7 @@ def line_two( else: output += ", " + str(self.state) if hasattr(self, "zip") and self.zip: - output += " " + str(self.zip) + output += " " + str(self.zip).zfill(5) elif hasattr(self, "postal_code") and self.postal_code: output += " " + str(self.postal_code) return output @@ -576,7 +576,7 @@ def on_one_line( else: output += ", " + str(self.state) if hasattr(self, "zip") and self.zip: - output += " " + str(self.zip) + output += " " + str(self.zip).zfill(5) elif hasattr(self, "postal_code") and self.postal_code: output += " " + str(self.postal_code) if (