Skip to content

Commit 0bc5c29

Browse files
ucoderylwasser
authored andcommitted
use literalinclude in code-style-linting-format
1 parent af1c7e3 commit 0bc5c29

File tree

6 files changed

+157
-108
lines changed

6 files changed

+157
-108
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
repos:
2+
- repo: https://github.com/PyCQA/isort
3+
rev: 5.11.4
4+
hooks:
5+
- id: isort
6+
files: \.py$
7+
8+
# Misc commit checks using built in pre-commit checks
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v4.4.0
11+
# ref: https://github.com/pre-commit/pre-commit-hooks#hooks-available
12+
hooks:
13+
# Autoformat: Makes sure files end in a newline and only a newline.
14+
- id: end-of-file-fixer
15+
16+
# Lint: Check for files with names that would conflict on a
17+
# case-insensitive filesystem like MacOS HFS+ or Windows FAT.
18+
- id: check-case-conflict
19+
- id: trailing-whitespace
20+
21+
# Linting: Python code (see the file .flake8)
22+
- repo: https://github.com/PyCQA/flake8
23+
rev: "6.0.0"
24+
hooks:
25+
- id: flake8
26+
27+
# Black for auto code formatting
28+
repos:
29+
- repo: https://github.com/psf/black
30+
rev: 22.12.0
31+
hooks:
32+
- id: black
33+
language_version: python3.8
34+
35+
# Tell precommit.ci bot to update codoe format tools listed in the file
36+
# versions every quarter
37+
# The default it so update weekly which is too many new pr's for many
38+
# maintainers (remove these lines if you aren't using the bot!)
39+
ci:
40+
autoupdate_schedule: quarterly

examples/pure-hatch/pyproject.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ docs = [
3636
"sphinx",
3737
"pydata-sphinx-theme"
3838
]
39+
40+
[tool.ruff]
41+
select = [
42+
"E", # pycodestyle errors
43+
"W", # pycodestyle warnings
44+
"F", # pyflakes. "E" + "W" + "F" + "C90" (mccabe complexity) is equivalent to flake8
45+
"I", # isort
46+
]
47+
48+
[tool.ruff.isort]
49+
known-first-party = ["examplePy"]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def celsius_to_fahrenheit(celsius):
2+
"""
3+
Convert temperature from Celsius to Fahrenheit.
4+
5+
Parameters:
6+
celsius (float): Temperature in Celsius.
7+
8+
Returns:
9+
float: Temperature in Fahrenheit.
10+
"""
11+
fahrenheit = (celsius * 9/5) + 32
12+
return fahrenheit
13+
14+
15+
def fahrenheit_to_celsius(fahrenheit):
16+
"""
17+
Convert temperature from Fahrenheit to Celsius.
18+
19+
Parameters:
20+
fahrenheit (float): Temperature in Fahrenheit.
21+
22+
Returns:
23+
float: Temperature in Celsius.
24+
"""
25+
celsius = (fahrenheit - 32) * 5/9
26+
return celsius
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from examplePy.temperature import fahrenheit_to_celsius
2+
import pandas
3+
from typing import Sequence
4+
5+
def calc_annual_mean(df: pandas.DataFrame):
6+
"""Function to calculate the mean temperature for each year and the final mean"""
7+
# TODO: make this a bit more robust so we can write integration test examples??
8+
# Calculate the mean temperature for each year
9+
yearly_means = df.groupby('Year').mean()
10+
11+
# Calculate the final mean temperature across all years
12+
final_mean = yearly_means.mean()
13+
14+
# Return a converted value
15+
return fahrenheit_to_celsius(yearly_means), fahrenheit_to_celsius(final_mean)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Sequence
2+
3+
import pandas
4+
5+
from examplePy.temperature import fahrenheit_to_celsius
6+
7+
def calc_annual_mean(df: pandas.DataFrame):
8+
"""Function to calculate the mean temperature for each year and the final mean"""
9+
# TODO: make this a bit more robust so we can write integration test examples??
10+
# Calculate the mean temperature for each year
11+
yearly_means = df.groupby('Year').mean()
12+
13+
# Calculate the final mean temperature across all years
14+
final_mean = yearly_means.mean()
15+
16+
# Return a converted value
17+
return fahrenheit_to_celsius(yearly_means), fahrenheit_to_celsius(final_mean)

package-structure-code/code-style-linting-format.md

Lines changed: 48 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ a discussion of:
3838
## Use a code format tool (or tools) to make your life easier
3939

4040
We suggest that you use a code format tool, or a set of format tools, because
41-
manually applying all of the PEP 8 format specifications is time consuming
42-
for both maintainers and can be a road block for potential new contributors.
41+
manually applying all of the PEP 8 format specifications is both time consuming
42+
for maintainers and can be a road block for potential new contributors.
4343
Code formatters will automagically reformat your code for you, adhering to
4444
PEP 8 standards and applying consistent style decisions throughout.
4545

@@ -70,29 +70,29 @@ discussed below, is an example of a commonly-used code linter.
7070

7171
### Code Formatters (and stylers)
7272

73-
Python focused code formatters often follow PEP 8 standards. However, they also
74-
make stylistic decisions about code consistency. Code formatters will
75-
reformat your code for you.
73+
Code formatters will reformat your code for you. Python focused code formatters
74+
often follow PEP 8 standards. However, they also make stylistic decisions about
75+
code consistency.
7676

7777
Black is an example of a commonly-used code formatter. Black both applies PEP 8
7878
standards while also making decisions about things like consistent use of double
7979
quotes for strings, and spacing of items in lists.
8080

8181
You will learn more about Black below.
8282

83-
## Code format and style
83+
## Code linting, formatting and styling tools
8484

8585
### Black
8686

8787
[Black](https://black.readthedocs.io/en/stable/) is a code
8888
formatter. Black will automagically (and _unapologetically_)
8989
fix spacing issues and ensure code format is consistent throughout your
90-
package. Black also generally adhere to PEP 8 style guidelines with
90+
package. Black also generally adheres to PEP 8 style guidelines with
9191
some exceptions. A few examples of those exceptions are below:
9292

9393
- Black defaults to a line length of 88 (79 + 10%) rather than the 79 character `PEP 8` specification. However, line length is a setting can be manually overwritten in your Black configuration.
9494
- Black will not adjust line length in your comments or docstrings.
95-
- This tool will not review and fix import order (you need _isort_ or _Ruff_ to do that - see below).
95+
- This tool will not review and fix import order (you need `isort` or `ruff` to do that - see below).
9696

9797
```{tip}
9898
If you are interested in seeing how Black will format your code, you can
@@ -102,7 +102,7 @@ use the [Black playground](https://black.vercel.app/)
102102
Using a code formatter like Black will leave you more time to work on
103103
code function rather than worry about format.
104104

105-
### flake8 for linting code in Python packages
105+
### Flake8
106106

107107
To adhere to Python `pep8` format standards, you might want to add
108108
[flake8](https://flake8.pycqa.org/en/latest/) to your code format
@@ -122,9 +122,8 @@ called `stravalib`.
122122

123123
The line length standard for PEP 8 is 79 characters.
124124

125-
Notice that
126-
flake8 returns a list of issues that it found in the model.py module in the
127-
command line. The Python file itself is not modified. Using on this output,
125+
Notice that flake8 returns a list of issues that it found in the model.py module
126+
on the command line. The Python file itself is not modified. Using this output,
128127
you can fix each issue line by line manually.
129128

130129
```bash
@@ -152,7 +151,7 @@ your package.
152151
> - Related third party imports.
153152
> - Local application/library specific imports.
154153
155-
While **flake8** will identify unused imports in your code, it won't
154+
While `flake8` will identify unused imports in your code, it won't
156155
fix or identify issues with the order of package imports.
157156

158157
`isort` will identify where imports in your code are out of
@@ -164,75 +163,57 @@ up your code.
164163

165164
Code imports before `isort` is run:
166165

167-
Below, the exc module is a part of starvalib which is a
168-
third party package. `abc` and `logging` are core `Python` packages
169-
distributed with `Python`. Also notice that there are extra
170-
spaces in the imports listed below.
171-
172-
```python
173-
from stravalib import exc
174-
import abc
175-
import logging
176-
177-
from collections.abc import Sequence
166+
Below, the `pandas` is a third party package, `typing` is a core `Python`
167+
package distributed with `Python`, and `examplePy.temperature` is a first-party
168+
module which means it belongs to the same package as the file doing the import.
169+
Also notice that there are no spaces in the imports listed below.
178170

171+
:::{literalinclude} ../examples/pure-hatch/src/examplePy/temporal-raw.py
172+
:language: python
173+
:end-before: def calc_annual_mean
174+
:::
179175

180-
from stravalib import unithelper as uh
176+
From the project root, run:
177+
```bash
178+
isort src/examplePy/temporal.py
181179
```
182180

183-
Run:
184-
`isort stravalib/model.py`
181+
Python file `temporal.py` imports after `isort` has been run
185182

186-
Python file model.py imports after `isort` has been run
187-
188-
```python
189-
import abc
190-
import logging
191-
from collections.abc import Sequence
192-
193-
from stravalib import exc
194-
from stravalib import unithelper as uh
195-
```
183+
:::{literalinclude} ../examples/pure-hatch/src/examplePy/temporal.py
184+
:language: python
185+
:end-before: def calc_annual_mean
186+
:::
196187

197188
### Ruff
198189

199-
[Ruff](https://beta.ruff.rs) is a new addition to the code quality ecosystem,
200-
gaining some traction since its release.
201-
`ruff` is a linter for Python, aiming to replace several tools behind a single interface.
202-
As such, `ruff` can be used instead of `flake8` and `isort`.
190+
[Ruff](https://docs.astral.sh/ruff/) is a new addition to the code quality
191+
ecosystem, gaining some traction since its release. `ruff` is both a linter
192+
and a code formatter for Python, aiming to replace several tools behind a single
193+
interface. As such, `ruff` can be used at a replacement of all other tools
194+
mentioned here, or in complement to some of them.
203195

204196
`ruff` has some interesting features that distinguish it from other linters:
205197

206198
- Linter configuration in `pyproject.toml`
207199
- Several hundred rules included, many of which are automatically fixable
208-
- Rules explanation, see [F403](https://beta.ruff.rs/docs/rules/undefined-local-with-import-star/) for an example
200+
- Rules explanation, see [F403](https://docs.astral.sh/ruff/rules/undefined-local-with-import-star/) for an example
209201
- Fast execution time, makes a quick feedback loop possible even on large projects.
210202

211-
Here is a simple configuration to get started with `ruff`:
212-
213-
```toml
214-
# pyproject.toml
215-
216-
[tool.ruff]
217-
select = [
218-
"E", # pycodestyle errors
219-
"W", # pycodestyle warnings
220-
"F", # pyflakes. "E" + "W" + "F" + "C90" (mccabe complexity) is equivalent to flake8
221-
"I", # isort
222-
]
223-
ignore = [
224-
"E501", # line >79, handled by black
225-
]
226-
```
203+
Here is a simple configuration to get started with `ruff`. It would go into your `pyproject.toml`:
227204

228-
Depending on your project, you might want to add the following to sort imports correctly:
205+
:::{literalinclude} ../examples/pure-hatch/pyproject.toml
206+
:language: toml
207+
:start-at: [tool.ruff]
208+
:end-before: [tool.ruff.isort]
209+
:::
229210

230-
```toml
231-
# pyproject.toml
211+
Depending on your project, you might want to add the following to sort imports correctly:
232212

233-
[tool.ruff.isort]
234-
known-first-party = ["<package_folder>"]
235-
```
213+
:::{literalinclude} ../examples/pure-hatch/pyproject.toml
214+
:language: toml
215+
:start-at: [tool.ruff.isort]
216+
:::
236217

237218
## How to use code formatter in your local workflow
238219

@@ -347,50 +328,9 @@ Below is an example **.pre-commit-cofig.yaml** file that can be used to setup
347328
the pre-commit hook and the pre-commit.ci bot if you chose to implement that
348329
too.
349330

350-
```yaml
351-
# file: .pre-commit-config.yaml
352-
353-
repos:
354-
- repo: https://github.com/PyCQA/isort
355-
rev: 5.11.4
356-
hooks:
357-
- id: isort
358-
files: \.py$
359-
360-
# Misc commit checks using built in pre-commit checks
361-
- repo: https://github.com/pre-commit/pre-commit-hooks
362-
rev: v4.4.0
363-
# ref: https://github.com/pre-commit/pre-commit-hooks#hooks-available
364-
hooks:
365-
# Autoformat: Makes sure files end in a newline and only a newline.
366-
- id: end-of-file-fixer
367-
368-
# Lint: Check for files with names that would conflict on a
369-
# case-insensitive filesystem like MacOS HFS+ or Windows FAT.
370-
- id: check-case-conflict
371-
- id: trailing-whitespace
372-
373-
# Linting: Python code (see the file .flake8)
374-
- repo: https://github.com/PyCQA/flake8
375-
rev: "6.0.0"
376-
hooks:
377-
- id: flake8
378-
379-
# Black for auto code formatting
380-
repos:
381-
- repo: https://github.com/psf/black
382-
rev: 22.12.0
383-
hooks:
384-
- id: black
385-
language_version: python3.8
386-
387-
# Tell precommit.ci bot to update codoe format tools listed in the file
388-
# versions every quarter
389-
# The default it so update weekly which is too many new pr's for many
390-
# maintainers (remove these lines if you aren't using the bot!)
391-
ci:
392-
autoupdate_schedule: quarterly
393-
```
331+
:::{literalinclude} ../examples/pure-hatch/pyproject.toml
332+
:language: yaml
333+
:::
394334

395335
This file specifies a hook that will be triggered automatically before each `git commit`,
396336
in this case, it specifies a `flake8` using version `6.0.0`.

0 commit comments

Comments
 (0)