Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script file and task completion #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added database_script_code.pdf
Binary file not shown.
80 changes: 80 additions & 0 deletions pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from fpdf import FPDF
import pandas as pd
import sqlite3

# Code to be added to the PDF
code_text = """
import pandas as pd
import sqlite3

#----CSV Files paths
csv_file_0 = 'data/shipping_data_0.csv'
csv_file_1 = 'data/shipping_data_1.csv'
csv_file_2 = 'data/shipping_data_2.csv'
database_path = 'shipment_database.db'

#----Reading CSV files into DataFrames
df0 = pd.read_csv(csv_file_0)
df1 = pd.read_csv(csv_file_1)
df2 = pd.read_csv(csv_file_2)

#---Printing columns to debug
print("Columns in df1:", df1.columns)
print("Columns in df2:", df2.columns)

#---Combining data from shipping_data_1.csv and shipping_data_2.csv
df_combined = pd.merge(df1, df2, on='shipment_identifier')

#---Calculating total quantities for each shipment
df_combined['total_quantity'] = df_combined.groupby('shipment_identifier')['product'].transform('size')

#---Connecting to SQLite database
conn = sqlite3.connect(database_path)
cursor = conn.cursor()

#---Creating table shipping_data_1 if not exists
cursor.execute('''
CREATE TABLE IF NOT EXISTS shipping_data_1 (
shipment_identifier TEXT,
product TEXT,
on_time TEXT,
origin_warehouse TEXT,
destination_store TEXT,
driver_identifier TEXT,
quantity INTEGER
)
''')

#--Inserting data from shipping_data_0.csv
df0.to_sql('shipping_data_0', conn, if_exists='append', index=False)

#---Inserting combined data into shipping_data_1 table
for index, row in df_combined.iterrows():
cursor.execute(
# INSERT INTO shipping_data_1 (shipment_identifier,
# product, on_time, origin_warehouse, destination_store, driver_identifier, quantity)
# VALUES (?, ?, ?, ?, ?, ?, ?)
# (row['shipment_identifier'], row['product'], row['on_time'], row['origin_warehouse'], row['destination_store'], row['driver_identifier'], row['total_quantity']))

#---Commiting and close connection
conn.commit()
conn.close()

print("Data inserted successfully!")
"""

# Creating a PDF object
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Courier", size=8)

# Adding code content to the PDF
for line in code_text.splitlines():
pdf.cell(200, 5, txt=line, ln=True)

# Save the PDF file
pdf_file_path = "database_script_code.pdf"
pdf.output(pdf_file_path)

print(f"PDF created successfully at {pdf_file_path}")
56 changes: 56 additions & 0 deletions script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pandas as pd
import sqlite3

#----CSV Files paths
csv_file_0 = 'data/shipping_data_0.csv'
csv_file_1 = 'data/shipping_data_1.csv'
csv_file_2 = 'data/shipping_data_2.csv'
database_path = 'shipment_database.db'

#----Reading CSV files into DataFrames
df_main = pd.read_csv(csv_file_0)
df_ship_info = pd.read_csv(csv_file_1)
df_ship_loc = pd.read_csv(csv_file_2)

#---Printing columns to debug
print("Columns in df_ship_info:", df_ship_info.columns)
print("Columns in df_ship_loc:", df_ship_loc.columns)

#---Combining data from shipping_data_1.csv and shipping_data_2.csv
df_combined = pd.merge(df_ship_info, df_ship_loc, on='shipment_identifier')

#---Calculating total quantities for each shipment
df_combined['total_qty'] = df_combined.groupby('shipment_identifier')['product'].transform('size')

#---Connecting to SQLite database
connection = sqlite3.connect(database_path)
cur = connection.cursor()

#---Creating table shipping_data_1 if not exists
cur.execute('''
CREATE TABLE IF NOT EXISTS shipping_data_1 (
shipment_identifier TEXT,
product TEXT,
on_time TEXT,
origin_warehouse TEXT,
destination_store TEXT,
driver_identifier TEXT,
quantity INTEGER
)
''')

#--Inserting data from shipping_data_0.csv
df_main.to_sql('shipping_data_0', connection, if_exists='append', index=False)

#---Inserting combined data into shipping_data_1 table
for idx, record in df_combined.iterrows():
cur.execute("""
INSERT INTO shipping_data_1 (shipment_identifier, product, on_time, origin_warehouse, destination_store, driver_identifier, quantity)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (record['shipment_identifier'], record['product'], record['on_time'], record['origin_warehouse'], record['destination_store'], record['driver_identifier'], record['total_qty']))

#---Commiting and close connection
connection.commit()
connection.close()

print("Data inserted successfully!")
Binary file modified shipment_database.db
Binary file not shown.