|
| 1 | +--- |
| 2 | +title: "2.3 Analysing Code using a Linter" |
| 3 | +teaching: 10 |
| 4 | +exercises: 0 |
| 5 | +--- |
| 6 | + |
| 7 | +:::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- FIXME |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +::::::::::::::::::::::::::::::::::::: objectives |
| 14 | + |
| 15 | +- FIXME |
| 16 | + |
| 17 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 18 | + |
| 19 | +## Installing a Code Linter |
| 20 | + |
| 21 | +The first thing we need to do is Install pylint |
| 22 | +Now fortunately, pylint can be installed as a Python package |
| 23 | +QUESTION: who has installed a Python package before, using the program pip? Yes/No |
| 24 | +QUESTION: who has created and used a Python virtual environment before? Yes/No |
| 25 | +Ok, so we’re going to create what’s known as a virtual environment |
| 26 | +This is a genuinely very useful technique - I cannot recommend this strongly enough! |
| 27 | +There isn’t a developer I know that doesn’t use virtual environments to install their packages whenever they can |
| 28 | +And to be honest, this could be a topic all on its own |
| 29 | +The idea is that |
| 30 | +Instead of installing Python packages at the level of our machine’s Python installation, which we could do |
| 31 | +We’re going to install them within their own container, which is separate to the machine’s Python installation |
| 32 | +And then we’ll run our Python code only using packages within that virtual environment |
| 33 | +The benefit of this is that it creates a clear separation between the packages we use for this project, and the packages we use for another |
| 34 | +The other benefit is that we don’t end up with a machine’s Python installation with a clutter of a thousand different packages |
| 35 | +Can become very difficult to tell which packages are used for which project |
| 36 | +If someone else needs to run your code for example, by using a virtual environment, you can be sure what your code actually needs as dependencies |
| 37 | + |
| 38 | +## Setting up a Development Environment |
| 39 | + |
| 40 | +So let’s create a Python virtual environment now, and activate it |
| 41 | +So, make sure you’re in the root directory of the repository, then type |
| 42 | +python -m venv venv |
| 43 | +Here, we’re using the built-on Python venv module - short for virtual environment - to create a virtual environment directory called venv |
| 44 | +We could have called the directory anything, but naming it venv is a common convention |
| 45 | +We create the venv within the repository root directory, which is also an established convention |
| 46 | +This makes sure the venv is closely associated with this project, and not easily confused with another |
| 47 | +Once created, we can activate the venv so it’s the one in use |
| 48 | +We do that by: |
| 49 | +(if on Linux or Mac) source venv/bin/activate |
| 50 | +(if on Windows) source venv/Scripts/activate |
| 51 | +QUESTION: who has successfully created and activated their virtual environment? Yes/No? |
| 52 | +Ok, so what’s in this virtual environment? |
| 53 | +we can do: pip list |
| 54 | +We can see this is essentially empty, aside from some default packages that are always installed |
| 55 | +So, the next thing we can do is install any packages needed for this codebase |
| 56 | +It turns out, there isn’t any needed for the code itself |
| 57 | +But - we wish to use pylint, and that’s a python package |
| 58 | +So we can install pylint into our virtual environment using |
| 59 | +pip install pylint |
| 60 | +You’ll notice the prompt changes to reflect that the virtual environment is active - a handy reminder |
| 61 | +And with that, we’re ready to go |
| 62 | +Important thing with virtual environments |
| 63 | +Don’t add the venv directory to a GitHub repository |
| 64 | + |
| 65 | +## Analysing our Code using a Linter |
| 66 | + |
| 67 | +Now the magic can happen - let’s run pylint |
| 68 | +If you run it without any arguments, we can see it’s got a daunting array of options |
| 69 | +But fortunately the basic use is very simple |
| 70 | +Let’s point it at our code and see what it thinks |
| 71 | +pylint climate_analysis.py |
| 72 | +We run this, and it gives us a report |
| 73 | +essentially, a list of issues, and a score |
| 74 | +for each issue, it tells us the filename, the line number and text column the problem occurred, an issue identifier (what type of issue it is), and some text describing this type of error (as well as a shortened form of the error type) |
| 75 | +You’ll notice there’s also a score at the bottom, out of 10 |
| 76 | +Essentially, for every infraction, it deducts from the ideal score of 10 |
| 77 | +It is perfectly possible to get a negative score! It just keeps deducting from 10! |
| 78 | +But we can see here that our score appears very low - 0.59/10 |
| 79 | +If we were to resolve each of these issues in turn, we should get a perfect score |
| 80 | +We can also ask for more information on an error type |
| 81 | +for example, we can see at line 9, near column 35, there is a trailing whitespace |
| 82 | +pylint —help-msg C0303 |
| 83 | +Which is helpful if we need clarification |
| 84 | +if we edit the file, e.g. nano climate-analysis.py |
| 85 | +and go to line 9, column 35, |
| 86 | +(we can use nano’s cursor locator function to help us find column 35), |
| 87 | +we can see that there is an unnecessary space |
| 88 | +I’ve made this visible in nano so you can see it (although usually you wouldn’t) |
| 89 | +QUESTION: who’s managed to run pylint on the example code? Yes/No |
| 90 | +Let’s fix this issue now. Let’s remove the space and re-run pylint on it |
| 91 | +Delete space, save |
| 92 | +pylint climate_analysis.py |
| 93 | +And we see that the error has disappeared and our score has gone up! |
| 94 | +Note that it also gives us a comparison against our last score |
| 95 | +Warning - it can get quite addictive to keep increasing your score |
| 96 | +which might well be the point! |
| 97 | +So looking at the issue types, what do the C, W, R symbols mean? |
| 98 | +Show pylint —long-help |
| 99 | +At the end, we can see a breakdown of what they mean |
| 100 | +I is for informational messages |
| 101 | +C for a programming standards violation - it’s not conforming to the normally accepted conventions of writing good code, things like variable or function naming |
| 102 | +R for a need to refactor, due to a “bad code smell” |
| 103 | +W for warning - something that |
| 104 | +E for error - so pylint think’s it’s spotted a bug (useful, but I wouldn’t depend on this!) |
| 105 | +and F for a fatal pylint error |
| 106 | +So if we run it again on our code |
| 107 | +pylint climate_analysis.py |
| 108 | +We can see that most of our issues are do to with conventions |
| 109 | + |
| 110 | + |
| 111 | +::::::::::::::::::::::::::::::::::::: keypoints |
| 112 | + |
| 113 | +- FIXME |
| 114 | + |
| 115 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
0 commit comments