Wiki Page: Understanding create_task in Python

Overview

The create_task function is a crucial component in task management applications, where it is used to convert text data into an instance of a Task class. This function is designed to handle text data that contains information about a task, such as its title and urgency level. The function is implemented in two different ways, using a named tuple and a custom class, and is also tested using Python's unittest module.

Implementations of create_task

Using Named Tuple

In one implementation, the create_task function utilizes a named tuple to represent a task. This approach is demonstrated in the following code snippet:

Listing 13.3 Creating a function containing a breakpoint (task_debug.py)

from collections import namedtuple
Task = namedtuple("Task", "title urgency")                #1
 
def obtain_text_data(want_bad):
    text = "Laundry,3#" if want_bad else "Laundry,3"
    return text
 
def create_task(inject_bug: bool):
    breakpoint()                                          #2
    task_text = obtain_text_data(inject_bug)              #3
    title, urgency_text = task_text.split(",")
    urgency = int(urgency_text)
    task = Task(title, urgency)
    return task
 
 
if __name__ == "__main__":
    create_task(inject_bug=False)
#1 Creating a named tuple class
#2 Adding a breakpoint
#3 This is line number 10.

In this version, the create_task function processes text data obtained from the obtain_text_data function. A breakpoint is added to facilitate debugging, allowing developers to pause execution and inspect the program's state.

Using Custom Class

Another implementation of the create_task function uses a custom class to define a task. This is shown in the following listing:

Listing 13.4 Defining a function to be tested (task_func.py)

class Task:                               #1
    def __init__(self, title, urgency):
        self.title = title
        self.urgency = urgency
 

def create_task(text):
    title, urgency_text = text.split(",")
    urgency = int(urgency_text)
    task = Task(title, urgency)
    return task
#1 Creates a custom class

Here, the Task class is explicitly defined with an initializer method to set the title and urgency attributes. The create_task function splits the input text to extract these values and create a Task object.

Testing create_task

To ensure the correctness of the create_task function, it is tested using the unittest module. This module provides a framework for writing and running tests, as illustrated in the following listings:

Listing 13.5 Testing a function using TestCase (test_task_func.py)

from task_func import Task, create_task         #1
import unittest                                 #2
 
class TestTaskCreation(unittest.TestCase):      #3
    def test_create_task(self):
        task_text = "Laundry,3"
        created_task = create_task(task_text)   #4
        self.assertEqual(created_task.__dict__, 
 Task("Laundry", 3).__dict__)
 
 
if __name__ == "__main__":
    unittest.main()
#1 Imports the class and function from the script file
#2 Imports the module
#3 Inherits the TestCase class
#4 Calls the function to be tested

This test case verifies that the create_task function correctly parses the input text and creates a Task object with the expected attributes.

Listing 13.6 Testing multiple functions (test_task_func.py)

from task_func import Task, create_task, create_task_from_dict
import unittest
 
class TestTaskCreation(unittest.TestCase):
    def test_create_task(self):
        task_text = "Laundry,3"
        created_task = create_task(task_text)
        self.assertEqual(created_task.__dict__, 
 Task("Laundry", 3).__dict__)
 
    def test_create_task_from_dict(self):
        task_data = {"title": "Laundry", "urgency": 3}
        created_task = create_task_from_dict(task_data)
        self.assertEqual(created_task.__dict__, 
 Task("Laundry", 3).__dict__)
 
 
if __name__ == "__main__":
    unittest.main()

This listing extends the test suite to include another function, create_task_from_dict, ensuring comprehensive testing of task creation functionalities.

Debugging with Breakpoints

The create_task function in task_debug.py includes a breakpoint to aid in debugging. When executed, the script enters debug mode, allowing developers to step through the code and inspect variables at each step. This is particularly useful for identifying and fixing bugs in the function.

To run the script in debug mode, use the following command:

$ python3 task_debug.py
> /full_path/task_debug.py(10)create_task()
-> task_text = obtain_text_data(inject_bug)
(Pdb)

In debug mode, you can execute lines one by one using commands like n (next) to proceed to the next line, providing a hands-on approach to understanding the function's behavior.