Course Management and Data Comparison

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to create and manage course information, including creating course instances, storing them in a dictionary, making a backup of the data, updating the course information, and comparing the updated information with the previous data.

👀 Preview

Linux - New Data
COURSE Linux
Lab count: 20
Public: True
Price: 199
Linux - Old Data
COURSE Linux
Lab count: 10
Public: True
Price: 99
---------------------------
Python - New Data
COURSE Python
Lab count: 10
Public: True
Price: 99
Python - Old Data
COURSE Python
Lab count: 7
Public: True
Price: 79
---------------------------

ðŸŽŊ Tasks

In this project, you will learn:

  • How to create Course and BootcampCourse classes
  • How to create instances of BootcampCourse and store them in a dictionary
  • How to make a backup of the course information
  • How to update the number of labs and the price for each course
  • How to compare the updated course information with the previously saved data

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept of inheritance and how to create subclasses in Python
  • Implement methods to update and compare course information
  • Utilize dictionaries to store and manage course data
  • Make backups of data and compare updated information with previous records

Create Course and BootcampCourse Classes

In this step, you will learn how to create the Course and BootcampCourse classes.

  1. Open the course.py file located in the /home/labex/project directory.

  2. In the Course class:

    • Define the __init__ method that takes in the course name, whether it is public or not, and the number of labs.
    • Implement the count property to return the number of labs.
    • Implement the typename method to return the object type.
    • Implement the info method to print the course name and the typename.
    • Implement the is_public method to return the public status of the course.
    class Course:
     OBJECT_TYPE = "COURSE"
    
     def __init__(self, name, public, lab_count):
         self.name = name
         self.public = public
         self.lab_count = lab_count
    
     @property
     def count(self):
         return self.lab_count
    
     def typename(self):
         return self.OBJECT_TYPE
    
     def info(self):
         print(self.typename(), self.name)
    
     def is_public(self):
         return self.public
  3. In the BootcampCourse class:

    • Define the __init__ method that takes in the course name, the number of labs, and the price. Call the __init__ method of the Course class and set the public status to True.
    • Implement the update method that takes in the new number of labs and the new price, and updates the corresponding attributes.
    • Implement the info method that calls the info method of the Course class and then prints the number of labs, the public status, and the price.
    class BootcampCourse(Course):
     def __init__(self, name, lab_count, price):
         super().__init__(name, True, lab_count)
         self.price = price
    
     def update(self, lab_count, price):
         self.lab_count = lab_count
         self.price = price
    
     def info(self):
         super().info()
         print("Lab count:", self.count)
         print("Public:", self.public)
         print("Price:", self.price)

Create and Manage Courses

In this step, you will learn how to create instances of BootcampCourse, store them in a dictionary, and make a backup of the course information.

  1. In the if __name__ == "__main__": block:

    • Create two instances of BootcampCourse: linux_course and python_course.
    • Create a dictionary called course_dict and store the two course instances in it, using the course names as keys.
    • Create a backup of the course_dict by making a deep copy of it and storing it in backup_course_dict.
    if __name__ == "__main__":
       linux_course = BootcampCourse("Linux", 10, 99)
       python_course = BootcampCourse("Python", 7, 79)
    
       ## build course dict
       course_dict = {"Linux": linux_course, "Python": python_course}
    
       ## copy and backup course dict
       backup_course_dict = copy.deepcopy(course_dict)

Update Course Information

In this step, you will learn how to update the number of labs and the price for each course.

  1. In the if __name__ == "__main__": block:

    • Update the number of labs and the price for the linux_course using the update method.
    • Update the number of labs and the price for the python_course using the update method.
    ## update lab count and price
    linux_course.update(20, 199)
    python_course.update(10, 99)

Compare Course Information

In this step, you will learn how to compare the updated course information with the previously saved course information.

  1. In the if __name__ == "__main__": block:

    1. Iterate through the keys in the course_dict (which are "Linux" and "Python").
    2. For each key:
    • Print the course name and the label "- New Data".
    • Call the info method on the corresponding course instance in the course_dict.
    • Print the course name and the label "- Old Data".
    • Call the info method on the corresponding course instance in the backup_course_dict.
    • Print a separator line.
## compare with backup
for name in ("Linux", "Python"):
    print(name, "- New Data")
    course_dict[name].info()
    print(name, "- Old Data")
    backup_course_dict[name].info()
    print("---------------------------")

Run the course.py file to see the output.

python3 course.py

Output:

Linux - New Data
COURSE Linux
Lab count: 20
Public: True
Price: 199
Linux - Old Data
COURSE Linux
Lab count: 10
Public: True
Price: 99
---------------------------
Python - New Data
COURSE Python
Lab count: 10
Public: True
Price: 99
Python - Old Data
COURSE Python
Lab count: 7
Public: True
Price: 79
---------------------------
âœĻ Check Solution and Practice

Summary

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

Other Python Tutorials you may like