Loading video player…

Run the wordcount Command (Solution)

In this lesson, you’ll get instructions on how to solve your first task of the coding challenge. Along the way, you’ll revisit the recommended workflow for using pytest to receive automated feedback and read a brief summary of the key concepts at the end.

Steps to Complete the Task

After carefully reviewing the task requirements and ensuring you understand what’s expected, you can begin working on a solution. This will help you align your solution with the acceptance criteria. Here are the recommended steps to follow when solving a task.

Step 1: Reveal the Acceptance Criteria

Open the terminal now and run the pytest command to check the acceptance criteria for the unlocked tasks:

Failed Acceptance Criteria

Because you haven’t implemented the wordcount command yet, the current task will initially show as failed. You’ll fix it now!

Step 2: Update the wordcount.py File

Switch back to your code editor and open the wordcount.py placeholder file located in the src/ folder within your project. To help you get through the first task, you’ll find some useful comments there:

Python src/wordcount.py
# Uncomment the main() function below to solve your first task:
# def main():
#     pass

Go ahead and uncomment the main() function by removing the hash symbol (#) at the beginning of the highlighted lines. You can also remove the first line if you want to, which would result in the following code snippet:

Python src/wordcount.py
def main():
    pass

You’ve just defined an empty main() function, which is enough to make the wordcount command return successfully with a zero exit status code. This command is already mapped to your main() function through the pyproject.toml file, so there’s nothing else left to do:

TOML pyproject.toml
# ...

[project.scripts]
wordcount = "wordcount:main"

Thanks to this setup, you can now run the wordcount command (without the .py extension) in the terminal:

Shell
(venv) $ wordcount

If the command exits without printing anything in the output, then it’s a good indication that everything went fine. Otherwise, you might see a Python traceback similar to this one:

Shell
(venv) $ wordcount
Traceback (most recent call last):
  File "/home/vscode/.local/bin/wordcount", line 5, in <module>
    from wordcount import main
ImportError: cannot import name 'main' from 'wordcount' (/.../wordcount.py)

The error above means that Python couldn’t find the main() function in your wordcount.py module.

After making this change, save the wordcount.py file and return to the terminal.

Step 3: Verify Your Solution

You can run the pytest command again to verify that the task is now marked as completed:

Hierarchy of Tasks and Their Acceptance Criteria

Well done! Since you’ve satisfied the only acceptance criterion for this task, you’ve been automatically promoted to the next task. To read its instructions, click on the link displayed in the terminal or go to the next lesson. You can also run the pytest --task command.

Summary

You’ve solved this task by defining a Python function called main() in your wordcount.py file. This function doesn’t take any arguments or return a value. Because functions in Python can’t have an empty body, you plugged it with a pass statement as a placeholder to avoid a syntax error.

The main() function is hooked up to the wordcount command in the shell through the pyproject.toml configuration file. This mapping takes effect when you install your Python project with pip. If you installed it before in the editable mode using pip -e, then you don’t need to reinstall it. If you created a GitHub Codespace for this challenge, then the project will have been already installed for your convenience.

What’s Next?

Alright. Now that you’ve gotten acquainted with the recommended workflow and have seen one way of using the pytest-based automation tool, you can try solving the second task on your own. Good luck!

🎯 Jump into the next lesson to get started with your next task.

00:00 In this lesson, you’ll get instructions on how to solve your first task of this coding challenge. Along the way, you’ll revisit the recommended workflow for using pytest to receive automated feedback.

00:12 Finally, you’ll get a brief summary of the key concepts covered in the sample solution provided for this task.

00:19 I’m currently in my GitHub Codespaces environment where the project has already been loaded and installed. This is a web-based VS Code environment that was created specifically for this coding challenge, and it has everything you need to start solving it right away in your web browser.

00:34 Here on the left, you’ll find all the project files including the wordcount.py, which is already open for editing. Below, there’s the terminal window where you can type the usual Unix commands.

00:46 However, you’re most likely going to type one of these. For example, to quickly bring up the instructions for your current task, you want to type pytest --task.

00:57 This will open the corresponding page on Real Python with examples and the acceptance criteria that you must meet in order to advance to the next task.

01:06 As you can see, the first time you try to open an external link in a GitHub Codespaces environment, you’ll see this message. You need to tell GitHub that it’s okay to trust Real Python by adding realpython.com to trusted domains.

01:21 Now, you can read through the instructions. Note that depending on how far you’ve progressed through the challenge, you’ll be taken to a different page with different instructions.

01:31 Okay. After carefully reviewing the task requirements and ensuring that you understand what’s expected, you can begin working on a solution, and you typically do this by implementing a piece of Python code in the editor.

01:44 Conveniently, you can see some helpful comments here that tell you what to do. However, let’s pretend for a second that you’re unsure how to move forward.

01:52 To get some initial feedback, you can run the pytest command without any arguments. This will show you a color-coded tree view of the tasks broken down into their individual acceptance criteria.

02:04 Since you’ve only started, most of these tasks are locked except for task number one, which is currently failing. If you ever get stuck and have a few of these failures in a row, then pytest will try to help you remove any roadblocks by suggesting one or more online resources.

02:20 These are clickable links usually pointing to Real Python resources, such as tutorials, video courses, podcast episodes, and learning paths. They can provide insights and examples to help you overcome any obstacles on your way to completing the task at hand.

02:36 Okay, let’s follow the instructions to see what happens when you do actually solve a task. Switch back to your code editor now and uncomment these two lines.

02:45 You can also remove the first line if you want to. Then make sure to save the file before running pytest again.

02:52 Well done! Since you’ve satisfied the only acceptance criterion for this task, you’ve been automatically promoted to the next task. To read its instructions, you can click on the link displayed in the terminal or navigate to the next lesson.

03:06 You can also run pytest --task again in the terminal. But why did this work exactly? You’ve just defined an empty main() function, which was enough to make the wordcount command return successfully with a zero exit status code.

03:19 This command is already mapped to your main() function through the pyproject.toml file here at the bottom, so there’s nothing else left to do.

03:27 Thanks to this setup. You can now run the wordcount command without the .py extension in the terminal. If the command exits without printing anything in the output, then it’s a good indication that everything went fine.

03:40 Otherwise, you might see a Python traceback with an error message telling you that Python couldn’t find your main() function in the wordcount module.

03:48 To sum up, you’ve solved this task by defining a Python function called main() in your wordcount.py file.

03:54 This function doesn’t take any arguments or return a value. Because functions in Python can’t have an empty body, you plugged it with a pass statement as a placeholder to avoid a syntax error.

04:06 The main() function is hooked up to the wordcount command in the shell through the pyproject.toml configuration file.

04:13 This mapping takes effect when you install your Python project with pip. Alternatively, if you installed it before in the editable mode using the pip -e command, then you don’t need to reinstall the project.

04:26 If you created a GitHub Codespace for this challenge, then the project will have been already installed for your convenience. All right, now that you’ve got acquainted with the recommended workflow and have seen one way of using the pytest-based automation tool, you can try solving the next task on your own.

04:42 Good luck!

Become a Member to join the conversation.