import requests
import pandas as pd
import sqlite3
from bs4 import BeautifulSoup
from datetime import datetime
Task 1: Log Progress
I'm kind of confused as to what "Use the list of log points provided" means,
so I just guessed where to place the log statements
def log_progress(message):
with open('code_log.txt', 'a') as log_file:
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S:')
log_file.write(f"{now}: {message}\n")
log_file.close()
# Get the first html table from the article on the page
table = soup.find('table', {'class': 'wikitable'})
rows = table.find_all('tr')
data = []
# The task says that I need to extract the first 10 banks, but there are
# 10 banks in total lol
for row in rows[1:11]:
cols = row.find_all('td')
rank = cols[0].get_text(strip=True)
name = cols[1].get_text(strip=True)
mc_usd = float(cols[2].get_text(strip=True).replace(',', ''))
data.append([name, mc_usd])
df = pd.DataFrame(data, columns=['Name', 'MC_USD_Billion'])
log_progress("Data extracted successfully")
return df
def load_to_csv(df):
log_progress("Loading data to CSV...")
df.to_csv('./Largest_banks_data.csv', index=False)
log_progress("Data loaded to CSV successfully")
Task 5: Load to Database
The task make it seem like I load the data into the database AND
also run queries against the newly created table, but I feel like
Assistant
Here's the rewritten function run_queries() along with the code to execute the specified queries:
def run_queries(query_statements, sql_connection):
''' This function runs multiple queries on the database table,
prints the query and its output on the terminal, and logs the execution.'''
for query in query_statements:
log_progress(f"Executing query: {query}")
result = pd.read_sql_query(query, sql_connection)
print(f"Query: {query}\nOutput:\n{result}")
log_progress("Queries executed successfully")
sql_connection.close()
# Establish the SQLite connection
sql_connection = sqlite3.connect('Banks.db')
# Define the queries to be executed
query_statements = [
"SELECT * FROM Largest_banks",
"SELECT AVG(MC_USD_Billion) FROM Largest_banks",
"SELECT Name FROM Largest_banks LIMIT 5"
]
# Execute the queries
run_queries(query_statements, sql_connection)
it is best to keep them separate? I'm sure there is a typo in task 6
def load_to_db(df):
log_progress("Loading data to the database...")
conn = sqlite3.connect('Banks.db')
df.to_sql('Largest_banks', conn, if_exists='replace', index=False)
conn.close()
log_progress("Data loaded to the database successfully")
Task 6: Verify Data in the DAtabase
def verify_db():
log_progress("Verifying that the data is loaded to the database correctly")
conn = sqlite3.connect('Banks.db')
result = pd.read_sql_query("SELECT * FROM Largest_banks", conn)
print(result)
if len(result) == 10:
log_progress("Verification completed successfully. The database contains exactly 10 banks.")
else:
log_progress("Error: Database contains more or less than 10 banks.")
conn.close()
if name == "main":
df_extracted = extract()
df_transformed = transform(df_extracted)
load_to_csv(df_transformed)
load_to_db(df_transformed)
verify_db()
log_progress("Completed successfully")
To match the template below
Code for ETL operations on Country-GDP data
Importing the required libraries
def log_progress(message):
''' This function logs the mentioned message of a given stage of the
code execution to a log file. Function returns nothing'''
def extract(url, table_attribs):
''' This function aims to extract the required
information from the website and save it to a data frame. The
function returns the data frame for further processing. '''
return df
def transform(df, csv_path):
''' This function accesses the CSV file for exchange rate
information, and adds three columns to the data frame, each
containing the transformed version of Market Cap column to
respective currencies'''
return df
def load_to_csv(df, output_path):
''' This function saves the final data frame as a CSV file in
the provided path. Function returns nothing.'''
def load_to_db(df, sql_connection, table_name):
''' This function saves the final data frame to a database
table with the provided name. Function returns nothing.'''
def run_query(query_statement, sql_connection):
''' This function runs the query on the database table and
prints the output on the terminal. Function returns nothing. '''
''' Here, you define the required entities and call the relevant
functions in the correct order to complete the project. Note that this
portion is not inside any function.'''
Assistant: Here is your refactored code matching the provided template:
# Code for ETL operations on Country-GDP data
# Importing the required libraries
import requests
import pandas as pd
import sqlite3
from bs4 import BeautifulSoup
from datetime import datetime
def log_progress(message):
''' This function logs the mentioned message of a given stage of the code execution to a log file. Function returns nothing'''
with open('code_log.txt', 'a') as log_file:
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_file.write(f"{now}: {message}\n")
def extract(url, table_attribs):
''' This function aims to extract the required information from the website and save it to a data frame. The function returns the data frame for further processing. '''
log_progress("Extracting data...")
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', table_attribs)
rows = table.find_all('tr')
data = []
for row in rows[1:11]:
cols = row.find_all('td')
name = cols[1].get_text(strip=True)
mc_usd = float(cols[2].get_text(strip=True).replace(',', ''))
data.append([name, mc_usd])
df = pd.DataFrame(data, columns=['Name', 'MC_USD_Billion'])
log_progress("Data extracted successfully")
return df
def transform(df, csv_path):
''' This function accesses the CSV file for exchange rate information, and adds three columns to the data frame, each containing the transformed version of Market Cap column to respective currencies'''
log_progress("Transforming data...")
exchange_rate_df = pd.read_csv(csv_path)
rates = dict(zip(exchange_rate_df.Currency, exchange_rate_df.Rate))
df['MC_GBP_Billion'] = (df['MC_USD_Billion'] * rates['GBP']).round(2)
df['MC_EUR_Billion'] = (df['MC_USD_Billion'] * rates['EUR']).round(2)
df['MC_INR_Billion'] = (df['MC_USD_Billion'] * rates['INR']).round(2)
log_progress("Data transformed successfully")
return df
def load_to_csv(df, output_path):
''' This function saves the final data frame as a CSV file in the provided path. Function returns nothing.'''
log_progress("Loading data to CSV...")
df.to_csv(output_path, index=False)
log_progress("Data loaded to CSV successfully")
def load_to_db(df, sql_connection, table_name):
''' This function saves the final data frame to a database table with the provided name. Function returns nothing.'''
log_progress("Loading data to the database...")
df.to_sql(table_name, sql_connection, if_exists='replace', index=False)
sql_connection.close()
log_progress("Data loaded to the database successfully")
def run_query(query_statement, sql_connection):
''' This function runs the query on the database table and prints the output on the terminal. Function returns nothing. '''
log_progress("Verifying that the data is loaded to the database correctly")
result = pd.read_sql_query(query_statement, sql_connection)
print(result)
if len(result) == 10:
log_progress("Verification completed successfully. The database contains exactly 10 banks.")
else:
log_progress("Error: Database contains more or less than 10 banks.")
sql_connection.close()
# Define URL and table attributes for extraction
url = "https://web.archive.org/web/20230908091635/https://en.wikipedia.org/wiki/List_of_largest_banks"
table_attribs = {'class': 'wikitable'}
# Define CSV paths and SQL connection details
csv_path = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMSkillsNetwork-PY0221EN-Coursera/labs/v2/exchange_rate.csv"
output_csv_path = './Largest_banks_data.csv'
sql_connection = sqlite3.connect('Banks.db')
table_name = 'Largest_banks'
query_statement = "SELECT * FROM Largest_banks"
# Execute ETL process
df_extracted = extract(url, table_attribs)
df_transformed = transform(df_extracted, csv_path)
load_to_csv(df_transformed, output_csv_path)
load_to_db(df_transformed, sql_connection, table_name)
run_query(query_statement, sql_connection)
log_progress("Completed successfully")
This refactoring organizes the code into modular functions that match the specified template, providing clear documentation for each function's purpose.
User: Rewrite this function to match the requirements below
def run_query(query_statement, sql_connection):
log_progress("Verifying that the data is loaded to the database correctly")
result = pd.read_sql_query(query_statement, sql_connection)
print(result)
if len(result) == 10:
log_progress("Verification completed successfully. The database contains exactly 10 banks.")
else:
log_progress("Error: Database contains more or less than 10 banks.")
Write the function run_queries() that accepts the query statement, and the SQLite3 Connection object, and generates the output of the query. The query statement should be printed along with the query output.
Execute 3 function calls using the queries as mentioned below.
Print the contents of the entire table
Query statement:
1
SELECT * FROM Largest_banks
Copied!
Print the average market capitalization of all the banks in Billion USD.
Query statement:
1
SELECT AVG(MC_GBP_Billion) FROM Largest_banks
Copied!
Print only the names of the top 5 banks
Query statement:
1
SELECT Name from Largest_banks LIMIT 5
Copied!
Make the relevant log entry.
Assistant:
Explanation:
Function run_queries(): This function iterates over a list of SQL query statements, executes each query using the sql_connection, and prints both the query and the result to the console.
Parameters:
query_statements: List of SQL query strings to be executed.
sql_connection: SQLite3 connection object used for executing queries.
Execution:
The function logs the start of each query execution.
It prints each query with its output.
After executing all queries, it logs the successful completion of query executions.
Closes the database connection at the end.
This setup matches the requirements you provided, allowing the execution of multiple SQL queries and logging relevant information.