Skip to content

Instantly share code, notes, and snippets.

@mcrd25
Last active January 21, 2023 23:28
Show Gist options
  • Save mcrd25/1de8a25be58a51e56a8476cf3ee6598d to your computer and use it in GitHub Desktop.
Save mcrd25/1de8a25be58a51e56a8476cf3ee6598d to your computer and use it in GitHub Desktop.
Google Automation Cert final captsone
#!/usr/bin/env python3
from PIL import Image
import os
dir = 'path/to/images'
for file in os.listdir(dir):
"""to ignore .DS_STORE file"""
if not file.startswith("."):
with Image.open(dir+"/"+file) as img:
new_file = "/opt/icons/"+file
img.rotate(90).resize((128,128)).convert('RGB').save(new_file,'JPEG')
#!/usr/bin/env python3
import os
import json
import locale
import sys
import reports
import emails
def load_data(filename):
"""Loads the contents of filename as a JSON file."""
with open(filename) as json_file:
data = json.load(json_file)
return data
def format_car(car):
"""Given a car dictionary, returns a nicely formatted name."""
return "{} {} ({})".format(
car["car_make"], car["car_model"], car["car_year"])
def process_data(data):
"""Analyzes the data, looking for maximums.
Returns a list of lines that summarize the information.
"""
max_revenue = {"revenue": 0}
max_sales = {"total_sales": 0}
popular_years = {}
for item in data:
# Calculate the revenue generated by this model (price * total_sales)
# We need to convert the price from "$1234.56" to 1234.56
item_price = locale.atof(item["price"].strip("$"))
item_revenue = item["total_sales"] * item_price
if item_revenue > max_revenue["revenue"]:
item["revenue"] = item_revenue
max_revenue = item
# TODO: also handle max sales
# TODO: also handle most popular car_year
if item["total_sales"] > max_sales["total_sales"]:
max_sales = item
year_key = item["car"]["car_year"]
popular_years[year_key] = popular_years.get(year_key, 0) + item["total_sales"]
max_year = max(popular_years, key=popular_years.get)
summary = [
"The {} generated the most revenue: ${}".format(
format_car(max_revenue["car"]), max_revenue["revenue"]),
"The {} had the most sales: {}".format(
format_car(max_sales["car"]), max_sales["total_sales"]),
"The most popular year was {} with {} sales.".format(max_year, popular_years[max_year]),
]
return summary
def cars_dict_to_table(car_data):
"""Turns the data in car_data into a list of lists."""
table_data = [["ID", "Car", "Price", "Total Sales"]]
for item in car_data:
table_data.append([item["id"], format_car(item["car"]), item["price"], item["total_sales"]])
return table_data
def main(argv):
"""Process the JSON data and generate a full report out of it."""
data = load_data("car_sales.json")
summary = process_data(data)
print(summary)
sum_str = ""
for line in summary:
sum_str+= line + "<br/>"
cars_table = cars_dict_to_table(data)
# TODO: turn this into a PDF report
reports.generate("/tmp/cars.pdf", "Car Report", sum_str, cars_table)
# TODO: send the PDF report as an email attachment
sender = "automation@example.com"
receiver = "{}@example.com".format(os.environ.get('USER'))
subject = "Sales summary for last month"
body = ""
for line in summary:
body += line + "\n"
emails.send(emails.generate(sender, receiver, subject, body, "/tmp/cars.pdf") )
if __name__ == "__main__":
main(sys.argv)
#!/usr/bin/env python3
from PIL import Image
import os
dir = 'supplier-data/images/'
for file in os.listdir(dir):
"""to ignore .DS_STORE file"""
filename, ext = os.path.splitext(file)
if ext == '.tiff':
with Image.open(dir+file) as img:
new_file = dir+filename+'.jpeg'
img.convert('RGB').resize((600,400)).save(new_file,'JPEG')
#!/usr/bin/env python3
import email.message
import mimetypes
import os.path
import smtplib
def generate(sender, recipient, subject, body, attachment_path):
# Basic Email formatting
message = email.message.EmailMessage()
message["From"] = sender
message["To"] = recipient
message["Subject"] = subject
message.set_content(body)
# Process the attachment and add it to the email
if attachment_path != None:
attachment_filename = os.path.basename(attachment_path)
mime_type, _ = mimetypes.guess_type(attachment_path)
mime_type, mime_subtype = mime_type.split('/', 1)
with open(attachment_path, 'rb') as ap:
message.add_attachment(ap.read(),
maintype=mime_type,
subtype=mime_subtype,
filename=attachment_filename)
return message
def send(message):
mail_server = smtplib.SMTP('localhost')
mail_server.send_message(message)
mail_server.quit()
#!/usr/bin/env python3
import shutil
import psutil
import os
import socket
import emails
import sys
sender = "automation@example.com"
receiver = "{}@example.com".format(os.environ.get('USER'))
subject = ""
email_body = "Please check your system and resolve the issue as soon as possible."
def system_check():
if psutil.cpu_percent() > 80:
subject = "Error - CPU usage is over 80%"
emails.send(emails.generate(sender, receiver, subject, email_body, None))
if psutil.disk_usage('/').percent < 20:
subject = "Error - Available disk space is less than 20%"
emails.send(emails.generate(sender, receiver, subject, email_body, None))
if psutil.virtual_memory().available / (1024.0 ** 2) < 500:
subject = "Error - Available memory is less than 500MB"
emails.send(emails.generate(sender, receiver, subject, email_body, None))
if socket.gethostbyname('localhost') != '127.0.0.1':
subject = "Error - localhost cannot be resolved to 127.0.0.1"
emails.send(emails.generate(sender, receiver, subject, email_body, None))
def main(argv):
system_check()
if __name__ == "__main__":
main(sys.argv)
#!/usr/bin/env python3
import os
import json
import locale
import sys
import reports
import datetime
import emails
dir = 'supplier-data/descriptions/'
fruit_info = ''
for f in os.listdir(dir):
filename, ext = os.path.splitext(f)
if ext == '.txt':
with open(dir+f, 'r') as file:
lines = file.readlines()
fruit_name = "name: "+lines[0].strip()
fruit_weight = "weight: " +lines[1].strip()
fruit_info += fruit_name+"<br/>"+fruit_weight+"<br/><br/>"
def main(argv):
date_str = datetime.datetime.now().strftime("%B %d, %Y")
title = "Processed Update on "+date_str
print(title)
attachment = '/tmp/processed.pdf'
paragraph = fruit_info
reports.generate_report(attachment, title, paragraph)
sender = "automation@example.com"
receiver = "{}@example.com".format(os.environ.get('USER'))
subject = "Upload Completed - Online Fruit Store"
body = "All fruits are uploaded to our website successfully. A detailed list is attached to this email."
emails.send(emails.generate(sender, receiver, subject, body, attachment))
if __name__ == "__main__":
main(sys.argv)
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
def generate_report(report_file, report_title, report_info):
report = SimpleDocTemplate(report_file)
styles = getSampleStyleSheet()
title = Paragraph(report_title, styles["h1"])
content = Paragraph(report_info, styles["BodyText"])
empty_line = Spacer(1,20)
report.build([title, empty_line, content])
#! /usr/bin/env python3
import os
import requests
import json
dir = '/data/feedback/'
ip = 'your xternal ip address given'
url = 'http://%s/feedback/' % (ip)
for filename in os.listdir(dir):
if filename.endswith('.txt'):
with open(dir+filename, 'r') as file:
data = {}
lines = file.readlines()
data["title"] = lines[0].strip()
data["name"] = lines[1].strip()
data["date"] = lines[2].strip()
data["feedback"] = lines[3].strip()
res = requests.post(url, json=data)
if (res.status_code == 201 ):
print("successfully added feedback from %s!" % (data['name']))
else:
print(res.status_code)
#! /usr/bin/env python3
import os
import requests
dir = 'supplier-data/descriptions/'
ip = ''
url = 'http://%s/fruits/' % (ip)
for f in os.listdir(dir):
filename, ext = os.path.splitext(f)
if ext == '.txt':
with open(dir+f, 'r') as file:
data = {}
lines = file.readlines()
data['name'] = lines[0].strip()
data['weight'] = int(lines[1].strip().split()[0])
data['description'] = lines[2].strip()
data['image_name'] = filename+'.jpeg'
res = requests.post(url, json=data)
if (res.status_code == 201):
print("successfully added fruit: %s!" % (data['name']))
else:
print(res.status_code)
#!/usr/bin/env python3
import requests
import os
url = "http://localhost/upload/"
dir = "supplier-data/images/"
for file in os.listdir(dir):
if file.endswith(".jpeg"):
with open(dir+file, 'rb') as img:
res = requests.post(url, files={'file': img})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment