Abstract
This chapter will introduce you to different ways of approaching the writing of very simple programs in Python and, in particular, will cover Jupyter notebooks and an introduction to the use of the Python debugger. Although both are relatively well-known tools, many people are proficient in the use of one but not both. It will also cover ways of approaching the writing of command-line interfaces and some useful third-party libraries to support succinct command-line tool development.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
You’ll be glad of this the first time you accidentally close the window and lose the code you’re working on.
- 2.
Some text editors integrate a terminal precisely to cut down on this kind of context switching.
- 3.
Pdb allows you to step through each iteration of a list comprehension, as you would do with a loop. This is useful when you have existing code that you are trying to diagnose a problem with, but frustrating when the list comprehension is incidental to your debugging.
- 4.
These can all be abbreviated, as shown in bold. step becomes s, prettyprint becomes pp, etc.
- 5.
I once so badly misunderstood a bug that I overused debug until the pdb prompt looked like ((((((Pdb)))))). This is an antipattern as it’s very easy to accidentally lose your place; try and use conditional breakpoints if you find yourself in a similar situation.
- 6.
You may find documentation recommending import pdb; pdb.set_trace(). This is an older style that is still quite common, but does the same thing albeit without some of the configurability and less readable.
- 7.
In fact, many people prefer to create a virtual environment just for Jupyter and add that to the system path, to avoid any risk of version conflicts in their global namespace.
- 8.
Some editors, such as the professional version of PyCharm IDE and Microsoft’s VSCode editor, have begun to offer a partial equivalent to the notebook interface from within the IDE. They don’t have all the functionality available, but it’s surprisingly good.
- 9.
✔ indicates that our requirements are met, ❌ indicates that they are not, and ⚠ represents that our requirements are met, but with a poor user experience.
- 10.
This means that if your cell ends with an assignment, it won’t show the value being assigned. This is because assignments in Python do not evaluate to a variable. It’s common to explicitly show this with version = sys.version_info
version
While you could also use Python 3.8’s “walrus” operator, (version := sys.version_info), as that does evaluate to the value being assigned, it looks rather strange so I recommend against using it for a stand-alone assignment. This operator is best used in the condition of loops and if statements, where it looks a lot more natural as the parentheses are not required in such cases.
- 11.
Part of the world-routable IPv6 address has been censored in these screenshots.
- 12.
This shortcut only works if the variable is available to the kernel, so you may find you have to run the cell that defines it before you can use the autocompletion. If you’re overwriting the same variable name with different data, then you may see the wrong information, but I’d recommend against doing this where possible as it can be confusing.
- 13.
IDEs and editors that offer notebook compatibility usually have a feature to do this from within the editor window, too.
- 14.
This is usually pronounced “dunder main” for “double underscore” as saying “underscore” four times adds 12 syllables and feels silly.
- 15.
Use ssh-copy-id user@host to set this up automatically, rather than manually editing the authorized_hosts file.
- 16.
If you prefer a console environment to the web environment of the Jupyter notebook, you can see a list of available kernels using jupyter kernelspec list and open an IPython shell connected to the specification of your choice with jupyter console --kernel kernelname.
- 17.
This is part of Adafruit’s excellent CircuitPython ecosystem. They have a lot more information on these sensors and how to use them in a variety of projects at https://learn.adafruit.com/dht
- 18.
This is defined by PEP508 at www.python.org/dev/peps/pep-0508/. There is a table on that page which lists the valid filters, although more may be added in future.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2020 Matthew Wilkes
About this chapter
Cite this chapter
Wilkes, M. (2020). Prototyping and environments. In: Advanced Python Development. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5793-7_1
Download citation
DOI: https://doi.org/10.1007/978-1-4842-5793-7_1
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-5792-0
Online ISBN: 978-1-4842-5793-7
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)