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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/booleans -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/conditional_statements -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/lists -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/tuples -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/sets -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/function_definition -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/default_arguments -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/classes_objects -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/constructor -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/polymorphism -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/encapsulation -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} python/build_in_functions -.-> lab-302729{{"`Fix the Incorrectly Implemented Lab Class`"}} end

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.

Summary

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

Other Python Tutorials you may like