Generating a Battle Report

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to generate a battle report based on the data files provided. The battle report will summarize the key information from the battles that occurred at three different enemy bases, including the battle ID, value, total participants, injured, dead, and fallen heroes.

👀 Preview

Battle Report

🎯 Tasks

In this project, you will learn:

  • How to read and understand the data files containing the battle information
  • How to implement the auto_generate_report function to generate the battle report
  • How to enhance the battle report with additional formatting and information
  • How to test and refine the battle report to ensure it meets the requirements

🏆 Achievements

After completing this project, you will be able to:

  • Efficiently read and process data from CSV files
  • Implement a function to generate a structured and informative report
  • Demonstrate your ability to follow step-by-step instructions and complete a coding project
  • Enhance your skills in data manipulation, formatting, and reporting

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/with_statement -.-> lab-302731{{"`Generating a Battle Report`"}} python/conditional_statements -.-> lab-302731{{"`Generating a Battle Report`"}} python/for_loops -.-> lab-302731{{"`Generating a Battle Report`"}} python/lists -.-> lab-302731{{"`Generating a Battle Report`"}} python/build_in_functions -.-> lab-302731{{"`Generating a Battle Report`"}} end

Read and Understand the Data Files

In this step, you will learn how to read and understand the data files provided for the project.

The data files for the three bases are named A.csv, B.csv, and C.csv. Each line of data contains the following information:

  • id: The ID number of the battle.
  • value: The value of the battle objective.
  • total: The total number of participants on our side in the battle.
  • injured: The total number of injuries on our side in the battle.
  • dead: The number of deaths on our side in the battle.
  • hero: The ID number of the deaths in the battle.

Implement the auto_generate_report Function

In this step, you will implement the auto_generate_report function to generate the battle report.

Open the auto_generate_report.py file. In the auto_generate_report.py file, The auto_generate_report function should take a list of file paths as input and return the battle report as a string. The function should read the data from each file, sort the data by the value field in descending order, and then generate the battle report.

Here's the initial code for the auto_generate_report function:

def auto_generate_report(file_paths):
    report = "Battle report as follows:\n"

    for file_path in file_paths:
        with open(file_path, "r") as file:
            reader = csv.DictReader(file)
            data = list(reader)

        data.sort(key=lambda x: int(x["value"]), reverse=True)

        base_name = file_path.split("/")[-1].split(".")[0].upper()
        report += (
            f"Battles occurred at Base {base_name}, sorted by value, are as follows:\n"
        )
        for row in data:
            report += f"Battle ID: {row['id']}, Value: {row['value']}, Total participants in this battle: {row['total']}, Injured: {row['injured']}, Dead: {row['dead']}."
            if row["dead"] == "0":
                report += " Heroes fallen: None.\n"
            else:
                report += f" Heroes fallen: {row['hero']}.\n"

    return report

To complete the implementation, you need to:

  1. Add error handling in case the file paths are not valid or the files cannot be read.
  2. Ensure that the report formatting is consistent and easy to read.
  3. Test the auto_generate_report function with the provided example input and verify that the output matches the expected format.

Test and Refine the Battle Report

In this final step, you will test the auto_generate_report function with the provided example input and refine the report as needed.

  1. Test the function by running the following code:
if __name__ == "__main__":
    file_paths = [
        "/home/labex/project/A.csv",
        "/home/labex/project/B.csv",
        "/home/labex/project/C.csv",
    ]
    print(auto_generate_report(file_paths))

This will generate the battle report based on the data in the provided files.

  1. Run the script using the following command:

    python auto_generate_report.py
    Battle Report
  2. Carefully review the generated battle report and ensure that it meets the following requirements:

    • The report is easy to read and understand.
    • The battle information for each base is sorted in descending order by the value field.
    • The "Heroes fallen" section correctly displays "None" when there are no deaths, and lists the IDs of the fallen heroes when there are deaths.
    • The overall formatting of the report is consistent and visually appealing.
  3. If you identify any issues or areas for improvement, update the auto_generate_report function accordingly and re-test the report.

Once you are satisfied with the generated battle report, you have successfully completed the project!

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like