pre-commit in git hooks, regex beginnings

diary of a codelovingyogi
2 min readOct 12, 2019

--

Recently I evaluated Python auto-formatters and decided to use Black, with pre-commit package to enforce Black formatting while using flake8 to validate PEP8 compliance.

You can do this by configuring the .pre-commit-config.yaml file to this:

repos:-   repo: https://github.com/psf/black    rev: stable    hooks:    - id: black      language_version: python3.6-   repo: https://github.com/pre-commit/pre-commit-hooks    rev: v1.2.3    hooks:    - id: flake8

The only thing is, if you use these two in combination, you should also customize Black so that the line-length used in Black is also 79 to match flake8 validation. You can do this by configuring black using the pyproject.toml file. Otherwise, Black may keep formatting your code to a line-length greater than 79 (default is 88) and you will keep failing flake8 validation during commit.

One of the other things I tried working with again is regex in Python. Pretty simple — I wanted to validate that certain text shows up in a string during some unit tests. Because the string is html and in order to comply with PEP8 I have to shorten my strings and asserting specific html strings became too complicated — so I decided to use regex to validate that a certain value exists instead.

To do this, I used something like this:

import rematch = re.search("<td> hi </td>", html)print(match.group())

With match.group() being the value I would find in my search.

When you use re.search(), a match object gets returned. It looks something like:

<re.Match object; span=(109, 122), match='<td> hi </td>'>

To get the match attribute I used the .group() method.

--

--

diary of a codelovingyogi
diary of a codelovingyogi

Written by diary of a codelovingyogi

quick notes, snippets, bugs, and fixes

No responses yet