Pythontimeit
Contents
import time
debug = True
def timeit(func):
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
end = time.perf_counter()
if debug:
print(f"{func.__name__} time cost: {end - start:.2f} seconds")
return result
return wrapper
@timeit
def my_function(x, y):
# Do some time-consuming operations
time.sleep(2)
return x + y
# Call the decorated my_function with arguments
result = my_function(3, 4)
# Output:
# my_function time cost: 2.00 seconds
print(result)
# Output: 7
# query from DB and send email after group by and filter
# Import the necessary libraries
import psycopg2
import pandas as pd
# Connect to the database
conn = psycopg2.connect(
host="syn",
database="postgres",
user="postgres",
password="abc123",
port=55432
)
# Create a cursor to execute the query
cur = conn.cursor()
# Execute the query
cur.execute(r"""SELECT app, subapp,jobname, to_char(date_trunc('week', datetime), 'YYMMDD') as week_start, 1 as count
FROM t_jobfailure where date_trunc('day', datetime) > current_date - interval '400 days';""")
# Fetch the results and convert to a DataFrame
results = pd.DataFrame(cur.fetchall(), columns=["app", "subapp", "jobname", "week_start", "count"])
# Close the cursor and connection
cur.close()
conn.close()
# Group the DataFrame by week start date and count the records in each group
results = results.groupby(["app", "subapp", "jobname", "week_start"]).count()
# print(results.reset_index())
results = results.reset_index()
# print(results)
# Filter the results to show only those with a count greater than 5
results = results[results["count"] > 0]
results = results.sort_values(by="week_start")
# Pivot the results
results = results.pivot(index=["app", "subapp", "jobname"], columns="week_start", values="count").fillna(0)
results = results.astype(int)
results = results.reset_index()
# Get the name of the last column in the DataFrame
last_column_name = results.columns[-1]
# Sort the DataFrame by the values in the last column in descending order
results = results.sort_values(by=last_column_name, ascending=False)
# print(results)
# Remove the index name from the DataFrame
results = results.rename_axis(index=None, columns=None)
# print(results)
# Convert the DataFrame to an HTML table
table = results.to_html(index=False)
# Import the BeautifulSoup library
from bs4 import BeautifulSoup
# Parse the HTML table
soup = BeautifulSoup(table, "html.parser")
# Find all table cells in the third column (starting from 0)
cells = soup.find_all("td")
# Loop through the cells and change the background color of cells with values greater than 3
for cell in cells:
try:
if int(cell.text) > 0:
cell["style"] = "background-color: pink;"
except Exception:
pass
# Get the updated HTML table
table = str(soup)
# Print the table
# print(table)
# Open the file in write mode
with open("myfile.html", "w") as f:
# Write to the file
f.write(table)
Author JimmyZhang
LastMod 2022-12-08