Skip to content

Commit 6563dbb

Browse files
jb3Akarys42ChrisLoveringmbaruhfisher60
committed
Add Code Jam template.
This project contains documentation and recommended tooling configuration for projects to be submitted during a Python Discord Code Jam. Co-authored-by: Akarys42 <matteobertucci2004@gmail.com> Co-authored-by: ChrisLovering <chris.lovering.95@gmail.com> Co-authored-by: mbaruh <8bee278@gmail.com> Co-authored-by: fisher60 <39353605+fisher60@users.noreply.github.com>
0 parents  commit 6563dbb

File tree

7 files changed

+380
-0
lines changed

7 files changed

+380
-0
lines changed

.github/workflows/lint.yaml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# GitHub Action Workflow enforcing our code style.
2+
3+
name: Lint, Build & Deploy
4+
5+
# Trigger the workflow on both push (to the main repository)
6+
# and pull requests (against the main repository, but from any repo).
7+
on: [push, pull_request]
8+
# Brand new concurrency setting! This ensures that not more than one run can be triggered for the same commit.
9+
# It is useful for pull requests coming from the main repository since both triggers will match.
10+
concurrency: lint-${{ github.sha }}
11+
12+
jobs:
13+
lint:
14+
runs-on: ubuntu-latest
15+
16+
env:
17+
# Set an environment variable to select pip's cache directory for us to actually cache between runs.
18+
PIP_CACHE_DIR: /tmp/pip-cache-dir
19+
# The Python version your project uses. Feel free to change this if required.
20+
PYTHON_VERSION: 3.9
21+
22+
steps:
23+
# Checks out the repository in the current folder.
24+
- name: Checks out repository
25+
uses: actions/checkout@v2
26+
27+
# Set up the right version of Python
28+
- name: Set up Python ${{ env.PYTHON_VERSION }}
29+
id: python
30+
uses: actions/setup-python@v2
31+
with:
32+
python-version: ${{ env.PYTHON_VERSION }}
33+
34+
# This step caches our Python dependencies. To make sure we
35+
# only restore a cache when the dependencies, the python version and
36+
# the runner operating system we create a cache key
37+
# that is a composite of those states.
38+
# Only when the context is exactly the same, we will restore the cache.
39+
- name: Restore pip cache
40+
uses: actions/cache@v2
41+
with:
42+
path: ${{ env.PIP_CACHE_DIR }}
43+
key: "python-pip-${{ runner.os }}-\
44+
${{ steps.python.outputs.python-version }}-\
45+
${{ hashFiles('./Pipfile', './Pipfile.lock') }}"
46+
47+
# Uncomment the step that matches your setup.
48+
49+
# Default Env setup
50+
- name: Run hooks through our requirement file
51+
run: |
52+
pip install -r dev-requirements.txt
53+
pre-commit run --all-files
54+
env:
55+
# Force pre-commit to do a system install.
56+
PIP_USER: 0
57+
58+
# Pipenv setup
59+
# - name: Run hooks through our Pipenv environment.
60+
# run: |
61+
# pipenv install --system --dev
62+
# pipenv run python -m pre-commit run --all-files
63+
# env:
64+
# # Force pre-commit to do a system install.
65+
# PIP_USER: 0
66+
67+
# Poetry setup
68+
# - name: Run hooks through our Poetry environment.
69+
# run: |
70+
# poetry install
71+
# poetry run python -m pre-commit run --all-files
72+
# env:
73+
# # Force pre-commit to do a system install.
74+
# PIP_USER: 0

.gitignore

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Files generated by the interpreter
2+
__pycache__/
3+
*.py[cod]
4+
5+
# Environment specific
6+
.venv
7+
venv
8+
.env
9+
env
10+
11+
# Unittest reports
12+
.coverage*
13+
14+
# Logs
15+
*.log
16+
17+
# PyEnv version selector
18+
.python-version
19+
20+
# Built objects
21+
*.so
22+
dist/
23+
build/
24+
25+
# IDEs
26+
# PyCharm
27+
.idea/
28+
# VSCode
29+
.vscode/
30+
# MacOS
31+
.DS_Store

.pre-commit-config.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Pre-commit setup
2+
# See https://github.com/python-discord/code-jam-template/tree/main#pre-commit-run-linting-before-committing
3+
#
4+
# Please uncomment the right lines depending on your setup.
5+
6+
repos:
7+
- repo: https://github.com/pre-commit/pre-commit-hooks
8+
rev: v2.5.0
9+
hooks:
10+
- id: check-toml
11+
- id: check-yaml
12+
- id: end-of-file-fixer
13+
- id: trailing-whitespace
14+
args: [--markdown-linebreak-ext=md]
15+
- repo: https://github.com/pre-commit/pygrep-hooks
16+
rev: v1.5.1
17+
hooks:
18+
- id: python-check-blanket-noqa
19+
- repo: local
20+
hooks:
21+
- id: isort
22+
name: ISort
23+
entry: python -m isort . # Default Env setup
24+
# entry: pipenv run python -m isort . # Pipenv setup
25+
# entry: poetry run python -m isort . # Poetry setup
26+
language: system
27+
types: [python]
28+
require_serial: true
29+
- id: flake8
30+
name: Flake8
31+
entry: python -m flake8 # Default Env setup
32+
# entry: pipenv run python -m flake8 # Pipenv setup
33+
# entry: poetry run python -m flake8 # Poetry setup
34+
language: system
35+
types: [python]
36+
require_serial: true

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2021 Python Discord
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
![](https://docs.github.com/assets/images/help/repository/use-this-template-button.png)
99+
100+
2. Give the repository a name and a description.
101+
102+
![](https://docs.github.com/assets/images/help/repository/create-repository-name.png)
103+
104+
3. Click **Create repository from template**.
105+
106+
4. Click **Settings** in your newly created repository.
107+
108+
![](https://docs.github.com/assets/images/help/repository/repo-actions-settings.png)
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+
![](https://docs.github.com/assets/images/help/repository/invite-a-collaborator-button.png)
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!

dev-requirements.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file contains all the development requirements for our linting toolchain.
2+
# Don't forget to pin your dependencies!
3+
# This list will have to be migrated if you wish to use another dependency manager.
4+
5+
# Base tools
6+
flake8~=3.7
7+
isort~=5.9
8+
pre-commit~=2.13.0
9+
10+
# Flake8 plugins, see https://github.com/python-discord/code-jam-template/tree/main#plugin-list
11+
flake8-annotations~=2.0
12+
flake8-bandit~=2.1
13+
flake8-docstrings~=1.5
14+
flake8-isort~=4.0

tox.ini

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Flake8 and ISort configuration
2+
3+
[flake8]
4+
# Increase the line length. This breaks PEP8 but it is way easier to work with.
5+
# The original reason for this limit was a standard vim terminal is only 79 characters,
6+
# but this doesn't really apply anymore.
7+
max-line-length=119
8+
# Don't lint the venv or the CPython cache.
9+
exclude=.venv,__pycache__
10+
# Ignore some of the most obnoxious linting errors.
11+
ignore=
12+
B311,W503,E226,S311,T000
13+
# Missing Docstrings
14+
D100,D104,D105,D106,D107,
15+
# Docstring Whitespace
16+
D203,D212,D214,D215,
17+
# Docstring Quotes
18+
D301,D302,
19+
# Docstring Content
20+
D400,D401,D402,D404,D405,D406,D407,D408,D409,D410,D411,D412,D413,D414,D416,D417,
21+
# Comments
22+
E266,
23+
# Type Annotations
24+
ANN002,ANN003,ANN101,ANN102,ANN204,ANN206
25+
26+
[isort]
27+
# Select the 5th style (Hanging grid grouped) to handle longer import.
28+
# This choice is mostly arbitrary and can be changed at your will.
29+
#
30+
# Example of this style:
31+
# from third_party import (
32+
# lib1, lib2, lib3, lib4,
33+
# lib5, ...
34+
# )
35+
multi_line_output=5

0 commit comments

Comments
 (0)