Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Your First ABC | The Foundations of ABCs
Python Abstract Base Classes
Osio 1. Luku 4
single

single

Challenge: Your First ABC

Pyyhkäise näyttääksesi valikon

Tehtävä

Pyyhkäise aloittaaksesi koodauksen

You are building the foundation of a reporting system. Your task is to define an abstract base class that enforces a consistent interface across all report types.

  1. Import ABC and abstractmethod from the abc module.
  2. Define an abstract class BaseReport(ABC) with two abstract methods:
    • generate(self, data) – produces and returns the report content as a string;
    • get_format(self) – returns the report format name as a string (e.g. "CSV", "JSON").
  3. Define a concrete class CsvReport(BaseReport) that:
    • implements generate(self, data)data is a list of dicts; returns a CSV string where the first line is the comma-joined keys of the first dict, and the second line is the comma-joined values of the first dict (convert each value to str);
    • implements get_format(self) that returns "CSV".
  4. Create an instance of CsvReport called csv_report.
  5. Store the result of csv_report.get_format() in a variable called report_format.
  6. Store the result of csv_report.generate([{"id": "R-001", "amount": "4500.0", "status": "approved"}]) in a variable called report_output.
  7. Print report_format and report_output.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt