extract module¶
Module for constructing data URLs and fetching crime data CSV files from SLMPD.
ExtractionError
¶
Bases: Exception
Raised if the target URL cannot be formed or network operations fail.
Source code in src/geocrime_stl/etl/extract.py
15 16 17 18 | |
construct_url(month_input=None, year_input=None)
¶
Parses flexible date inputs and builds the target SLMPD data asset URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
month_input
|
str | int | None
|
The month to target, specified as an integer, string number, or full/abbreviated month name. Defaults to the previous month. |
None
|
year_input
|
str | int | None
|
The year to target, specified as an integer or string. Defaults to the current year (or previous year if the current month is January). |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str, int, int]
|
A tuple containing: - url (str): The constructed download URL for the target CSV asset. - month_int (int): The validated numerical month (1-12). - year_int (int): The validated four-digit year. |
Raises:
| Type | Description |
|---|---|
ExtractionError
|
If the month or year input cannot be parsed or mapped properly. |
Source code in src/geocrime_stl/etl/extract.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
fetch_crime_data_csv(url, keep_raw_csv=False)
¶
Downloads raw crime data CSV bytes from the specified URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The target HTTP URL to fetch data from. |
required |
keep_raw_csv
|
bool
|
If True, saves a copy of the downloaded raw bytes locally using the filename derived from the URL. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
The raw content bytes of the downloaded CSV file. |
Raises:
| Type | Description |
|---|---|
ExtractionError
|
If an HTTP, network, or unexpected error occurs during download. |
Source code in src/geocrime_stl/etl/extract.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |