Sobes.tech
Junior — Senior

Creating and sending a report

livecode

Task condition

It is necessary to review the ReportGenerator class, which implements the following functions:

  • generating a report in plain text or HTML page;
  • saving the generated report to a file;
  • sending the report to a specified email address;
  • loading a previously saved report from a file.
class ReportGenerator:
    def __init__(self, data):
        self.data = data
        self.report = ""
        self.filename = "report.txt"
        self.report_type = "text"

    def generate_report(self):
        print("Generating report...")
        if self.report_type == "text":
            self.report = self._generate_text_report()
        elif self.report_type == "html":
            self.report = self._generate_html_report()
        else:
            print("Unsupported report type")
        print("Report generated.")

    def _generate_text_report(self):
        report = "Text Report\n"
        for item in self.data:
            report += f"- {item}\n"
        return report

    def _generate_html_report(self):
        report = "<html><body><h1>HTML Report</h1><ul>"
        for item in self.data:
            report += f"<li>{item}</li>"
        report += "</ul></body></html>"
        return report

    def save_report(self):
        with open(self.filename, "w") as f:
            f.write(self.report)
        print(f"Report saved to {self.filename}")

    def send_report(self, email):
        print(f"Sending report to {email}")
        if "@" not in email:
            raise Exception("Invalid email address")
        print("Report sent.")

    def load_previous_report(self):
        print("Loading previous report...")
        with open(self.filename, "r") as f:
            self.report = f.read()
        print("Previous report loaded.")
Creating and sending a report - Live Coding Python - sobes.tech