Skip to content

Commit

Permalink
Fix #840 - pad .zip attribute with 0 and ensure proper loading from c…
Browse files Browse the repository at this point in the history
…ourt list
  • Loading branch information
nonprofittechy committed Apr 25, 2024
1 parent 7bfe3eb commit 09f3bc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 13 additions & 4 deletions docassemble/AssemblyLine/al_courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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: "
Expand Down
6 changes: 3 additions & 3 deletions docassemble/AssemblyLine/al_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 09f3bc1

Please sign in to comment.