The Bites of Py exercises from PyBites are a wonderful way to improve your Python skills in short, focused practice sessions. You can even work on them right from your browser! Of course, that’s not the only way.
Here are a few different ways I might work on a bite. I hope some of these are useful – please share your own habits in the comments!
- Quickstart: Working Directly in the Browser
- Interactive Exploration: Using a REPL
- Testing/Debugging Support: A Full-Featured Editor
- First-time setup
- Per-bite setup
- Test Bites: A New Spin
- There’s No Wrong Way…
Quickstart: Working Directly in the Browser
If a bite appears to have a short solution with reasonably straightforward test cases, I’ll probably give it a try right in the browser. PyBites uses the Ace editor with some nice Python-specific additions such as:
This is a great way to start coding. It’s pleasant to use, with no requirements beyond a capable browser.
If a bite deals with concepts or modules that I’m not familiar with though, I often want to work more interactively. I’m not just submitting code for tests in that case – I’m also reading documentation and experimenting to get a better feel for the concepts in the bite. The browser editor falls short for me in those cases, so I might switch to…
Interactive Exploration: Using a REPL
As long as you have Python installed on your local machine, you’ll be able to run python
to launch the Python interpreter in interactive mode. This gives you a helpful REPL (Read-Eval-Print Loop) where you can explore, try things out, and see the output in real-time.
Depending on the bite you’re working on, you might need to install additional packages. It pays to do a little bit of work to keep your PyBites environment isolated, by following steps like these:
- Prepare a new
pybites
virtual environment. Real Python has a primer on virtual environments that can help you get started.
$ python -m venv pybites
$ source pybites/bin/activate
- Install required packages inside your
pybites
virtual environment. The specific requirements vary from bite to bite, but here are some packages that you’ll need eventually: - requests
- bs4 – for BeautifulSoup/web scraping bites
- feedparser
- python-dateutil
- pandas
$ python -m pip install requests bs4 feedparser python-dateutil pandas
Aside from running python
, there are a number of alternative REPLs available. This includes local tools such as bpython or ptpython, and web-based options like repl.it. My REPL of choice is the ptipython
component of ptpython, with vim keybindings. This is mostly personal preference though, so find the experience that best fits your style!
Sometimes after I’ve done some exploring and feel comfortable with the concepts of a bite, I find that I’m getting hung up with a few failing tests. In that case I am looking for a smoother flow for testing and debugging. I might jump over to…
Testing/Debugging Support: A Full-Featured Editor
With an editor like PyCharm or VS Code, you can run the same tests locally that PyBites runs in the browser. However, locally you’ve got a quicker test cycle and you can debug along the way!
When I set up my editor of choice (currently VS Code) to work on a bite, it goes something like this:
First-time setup
- Set up a directory where pybites code will live. For me, that is
~/code/pybites
.
mkdir -p ~/code/pybites
- Activate the same
pybites
virtual environment I created for use with my REPL. Microsoft has some helpful guidance for working with virtual environments in VS Code.
Per-bite setup
- Create a directory for the bite. In my case, code for bite
20
goes into~/code/pybites/20
. - Copy the code and test files. Again using bite 20 as an example, this means I have code in
~/code/pybites/20/account.py
and tests in~/code/pybites/20/test_account.py
. - Configure tests. This means enabling
pytest
and using the bite directory (such as~/code/pybites/20
) as the test root as described in the documentation.
With the setup steps done, I can discover, run and debug tests quickly.
Test Bites: A New Spin
Now that Test Bites are live, there’s an extra wrinkle to the coding and testing workflow. If you’ve already got a local environment set up though, you’ve already laid the groundwork for testing your tests! The last piece you need is the MutPy mutation testing tool. With that installed, you can run your mutation tests locally just like Bob did in the launch post!
Related Reading
Here are some ways that other PyBites community members get ready for practice:
There’s No Wrong Way…
If you’re practicing on PyBites, you’ll definitely be submitting code from the browser. But what other tools will help you along the way? The options are endless – so go nuts, find something that works for you, and share your own tips in the comments!
Keep calm and code in Python!
— AJ