Add pre-commit hook section to chapter 3

create pre-commit hooks
This commit is contained in:
Tom Hodson 2022-06-09 10:20:28 +02:00
parent 9eb933cb14
commit 1f8dbc0c89
2 changed files with 71 additions and 1 deletions

15
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,15 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- id: black-jupyter

View File

@ -153,10 +153,65 @@
"You tell Hypothesis how to generate the test data, in this case we use some numpy specifc code to generate 2 dimensional arrays with `dtype = int` and entries randomly sampled from `[1, -1]`. We use the same trick as before of checking two implementations against one another."
]
},
{
"cell_type": "markdown",
"id": "91fa6842-f214-4fd3-bbe5-0f20d7d0d2cc",
"metadata": {},
"source": [
"## Autoformaters\n",
"\n",
"While we're doing things that will help keep our code clean and tidy in the future, I would recommend installing a code formatter like `black`. This is a program that enforces a particular formatting style on your code by simply doing it for you. At first this sounds a bit weird, but it has a few benefits:\n",
"\n",
"- It makes git diffs as small as possible because formatting changes never show up\n",
"- It means you never have to discuss with your collaborators about code formatting, something which can waste a lot of time!\n",
"\n",
"Here I will show you how to setup `black` as a pre-commit hook, this means it runs before you commit anything to git, which is probably the best time to do it. We'll use a helper tool called [pre-commit](https://pre-commit.com/).\n",
"\n",
"```bash\n",
"pip install pre-commit\n",
"pre-commit sample-config >> .pre-commit-config.yaml # Generate an initial config\n",
"```\n",
"Now we add some additional lines to the `.pre-commit-config.yaml` config file to setup black:\n",
"```yaml\n",
"- repo: https://github.com/psf/black\n",
" rev: 21.12b0\n",
" hooks:\n",
" - id: black\n",
" - id: black-jupyter\n",
"```\n",
"And finally `pre-commit install` will make this run every time you commit to git. It's worth running it manually once the first time to check it works: `pre-commit run --all-files`. Running this I immediatly got a cryptic error that, on googling, turned out to be that something broke in version 21.12b0 of `21.12b0`. Running `precommit autoupdate` fixed this for me by updated `black` to a later version. Running `pre-commit run --all-files` a second time now gives me:\n",
"```bash\n",
"(recode) tom@TomsLaptop ReCoDE_MCMCFF % pre-commit run --all-files\n",
"trim trailing whitespace.................................................Passed\n",
"fix end of files.........................................................Passed\n",
"check yaml...........................................(no files to check)Skipped\n",
"check for added large files..............................................Passed\n",
"black....................................................................Passed\n",
"(recode) tom@TomsLaptop ReCoDE_MCMCFF % \n",
"```\n",
"\n",
"Now whenever you commit anything, `black` will autoformat it before it actually gets commited. I'll test this for myself by putting\n",
"```python\n",
"def ugly_litte_one_liner(a,b,c): return \" \".join([a,b,c/2. +3])\n",
"```\n",
"in a code cell below and we'll see how `black` formats it. The only gotcha here is that you have to reload jupyter notebooks from disk in order to see the changes that `black` makes."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "24874a21-b9b3-4c38-9a79-1a2b4bce88a9",
"metadata": {},
"outputs": [],
"source": [
"def ugly_litte_one_liner(a, b, c):\n",
" return \" \".join([a, b, c / 2.0 + 3])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21270ceb-f5b7-496b-a530-2def9f70b89f",
"id": "2f53a760-c1b4-43bf-8774-54d2d9edc230",
"metadata": {},
"outputs": [],
"source": []