|
| 1 | +_model: page |
| 2 | +--- |
| 3 | +_template: page-with-toc.html |
| 4 | +--- |
| 5 | +title: Python Guidelines |
| 6 | +--- |
| 7 | +description: These Python Guidelines (style guide) empower the community to |
| 8 | +focus on the intent of the code and work together with a minimum of friction. |
| 9 | +--- |
| 10 | +body: |
| 11 | + |
| 12 | + |
| 13 | +## Code Formatting |
| 14 | + |
| 15 | +### Overview |
| 16 | + |
| 17 | +All Python code must be formatted using [Black][black], *the uncompromising |
| 18 | +Python code formatter*. Each project should contain a `pyproject.toml` |
| 19 | +configuration file that limits line length to 79 characters. |
| 20 | + |
| 21 | +[black]: https://github.com/psf/black |
| 22 | + |
| 23 | +### Examples |
| 24 | + |
| 25 | +- Black configuration (`pyproject.toml`): [GitHub Search · org:creativecommons |
| 26 | + filename:pyproject.toml][blackconfig] |
| 27 | +- GitHub Actions workflow: [GitHub Search · org:creativecommons "pipenv run |
| 28 | + black"][blackactions] |
| 29 | + |
| 30 | +[blackconfig]: https://github.com/search?q=org%3Acreativecommons+filename:pyproject.toml |
| 31 | +[blackactions]: https://github.com/search?q=org%3Acreativecommons+%22pipenv+run+black%22&type=code |
| 32 | + |
| 33 | + |
| 34 | +## Style and Quality |
| 35 | + |
| 36 | +### Overview |
| 37 | + |
| 38 | +All Python code must be checked using [flake8][flake8], *a python tool that |
| 39 | +glues together pep8, pyflakes, mccabe, and third-party plugins to check |
| 40 | +the style and quality of some python code.* |
| 41 | + |
| 42 | +[flake8]: https://gitlab.com/pycqa/flake8 |
| 43 | + |
| 44 | +### Conflicts with Black |
| 45 | + |
| 46 | +Where flake8 conflicts with Black, the flake8 check should be skipped. For |
| 47 | +example, there are instances where black formats code that generates flake8 |
| 48 | +warning for [Whitespace before ':' (E203)][e203]. |
| 49 | + |
| 50 | +[e203]: https://www.flake8rules.com/rules/E203.html |
| 51 | + |
| 52 | + |
| 53 | +### Examples |
| 54 | + |
| 55 | +- flake8 configuration (`.flake8`): [GitHub Search · org:creativecommons |
| 56 | + filename:.flake8][flake8conf] |
| 57 | +- flake8 skip E203 warning: [GitHub Search · org:creativecommons "noqa: |
| 58 | + E203"][skipe203] |
| 59 | +- GitHub Actions workflow: [GitHub Search · org:creativecommons "pipenv run |
| 60 | + flake8"][flake8actions] |
| 61 | + |
| 62 | +[flake8conf]: https://github.com/search?q=org%3Acreativecommons+filename:.flake8 |
| 63 | +[skipe203]: https://github.com/search?q=org%3Acreativecommons+%22noqa%3A+E203%22&type=code |
| 64 | +[flake8actions]: https://github.com/search?q=org%3Acreativecommons+%22pipenv+run+flake8%22&type=code |
| 65 | + |
| 66 | + |
| 67 | +## Development Environment |
| 68 | + |
| 69 | +### Overview |
| 70 | + |
| 71 | +Please work to minimize the number of development related technologies. This |
| 72 | +helps minimize the overhead of contributing to multiple projects. Most of our |
| 73 | +projects use either [Pipenv][pipenv], the Python Development Workflow for |
| 74 | +Humans, or [docker-compose][dockercompose], a tool for defining and running |
| 75 | +multi-container Docker applications. |
| 76 | + |
| 77 | +[pipenv]: https://github.com/pypa/pipenv |
| 78 | +[dockercompose]: https://docs.docker.com/compose/ |
| 79 | + |
| 80 | + |
| 81 | +### Examples |
| 82 | + |
| 83 | +- Pipenv configuration: [GitHub Search · org:creativecommons |
| 84 | + filename:Pipfile][pipenvconf] |
| 85 | +- Docker-compose example: [docker-compose.yml for cccatalog][dockercomposeconf] |
| 86 | + and the [related README.md][relatedreadme] |
| 87 | + |
| 88 | +[pipenvconf]: https://github.com/search?q=org%3Acreativecommons+filename:Pipfile |
| 89 | +[dockercomposeconf]: https://github.com/creativecommons/cccatalog/blob/master/src/cc_catalog_airflow/docker-compose.yml |
| 90 | +[relatedreadme]: https://github.com/creativecommons/cccatalog#development-setup-for-airflow-and-api-puller-scripts |
| 91 | + |
| 92 | + |
| 93 | +## Additional Guidance |
| 94 | + |
| 95 | +Thankfully, Black removes a lot of style work and worry. Where there is room |
| 96 | +for developer discretion, the following guides are excellent resources: |
| 97 | + |
| 98 | +- [Google Python Style Guide][googlestyle] |
| 99 | +- [Code Style — The Hitchhiker's Guide to Python][hitchhikers] |
| 100 | + |
| 101 | +[googlestyle]: https://google.github.io/styleguide/pyguide.html |
| 102 | +[hitchhikers]: https://docs.python-guide.org/writing/style/ |
0 commit comments