Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a perfect Python project

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to build a perfect Python project, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

When starting a new Python project, it's easy to dive in and start coding. In fact, taking a little time to choose a good library will save a lot of time for future development and lead to a happier coding experience.

In an ideal world, all developer relationships are interdependent and interrelated (collaborative development), with code in a perfect format, free of low-level errors, and tests covering all code. In addition, all of this will be guaranteed each time it is submitted. (uniform code style, type detection, high test coverage, automatic detection)

First, let's create a new project directory:

Mkdir best_practices cd best_practicespipx installs command line tools for Python tripartite libraries

Pipx [2] is a command-line tool that can be used to quickly install Python tripartite libraries. We will use it to install pipenv and cookiecutter. Install pipx with the following command:

Python3-m pip install-- user pipx python3-m pipx ensurepath uses pipenv for dependency management

Pipenv [3] automatically creates and manages virtualenv (virtual environment) for your project and adds / removes packages from Pipfile when installing / uninstalling packages. It also generates a very important Pipfile.lock to ensure the reliability of dependencies.

This will greatly increase the confidence and fun of programming when you know that you and your teammates are using the same version of the library. Pipenv well solves the problem of using the same library but different versions. Pipenv has received wide attention and recognition in the past, and you can rest assured to use it. The installation commands are as follows:

Pipx install pipenv uses black and isort for code formatting

Black [4] can format our code:

Black is an uncompromising library for formatting Python code. By using it, you will give up the details of manually reformatting the code. In return, Black can bring speed, certainty, and avoid the hassle of adjusting the style of Python code, thus having more energy and time to focus on more important things.

No matter what project you are reading, the code formatted in black looks the same. Format is no longer an issue after a while, so you can focus more on the content.

Black makes code review faster by reducing code differences.

And isort [5] sorts our imports parts:

Isort sorts the parts of your imported Python package (imports), so you no longer have to manually sort the imports. It can sort the import alphabetically and automatically split it into multiple parts.

Install it using pipenv so that they do not confuse the deployment (you can specify to install only in the development environment):

Pipenv install black isort-dev

Black and isort are not compatible with the default options, so we will make isort follow the principles of black. Create a setup.cfg file and add the following configuration:

[isort] multi_line_output=3 include_trailing_comma=True force_grid_wrap=0 use_parentheses=True line_length=88

We can run these tools with the following command:

Pipenv run black pipenv run isort uses flake8 to guarantee code style

Flake8 ensures that the code conforms to the standard Python code specification defined in PEP8. Install using pipenv:

Pipenv install flake8-dev

Like isort, it requires some configuration to work well with black. Add these configurations to setup.cfg:

[flake8] ignore = E203, E266, E501, W503 max-line-length = 88 max-complexity = 18 select = BJI C Jing E Jing FJI WJI T4

Now we can run flake8 with the command: pipenv run flake8.

Static type checking using mypy

Mypy [6] is Python's non-mandatory static type checker designed to combine the advantages of dynamic (or "duck") types and static types. Mypy combines the expressiveness and convenience of Python with the compile-time type checking of a powerful type system, running them with any Python VM with virtually no runtime overhead.

Using types in Python takes a little time to get used to, but the benefits are enormous. As follows:

Static types can make programs easier to understand and maintain

Static typing can help you find errors earlier and reduce testing and debugging time

Static types can help you find errors that are difficult to find before your code goes into production

Pipenv install mypy-dev

By default, Mypy recursively checks the type comments of all imported packages and reports an error when the library does not contain these comments. We need to configure mypy to run only on our code and ignore import errors without type comments. Let's assume that our code is in the best_practices package configured below. Add this to setup.cfg:

[mypy] files=best_practices,test ignore_missing_imports=true

Now we can run mypy:

Pipenv run mypy

This is a useful cheat sheet [7].

Testing with pytest and pytest-cov

Writing tests using pytest [8] is very easy, and eliminating the resistance to writing tests means that you can write more tests quickly!

Pipenv install pytest pytest-cov-dev

This is a simple example on the pytest website:

# content of test_sample.py def inc (x): return x + 1 def test_answer (): assert inc (3) = = 5

To execute it:

$pipenv run pytest = = test session starts = = platform linux-- Python 3.x.y, pytest-5.x.y, py-1.x.y Pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_sample.py F = = FAILURES = = _ _ test_answer _ _ def test_answer (): > assert inc (3) = = 5 E assert 4 = = 5 E + where 4 = inc (3) test_sample.py:6: AssertionError = 1 failed in 0.12 seconds =

All of our test code is in the test directory, so please add this directory to setup.cfg:

[tool:pytest] testpaths=test

If you also want to view test coverage. Create a new file, .coveragerc, specifying that only coverage statistics for our project code are returned. For example, the best_practices project of the example is set as follows:

[run] source = best_practices [report] exclude_lines = # Have to re-enable the standard pragma pragma: no cover # Don't complain about missing debug-only code: def _ repr__ if self\ .debug # Don't complain if tests don't hit defensive assertion code: raise AssertionError raise NotImplementedError # Don't complain if non-runnable code isn't run: if 0: if _ name__ = =. _ main__.:

Now we can run the test and view the coverage.

Pipenv run pytest-- cov--cov-fail-under=100

-- cov-fail-under=100 sets the test coverage of the project if it is less than 100%, it will be considered a failure.

Git hooks of pre-commit

Git hooks allows you to run scripts whenever you want to submit or push. This enables us to run all tests and tests automatically each time we submit / push. Pre-commit [9] makes it easy to configure these hooks.

Git hook scripts are useful for identifying simple problems before submitting code reviews. We will run hooks on each commit to automatically point out problems in the code, such as missing semicolons, trailing whitespace, and debug statements. By pointing out these issues before code review, code reviewers can focus on changed code content without wasting time dealing with these trivial styling issues.

Here, we configure all of the above tools to execute when committing Python code changes (git commit), and then run pytest coverage only on push (because the test is in the last step). Create a new file .pre-commit-config.yaml with the following configuration:

Repos:-repo: local hooks:-id: isort name: isort stages: [commit] language: system entry: pipenv run isort types: [python]-id: black name: black stages: [commit] language: system entry: pipenv run black types: [python]-id : flake8 name: flake8 stages: [commit] language: system entry: pipenv run flake8 types: [python] exclude: setup.py-id: mypy name: mypy stages: [commit] language: system entry: pipenv run mypy types: [python] pass_filenames: false-id : pytest name: pytest stages: [commit] language: system entry: pipenv run pytest types: [python]-id: pytest-cov name: pytest stages: [push] language: system entry: pipenv run pytest--cov--cov-fail-under=100 types: [python] pass_filenames: false

If you need to skip these hooks, you can run git commit-no-verify or git push-no-verify

Build a project using cookiecutter

Now that we know what is included in the ideal project, we can convert it to a template [10] so that we can generate a new project with these libraries and configurations using a single command:

Pipx run cookiecutter gh:sourcery-ai/python-best-practices-cookiecutter

Fill in the project name and warehouse name, and a new project will be generated for you.

To complete the setup, perform the following steps:

# Enter project directory cd # Initialise git repo git init # Install dependencies pipenv install-- dev # Setup pre-commit and pre-push hooks pipenv run pre-commit install-t pre-commit pipenv run pre-commit install-t pre-push

The template project contains a very simple Python file and tests, and you can try these tools. After writing the code, you can execute the git commit for the first time, and all hooks will run.

Integrate into the editor

Although it is exciting to know that the code of the project is always at the highest level at the time of submission. However, it will be very uncomfortable to find a problem after all the changes have been made to the code (when it is submitted). So it's much better to expose problems in real time.

When saving the file, take some time to make sure that the code editor runs these commands. There is timely feedback, which means that you can quickly solve any minor problems introduced while the code is still impressed.

I personally use some excellent Vim plug-ins to accomplish this task:

Ale [11] run flake8 in real time and run black, isort, and mypy when saving the file

[12] vim-test [13] integrated with projectionist runs pytest on file preservation

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report