Fix the Incorrectly Implemented Lab Class

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to implement and test the Lab class in the LabEx backend code. The Lab class is a core component that represents an experiment within the LabEx platform.

👀 Preview

lab.tags=['python', 'data science', 'machine learning']
can user1 started the experiment: True
can user2 started the experiment: False

ðŸŽŊ Tasks

In this project, you will learn:

  • How to implement the insert_tag method to ensure that duplicate tags are not added to the lab object
  • How to implement the can_be_started method to determine whether a user can start the experiment, allowing only authenticated and member users to do so
  • How to test the Lab class to ensure it is working as expected

🏆 Achievements

After completing this project, you will be able to:

  • Understand the importance of properly implementing core classes in a backend system
  • Demonstrate the ability to write clean, maintainable, and bug-free code for a class
  • Gain experience in testing and validating the functionality of a class

Implement the Lab Class

In this step, you will learn how to implement the Lab class in the lab.py file. Follow the steps below to complete this step:

  1. Open the lab.py file in your preferred code editor.
  2. Locate the Lab class definition.
  3. Modify the __init__ method to the following code:
def __init__(self, name, tags=None):
    self.name = name
    self._tags = [] if tags is None else [tags]
  1. Implement the insert_tag method to ensure that duplicate tags are not inserted into the lab object. You can do this by checking if the tag already exists in the self._tags list before adding it.
def insert_tag(self, tag):
    """Insert tags and check if the tag already exists"""
    if tag not in self._tags:
        self._tags.append(tag)
  1. Implement the can_be_started method to determine whether the user can start the experiment. This method should return True if the user is authenticated and a member, and False otherwise.
def can_be_started(self, user):
    """Check if the user can start the experiment, only logged-in member users can start the experiment"""
    if user.is_authenticated and user.is_member:
        return True
    else:
        return False
  1. Save the lab.py file.

Test the Lab Class

In this step, you will test the Lab class to ensure that it is working as expected. Follow the steps below to complete this step:

  1. Open the lab.py file in your code editor.
  2. Locate the code at the bottom of the file, which creates a Lab object and tests the can_be_started method.
  3. Run the lab.py file using the following command:
python lab.py
  1. Observe the output, which should be:
lab.tags=['python', 'data science', 'machine learning']
can user1 started the experiment: True
can user2 started the experiment: False

This output verifies that the Lab class is working as expected, with the following behavior:

  • The Lab object has the expected tags without any duplicates.
  • The can_be_started method correctly determines that the authenticated and member user1 can start the experiment, but the unauthenticated and non-member user2 cannot.
âœĻ 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