03. Makefiles

Makefiles

ND9991 C04 L02 A03 Makefiles

You can follow along with Noah's code here.

Noah's Makefile

Here is what is contained in Noah's Makefile:

setup:
    python3 -m venv ~/.myrepo

install:
    pip install -r requirements.txt

test:
    python -m pytest -vv --cov=myrepolib tests/*.py
    python -m pytest --nbval notebook.ipynb

lint:
    pylint --disable=R,C myrepolib cli web

all: install lint test

Note that each line is indented with a tab.

Noah's requirements.txt

Below are the libraries included in Noah's requirements.txt file:

pytest
pylint
jupyter
pytest-cov
pandas
nbval
click
flask
requests

Breaking Down the Makefile

If you haven't seen a Makefile before, this may seem like a lot of random commands all jumbled together. Let's briefly go through each.

  • setup: You have seen most of this line before, which is dealing with our Python 3 virtual environment.
  • install: This installs the requirements for our environment. In our case, it also install the pytest and pylint libraries used later on in the Makefile.
  • test: This is broken into two parts for testing.
    • First, it will use .py files in the tests directory. The -vv flag ensures short test durations are still shown (see documentation), while the -cov flag helps to calculate what the test coverage of the code is (see documentation) in a given directory.
    • The second line is used to test Jupyter Notebook cells. The --nbval flag makes pytest pay attention to jupyter notebooks (see documentation).
  • lint: This will lint what is in the myrepolib directory, as well as the cli.py and web.py files in our current directory (see video). The --disable=R,C is used to disable the "convention" (C) and "refactor" (R) message classes (see related Stack Overflow post).
  • all: You may notice this line looks a little different than the above lines, with the commands on the same line. This will execute our install, lint and test commands.

After Makefile Creation

Once the Makefile is created, you can use:

  • make install
  • make lint

To install dependencies and test your code.

Makefiles

What is a Makefile's purpose?

SOLUTION: All of the above

Additional References

  • More on Makefiles: LINK
  • Makefile tutorial: LINK