|
| 1 | +# Python Discord Code Jam Repository Template |
| 2 | + |
| 3 | +## A Primer |
| 4 | +Hello code jam participants! We've put together this repository template for you to use in [our code jams](https://pythondiscord.com/events/) or even other Python events! |
| 5 | + |
| 6 | +This document will contain the following information: |
| 7 | +1. [What does this template contain?](#what-does-this-template-contain) |
| 8 | +2. [How do I use it?](#how-do-i-use-it) |
| 9 | +3. [How do I adapt it to my project?](#how-do-i-adapt-it-to-my-project) |
| 10 | + |
| 11 | +You can also look at [our style guide](https://pythondiscord.com/events/code-jams/code-style-guide/) to get more information about what we consider a maintainable code style. |
| 12 | + |
| 13 | +## What does this template contain? |
| 14 | + |
| 15 | +Here is a quick rundown of what each file in this repository contains: |
| 16 | +- `LICENSE`: [The MIT License](https://opensource.org/licenses/MIT), an OSS approved license which grants rights to everyone to use and modify your projects and limits your liability. We highly recommend you to read the license. |
| 17 | +- `.gitignore`: A list of files that will be ignored by Git. Most of them are auto-generated or contain data that you wouldn't want to share publicly. |
| 18 | +- `dev-requirements.txt`: Every PyPI packages used for the project's development, to ensure a common and maintainable code style. [More on that below](#using-the-default-pip-setup). |
| 19 | +- `tox.ini`: The configurations of two of our style tools: [`flake8`](https://pypi.org/project/flake8/) and [`isort`](https://pypi.org/project/isort/). |
| 20 | +- `.pre-commit-config.yaml`: The configuration of the [`pre-commit`](https://pypi.org/project/pre-commit/) tool. |
| 21 | +- `.github/workflows/lint.yaml`: A [GitHub Actions](https://github.com/features/actions) workflow, a set of actions run by GitHub on their server after each push, to ensure the style requirements are met. |
| 22 | + |
| 23 | +Each of these files have comments for you to understand easily, and modify to fit your needs. |
| 24 | + |
| 25 | +### flake8: general style rules |
| 26 | + |
| 27 | +Our first and probably most important tool is flake8. It will run a set of plugins on your codebase and warn you about any non-conforming lines. |
| 28 | +Here is a sample output: |
| 29 | +``` |
| 30 | +~> flake8 |
| 31 | +./app.py:1:1: D100 Missing docstring in public module |
| 32 | +./app.py:1:6: N802 function name 'helloWorld' should be lowercase |
| 33 | +./app.py:1:16: E201 whitespace after '(' |
| 34 | +./app.py:1:17: ANN001 Missing type annotation for function argument 'name' |
| 35 | +./app.py:1:23: ANN201 Missing return type annotation for public function |
| 36 | +./app.py:2:1: D400 First line should end with a period |
| 37 | +./app.py:2:1: D403 First word of the first line should be properly capitalized |
| 38 | +./app.py:3:19: E225 missing whitespace around operator |
| 39 | +``` |
| 40 | + |
| 41 | +Each line corresponds to an error. The first part is the file path, then the line number, and the column index. |
| 42 | +Then comes the error code, a unique identifier of the error, and then a human-readable message. |
| 43 | + |
| 44 | +If, for any reason, you do not wish to comply with this specific error on a specific line, you can add `# noqa: CODE` at the end of the line. |
| 45 | +For example: |
| 46 | +```python |
| 47 | +def helloWorld(): # noqa: N802 |
| 48 | + ... |
| 49 | +``` |
| 50 | +will pass linting. Although we do not recommend ignoring errors unless you have a good reason to do so. |
| 51 | + |
| 52 | +It is run by calling `flake8` in the project root. |
| 53 | + |
| 54 | +#### Plugin List: |
| 55 | + |
| 56 | +- `flake8-annotations`: Checks your code for the presence of [type-hints](https://docs.python.org/3/library/typing.html). |
| 57 | +- `flake8-bandit`: Checks for common security breaches. |
| 58 | +- `flake8-docstring`: Checks that you properly documented your code. |
| 59 | +- `flake8-isort`: Makes sure you ran ISort on the project. |
| 60 | + |
| 61 | +### ISort: automatic import sorting |
| 62 | + |
| 63 | +This second tool will sort your imports according to the [PEP8](https://www.python.org/dev/peps/pep-0008/#imports). That's it! One less thing for you to do! |
| 64 | + |
| 65 | +It is run by calling `isort .` in the project root. Notice the dot at the end, it tells ISort to use the current directory. |
| 66 | + |
| 67 | +### Pre-commit: run linting before committing |
| 68 | + |
| 69 | +This third tool doesn't check your code, but rather makes sure that you actually *do* check it. |
| 70 | + |
| 71 | +It makes use of a feature called [Git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) which allow you to run a piece of code before running `git commit`. |
| 72 | +The good thing about it is that it will cancel your commit if the lint doesn't pass. You won't have to wait for Github Actions to report and have a second fix commit. |
| 73 | + |
| 74 | +It is *installed* by running `pre-commit install` and can be run manually by calling only `pre-commit`. |
| 75 | + |
| 76 | +[Lint before you push!](https://soundcloud.com/lemonsaurusrex/lint-before-you-push) |
| 77 | + |
| 78 | +#### Hooks List: |
| 79 | + |
| 80 | +- `check-toml`: Lints and corrects your TOML files. |
| 81 | +- `check-yaml`: Lints and corrects your YAML files. |
| 82 | +- `end-of-file-fixer`: Makes sure you always have an empty line at the end of your file. |
| 83 | +- `trailing-whitespaces`: Removes whitespaces at the end of each line. |
| 84 | +- `python-check-blanket-noqa`: Forbids you from using noqas on large pieces of code. |
| 85 | +- `isort`: Runs ISort. |
| 86 | +- `flake8`: Runs flake8. |
| 87 | + |
| 88 | +The last two hooks won't ship with their own environment but will rather run shell commands. You will have to modify them if you change your dependency manager. |
| 89 | + |
| 90 | +## How do I use it? |
| 91 | + |
| 92 | +### Creating your Team Repository |
| 93 | + |
| 94 | +One person in the team, preferably the leader, will have to create the repository and add other members as collaborators. |
| 95 | + |
| 96 | +1. In the top right corner of your screen, where **Clone** usually is, you have a **Use this template** button to click. |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +2. Give the repository a name and a description. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +3. Click **Create repository from template**. |
| 105 | + |
| 106 | +4. Click **Settings** in your newly created repository. |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +5. Select **Manage access**. |
| 111 | + |
| 112 | +<!-- Yes, this is inline html. The source image is too vertical to be displayed with 100% width. --> |
| 113 | +<img src="https://docs.github.com/assets/images/help/repository/manage-access-tab.png" style="width: 30%"></img> |
| 114 | + |
| 115 | +6. Click **Invite a collaborator**. |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +7. Insert the names of each of your teammates, and invite them. Once they have accepted the invitation in their email, they will have write access to the repository. |
| 120 | + |
| 121 | +You are now ready to go! Now sit down, relax, and wait for the kickstart! |
| 122 | +Don't forget to swap "Python Discord" in the `LICENSE` file for the name of each of your team members or the name of your team after the start of the jam. |
| 123 | + |
| 124 | +### Using the Default Pip Setup |
| 125 | + |
| 126 | +Our default setup includes a bare requirement file to be used with a [virtual environment](https://docs.python.org/3/library/venv.html). |
| 127 | + |
| 128 | +We recommend this if you never have used any other dependency manager, although if you have, feel free to switch to it. More on that below. |
| 129 | + |
| 130 | +#### Creating the environment |
| 131 | +Create a virtual environment in the folder `.venv`. |
| 132 | +```shell |
| 133 | +$ python -m venv .venv |
| 134 | +``` |
| 135 | + |
| 136 | +#### Enter the environment |
| 137 | +It will change based on your operating system and shell. |
| 138 | +```shell |
| 139 | +# Linux, Bash |
| 140 | +$ source .venv/bin/activate |
| 141 | +# Linux, Fish |
| 142 | +$ source .venv/bin/activate.fish |
| 143 | +# Linux, Csh |
| 144 | +$ source .venv/bin/activate.csh |
| 145 | +# Linux, PowerShell Core |
| 146 | +$ .venv/bin/Activate.ps1 |
| 147 | +# Windows, cmd.exe |
| 148 | +> .venv\Scripts\activate.bat |
| 149 | +# Windows, PowerShell |
| 150 | +> .venv\Scripts\Activate.ps1 |
| 151 | +``` |
| 152 | + |
| 153 | +#### Installing the Dependencies |
| 154 | +Once the environment is created and activated, use this command to install the development dependencies. |
| 155 | +```shell |
| 156 | +$ pip install -r dev-requirements.txt |
| 157 | +``` |
| 158 | + |
| 159 | +#### Exiting the environment |
| 160 | +Interestingly enough, it is the same for every platform |
| 161 | +```shell |
| 162 | +$ deactivate |
| 163 | +``` |
| 164 | + |
| 165 | +Once the environment is activated, all the commands listed previously should work. We highly recommend that you run `pre-commit install` as soon as possible. |
| 166 | + |
| 167 | +## How do I adapt it to my project? |
| 168 | + |
| 169 | +If you wish to use Pipenv or Poetry, you will have to move the dependencies in `dev-requirements.txt` to the development dependencies of your tool. |
| 170 | + |
| 171 | +Additionally, you will have to open `.pre-commit-config.yaml` and `.github/workflows/lint.yaml` and uncomment the right section. |
| 172 | + |
| 173 | +When installing new dependencies, don't forget to [pin them](https://pip.pypa.io/en/stable/user_guide/#pinned-version-numbers) by adding a version tag at the end. |
| 174 | +For example, if I wish to install `Click`, a quick look at [PyPI](https://pypi.org/project/click/) tells me that 8.0.1 is the latest version. |
| 175 | +I will then add `click ~= 8.0`, without the last number, to my dependency manager. |
| 176 | + |
| 177 | +A code jam project is left unmaintained after the end of the event. If the dependencies aren't pinned, the project will break after the first major change in an API. |
| 178 | + |
| 179 | +## Final words |
| 180 | + |
| 181 | +Don't forget to replace this README with an actual description of your project! Images are also welcome! |
| 182 | + |
| 183 | +We hope this template will be helpful. Good luck in the jam! |
0 commit comments