Folks starting out with Python might benefit from a file with some more advanced features in it.
You do not have to understand every feature of the file to use it - just find a place to insert your code.
It is designed to be played about with, broken and then downloaded again if necessary for fresh experiments.
The main thing to play about with is change where it says logging.FATAL
to logging.WARNING
or logging.INFO
or logging.DEBUG
etc. and seeing what new info is shown when you run the file.
By making that experiment you can see about adding logging to your own functions to understand what is happening in them - and whether they are being executed as you want them to be.
The file (called example_script.py):
- gives some explanation of
#!/usr/bin/env python3
- notes source and license and some implications of that
- imports logging libary while configuring it to be silent - then explains how to set it to other levels
- provides a hello world function that runs
- notes that string formatting method is used in making log messages
- explains that line continuation character is used to have long line become two lines on screen while still functioning as one line of code
- includes in the hello world function a nested for loop that will show logging messages if logging level reads
level=logging.DEBUG
- has example
logging.warning()
andlogging.error()
calls and messages with explanations - indicates space for use to add more code
- uses
if __name__ == "__main__":
to provide a section for code that will run only if the script is executed from the commandline rather than import into the interpreter or another file - provides a pass statement so one can comment out the call to hello world and see the result of doing that when run from commandline as script
- calls the hello world function
- includes an example TODO: point to demonstrate an IDE feature - which can make those into lists