Some time ago I wrote an article about analyzing python code with pylint.
In this case, the article on unit tests with Docker and the pylint one will be combined to show how to run python code analysis with Docker.
The previous articles about Docker are:
-
Starting Django with docker using docker-compose with postgresql as a microservice.
-
Create a Continuous Integration and Deployment environment with Docker for node.js.
-
Install Jenkins through Docker and create a Jenkins Docker image
-
Create a Docker container as a development environment for Sails.js.
The Dockerfile changed a bit compared to the one used for the unit tests: now Debian will be used as the base, pylint will be installed through apt-get, and everything else stays the same as in the unit tests article.
Here is the Dockerfile:
FROM debian
MAINTAINER Ernesto Crespo
RUN apt-get update
RUN apt-get install -y pylint
RUN apt-get clean
WORKDIR /app
COPY . /app
The files used are the same ones used in the unit tests article, found in the github repository.
raizcuadrada
├── Dockerfile
├── LICENSE
├── raizcuadrada.py
├── raizcuadrada_test.py
└── README.md
The raizcuadrada.py module has the following:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Se importa el módulo math para calcular la raiz cuadrada.
"""
import math
#Función raiz cuadrada.
def Raiz(a):
"""Si a es mayor o igual a cero se calcula la raiz cuadrada"""
if a >= 0:
return math.sqrt(a)
#Si es menor a cero se genera una excepción donde se informa que a debe ser mayor o igual a cero.
else:
raise ValueError("a debe ser >= 0")
if __name__ == '__main__':
#Se importa el módulo doctest
import doctest
#Se realiza la prueba al archivo raizcuadra.txt
doctest.testfile("raizcuadrada.txt")
The Docker image is built with the following command:
docker build -t prueba-python .
To run the analysis from docker, run:
docker run -v "$PWD:/app" -ti prueba-python pylint raizcuadrada.py
And the result is the following:
docker run -v "$PWD:/app" -ti prueba-python pylint raizcuadrada.py
No config file found, using default configuration
************* Module raizcuadrada
C: 16, 0: Line too long (101/80) (line-too-long)
C: 12, 0: Invalid function name "Raiz" (invalid-name)
C: 12, 0: Invalid argument name "a" (invalid-name)
Report
======
9 statements analysed.
Messages by category
--------------------
+-----------+-------+---------+-----------+
|type |number |previous |difference |
+===========+=======+=========+===========+
|convention |3 |NC |NC |
+-----------+-------+---------+-----------+
|refactor |0 |NC |NC |
+-----------+-------+---------+-----------+
|warning |0 |NC |NC |
+-----------+-------+---------+-----------+
|error |0 |NC |NC |
+-----------+-------+---------+-----------+
Messages
--------
+--------------+------------+
|message id |occurrences |
+==============+============+
|invalid-name |2 |
+--------------+------------+
|line-too-long |1 |
+--------------+------------+
Global evaluation
-----------------
Your code has been rated at 6.67/10
Raw metrics
-----------
+----------+-------+------+---------+-----------+
|type |number |% |previous |difference |
+==========+=======+======+=========+===========+
|code |9 |36.00 |NC |NC |
+----------+-------+------+---------+-----------+
|docstring |4 |16.00 |NC |NC |
+----------+-------+------+---------+-----------+
|comment |3 |12.00 |NC |NC |
+----------+-------+------+---------+-----------+
|empty |9 |36.00 |NC |NC |
+----------+-------+------+---------+-----------+
Statistics by type
------------------
+---------+-------+-----------+-----------+------------+---------+
|type |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module |1 |NC |NC |100.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|class |0 |NC |NC |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
|method |0 |NC |NC |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
|function |1 |NC |NC |100.00 |100.00 |
+---------+-------+-----------+-----------+------------+---------+
Duplication
-----------
+-------------------------+------+---------+-----------+
| |now |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines |0 |NC |NC |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |NC |NC |
+-------------------------+------+---------+-----------
This is how python code analysis with pylint can be run using Docker.
Make a donation! If you liked the article you can make a donation with Bitcoin (BTC) using the digital wallet of your choice to the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV
Or by scanning the QR code from the wallet:
