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_tagmethod to ensure that duplicate tags are not added to the lab object - How to implement the
can_be_startedmethod to determine whether a user can start the experiment, allowing only authenticated and member users to do so - How to test the
Labclass 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:
- Open the
lab.pyfile in your preferred code editor. - Locate the
Labclass definition. - 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]
- Implement the
insert_tagmethod to ensure that duplicate tags are not inserted into the lab object. You can do this by checking if the tag already exists in theself._tagslist 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)
- Implement the
can_be_startedmethod to determine whether the user can start the experiment. This method should returnTrueif the user is authenticated and a member, andFalseotherwise.
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
- Save the
lab.pyfile.
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:
- Open the
lab.pyfile in your code editor. - Locate the code at the bottom of the file, which creates a
Labobject and tests thecan_be_startedmethod. - Run the
lab.pyfile using the following command:
python lab.py
- 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
Labobject has the expected tags without any duplicates. - The
can_be_startedmethod correctly determines that the authenticated and memberuser1can start the experiment, but the unauthenticated and non-memberuser2cannot.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



