K0nkere / MLOps_Car-prices-project

Covers the most important stages of ML system / Auction car prices prediction model for MLOps-zoomcamp
0 stars 0 forks source link

5.2 Pylint & black & isort #7

Open K0nkere opened 2 years ago

K0nkere commented 2 years ago

Pylint - docs

Under the pipenv shell pipenv install --dev pylint

Checking which pylint pylint --version

Running from the folder that contains scripts pylint prefect_model.py

Running recursively for all .py scripts pylint --recursive=y .

VSCode integration Ctrl+Shift+P > linter > pylint After that lines with warnings will be marked and its is possible to check each message

Creating config - in order to disable some messages create .pylintrc in the folder add in format disable=<message type>

[MESSAGES CONTROL]
disable=missing-final-newline,
             consider-using-f-string

Another type of config-file in the folder pyproject.toml

[tool.pylint.messages_control]

disable = [
    "missing-final-newline",
    "consider-using-f-string",
    "bare-except",
    ]

Maybe its need to select linter in Ctrl+Shift+P again

Disabling message for specific function/class - in the code after message # pylint: disable = too-many-arguments

When needed warnings will be fixed run again pylint --recursive=y . and get error-code for future usage echo $?

K0nkere commented 2 years ago

Black

to fix formatting problems

From the Pipfile folder pipenv install --dev black isort check black --version isort --version

!!! commit before using these in order to be able to restore code !!! it is recommended to first run black --diff filename.py | less - it will show what needs to fix

If black suppose to reformat a list/tuple/dict in a separate lines we can add a , after the final value so it would not touch it tags -S, --skip-string-normalization - black would not replace ' with "

Black configuration - can use pyproject.toml > black configuration > example

[tool.black]
line-length = 80
target-version = ['py39']
skip-string-normalization=True

run to fix the warnings black filename.py

As soon as we have made git commit ... it is possible to check changes git diff filename.py

K0nkere commented 2 years ago

isort

to fix libraries import problems

!!! commit before using these in order to be able to restore code !!! it is recommended to first run isort --diff filename.py | less - it will show what needs to fix

google isort config > Configuration options can use pyproject.toml

[tool.isort]
length_sort = true

length_sort - to sort imports by lenght not by convention

As soon as we have made git commit ... it is possible to check changes git diff filename.py

K0nkere commented 2 years ago

We can run all these things automaticly for all files - . or specifying the filename

isort .
black .
pylint --recursive=y .
pytest /tests