Python distribution used in this course: Anaconda
We use the Anaconda Python distribution. It is available at www.anaconda.com/download and contains:
- Python 3.7 or newer;
- many standard and useful libraries;
- the Spyder IDE (Integrated Development Environment), which we use in class and during the exam.
For more details on how to install Anaconda see https://docs.anaconda.com/anaconda/install
NOTICE: when installing Anaconda in Windows, choose the “Only for me” option.
Required/useful libraries
Once you have installed Anaconda, you must/could install these other Anaconda packages
Library | Needed for | Anaconda Prompt command | How to use it |
---|---|---|---|
ddt | Run tests read from a JSON file | conda install -c conda-forge ddt | It's imported by the test machinery |
pytest-timeout | Apply a timeout to each test execution | conda install pytest-timeout | Adds the new parameter --timeout=<seconds> to the pytest command |
stopit | Apply a timeout only to your function execution | conda install -c conda-forge stopit | It's imported by the test machinery |
pytest-profiling | Examine the time spent in each called function | conda install -c conda-forge pytest-profiling | Adds the new parameter --profile to the pytest command |
Spyder | IDE (editor+debugger) | conda install spyder | |
spyder-unittest | Run tests from inside Spider | conda install -c conda-forge spyder-unittest | Adds a new tab to Spyder to run/show tests (choose pytest and the directory) |
spyder-line-profiler | Examine the execution times from inside Spyder | conda install -c conda-forge spyder-line-profiler | Adds a new menu command to Spyder to profile the execution times of all functions |
radon | Compute the “intricacy” of your code
(maximum cyclomatic complexity) |
conda install -c conda-forge radon | radon cc -a -s --show-closures program.py |
Notepad | Write programs. DEPRECATED!! |
DO NOT USE IT: it does not properly handle the Unix newline character in text files ('\n') | |
Notepad++ | Write programs. | https://notepad-plus-plus.org | |
Wordpad | Write programs. | Included in Windows | |
Beyond Compare | Very useful file comparator for both text and pictures | https://www.scootersoftware.com/download.php |
To update all Anaconda libraries use the following command from an "Anaconda Prompt" window:
- conda update --all
Other useful commands
The homework tests are contained in the test_01.py file. Each one is a method that begins with 'test_' and is automatically discovered and run by pytest.
To run all tests:
- pytest test_01.py
-
pytest test_01.py -v --durations 0 # this one lists all the slowest tests
To run a specific test:
- pytest -v test_01.py -k <substring of the text name>
For example, pytest test_01.py -k 2_values -v.
To compute the code “intricacy” (maximum cyclomatic complexity):
- radon cc -a -s --show-closures program01.py.
To compute the average test running time (by taking the best of 3 runs and by averaging on 5 executions for each run):
-
python -u -m timeit -v -v -v -v -n 5 -r 3 -s 'from test_01 import Test' 'Test.main()'
To profile your code and understand which part is slower (if you have installed pytest-profiling):
- pytest -rA -v --profile test_01.py
NOTICE: to get the 20 slowest times you should modify pytest-profiling as follows:
Change file .../Anaconda/lib/python3.*/site-packages/pytest_profiling.py such that line 64 is changed to
pstats.Stats(self.combined, stream=terminalreporter).strip_dirs().sort_stats('tottime').print_stats(20)
instead than
pstats.Stats(self.combined, stream=terminalreporter).strip_dirs().sort_stats('cumulative').print_stats(20)
Snakeviz
To interactively explore the profiling results you can use snakeviz:
- install snakeviz (a .prof files visualizer): conda install snakeviz;
- execute your test with the profiler: pytest --profile test_01.py;
- show the execution times with snakeviz: snakeviz prof/combined.prof;
- your browser opens on a page that allows you to find where most time has been spent;
- look for your implemented function (normally named ex1) and click on its header line: the visualization will zoom on it.
Python 4 Android
Choose Python version 3