THIS IS ASSIGNMENT 0 IN THE CIS6930 DATA ENGINEERING COURSE. THE ASSIGNMENT FOCUSES ON EXTRACTING DATA. IN THIS ASSIGNMENT GRAB AN INCIDENTS PDF FILE FORM THE NORMAN, OKLAHAMA POLICE DEPARTMENT'S WEBSITE AND THEN EXTRACT THE DATA FROM THIS PDF AND PRINT OUT THE DIFFERENT CATEGORIES OF THE INCIDENTS.
pipenv install
Branch to be used: main
Command to run:
pipenv run python assignment0/main.py --incidents https://www.normanok.gov/sites/default/files/documents/2024-01/2024-01-08_daily_incident_summary.pdf
Video:
DEAssignment0Video.mov
-
download_data(url)
- Downloads data from a given URL.
- Uses the
urllib.requestmodule to make an HTTP request and fetch the data. - Saves the downloaded data to a local file.
-
extract_data_from_pdf(pdf_path)
- Takes a local PDF file path as input.
- Uses the
pypdfmodule to extract text from each page of the PDF. - Splits the extracted text into lines and processes each line to extract relevant information such as date, incident number, location, nature, and incident type.
- Returns a list of dictionaries, where each dictionary represents an incident with extracted information.
-
connectdb()
- Establishes a connection to an SQLite database named "normanpd.db".
- Returns a tuple containing a cursor and the connection.
-
createdb()
- Creates an SQLite table named "incidents" with specific columns for incident details like time, number, location, nature, and ORI.
- Returns the SQLite statement after execution.
-
populatedb(result)
- Takes a list of dictionaries (
result) representing incident data. - Inserts the incident data into the "incidents" table in the SQLite database.
- Returns the number of rows modified after the insert operation.
- Takes a list of dictionaries (
-
status()
- Executes an SQL query to get the count of incidents grouped by nature from the "incidents" table.
- Sorts the result first by count (in descending order) and then alphabetically by nature.
- Returns a sorted list of tuples containing nature and count.
-
print_status()
- Calls
status()to get the sorted incident data. - Prints the sorted data in the format "nature | count".
- Calls
-
getdb()
- Retrieves all data from the "incidents" table in the SQLite database.
- Returns a list of tuples representing all incidents.
-
deletedb()
- Deletes the "incidents" table from the SQLite database.
-
execute_functions(url)
- Downloads a PDF file from the given URL using
download_data(url). - Extracts incident data from the downloaded PDF using
extract_data_from_pdf(pdf). - Deletes the existing "incidents" table, creates a new one, and populates it with the extracted data using
deletedb(),createdb(), andpopulatedb(result). - Prints the status of incidents using
print_status().
- Downloads a PDF file from the given URL using
-
main()
- Defines a command-line interface using
argparse. - Parses the command-line arguments, specifically the
--incidentsargument for the URL. - Calls
execute_functions(url)with the provided URL.
- Defines a command-line interface using
-
Connect to the Database (
connectdb()):- Establish a connection to an SQLite database named "normanpd.db" using the
sqlite3module. - Create a cursor to interact with the database.
- Establish a connection to an SQLite database named "normanpd.db" using the
-
Create the Incidents Table (
createdb()):- Execute an SQL statement to create a table named "incidents" in the database.
- Define the columns of the table, such as
incident_time,incident_number,incident_location,nature, andincident_ori. - Use appropriate data types for each column (e.g., TEXT).
-
Populate the Incidents Table (
populatedb(result)):- For each incident entry in the extracted data:
- Construct an SQL
INSERTstatement to insert the data into the "incidents" table. - Execute the
INSERTstatement using the cursor. - Commit the changes to the database.
- Construct an SQL
- For each incident entry in the extracted data:
-
Query and Sort Incident Data (
status()):- Execute an SQL query to get the count of incidents grouped by nature from the "incidents" table.
- Sort the results first by count (in descending order) and then alphabetically by nature.
-
Print Sorted Incident Data (
print_status()):- Print the sorted incident data in the format "nature | count".
- Display the nature of incidents along with the corresponding count.
-
Retrieve All Incident Data (
getdb()):- Execute an SQL query to retrieve all data from the "incidents" table.
- Return a list of tuples representing all incidents.
-
Delete the Incidents Table (
deletedb()):- Execute an SQL statement to drop the "incidents" table if it already exists.
- This step is usually performed before creating a new table to avoid conflicts.
- It is assumed that the layout of the pdf files will be constant adn it will not change.
- It is also assumed that the except the
Naturefield all the other fields are alphanumeric.
-
test_createdb:- Deletes the database.
- Calls the
createdbfunction. - Asserts that the result of the
createdbfunction is notNone. - Asserts that the table named 'incidents' exists in the database.
-
test_connectdb:- Calls the
connectdbfunction. - Asserts that both the cursor (
cur) and connection (con) are notNone.
- Calls the
-
test_print_status:- Calls the
connectdbfunction. - Populates the database using the
populatedbfunction. - Calls the
print_statusfunction. - Asserts that the printed status matches the expected output.
- Calls the
-
test_populatedb:- Calls the
connectdbfunction. - Calls the
populatedbfunction. - Asserts that the count returned by
populatedbmatches the expected count.
- Calls the
-
test_extract_data_from_pdf:- Uses
pytest.mark.parametrizeto parameterize the test with different PDF paths and expected counts. - Calls the
extract_data_from_pdffunction with different PDF paths. - Asserts that the length of the result matches the expected count.
- Uses