Skip to content

Commit b5a39b9

Browse files
authored
Merge pull request #149 from GriceTurrble/chore/new-dighs
chore: ruff implementation, pre-commit updates
2 parents 5a91dec + 89e7425 commit b5a39b9

File tree

8 files changed

+124
-21
lines changed

8 files changed

+124
-21
lines changed

.pre-commit-config.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ repos:
2727
hooks:
2828
- id: pycln
2929
- repo: https://github.com/asottile/pyupgrade
30-
rev: "v3.17.0"
30+
rev: "v3.19.0"
3131
hooks:
3232
- id: pyupgrade
3333
args: [--py39-plus]
@@ -36,14 +36,15 @@ repos:
3636
hooks:
3737
- id: add-trailing-comma
3838
- repo: https://github.com/astral-sh/ruff-pre-commit
39-
rev: "v0.6.9"
39+
rev: "v0.8.1"
4040
hooks:
4141
- id: ruff
4242
args: [--fix]
4343
types_or: [python, pyi, jupyter]
4444
- id: ruff-format
4545
types_or: [python, pyi, jupyter]
46-
- repo: https://github.com/pre-commit/mirrors-prettier
47-
rev: "v4.0.0-alpha.8"
46+
- repo: https://github.com/rbubley/mirrors-prettier
47+
rev: "v3.4.1"
4848
hooks:
4949
- id: prettier
50+
types_or: [markdown, json, yaml, html]

2024/python/ruff.toml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Configuration: https://docs.astral.sh/ruff/configuration/
2+
# Settings: https://docs.astral.sh/ruff/settings/
3+
# Rules definitions: https://docs.astral.sh/ruff/rules/
4+
5+
# Match our Black formatting
6+
target-version = "py313"
7+
8+
[lint]
9+
select = [
10+
"B", # flake8-bugbear
11+
"E", # pycodestyle errors
12+
"F", # pyflakes
13+
"FLY", # flynt
14+
"I", # isort
15+
"N", # pep8-naming
16+
"NPY", # numpy-specific
17+
"PERF", # performance
18+
"PL", # Pylint
19+
"S", # flake8-bandit (security checks)
20+
"UP", # pyupgrade (pre-commit hook for pyupgrade should fix most)
21+
"W", # pycodestyle warnings
22+
]
23+
# TODO consider SLF (SLF001, private-member-access)
24+
# TODO consider TRY ruleset
25+
# mishandling exceptions, like raising broad `Exception` or including a `return` inside `try` blocks.
26+
27+
# Ignore certain rules across the entire repo
28+
# (after selecting a set of rules like 'E', ignore subsets of those rules here)
29+
ignore = [
30+
# Selected ignore rules
31+
"E203", # whitespace-before-punctuation
32+
"F401", # unused-import (pycln will remove these)
33+
"F811", # redefined-while-unused
34+
"PLR0913", # too-many-arguments
35+
"S101", # assert (usage of the assert statement)
36+
"S113", # request-without-timeout
37+
"S602", # subprocess-popen-with-shell-equals-true
38+
"S603", # subprocess-without-shell-equals-true
39+
"S607", # start-process-with-partial-path
40+
"S608", # hardcoded-sql-expression
41+
# Rules recommended to avoid when using Ruff formatter
42+
"COM812", # missing-trailing-comma
43+
"COM819", # prohibited-trailing-comma
44+
"D206", # indent-with-spaces
45+
"D300", # triple-single-quotes
46+
"E111", # indentation-with-invalid-multiple
47+
"E114", # indentation-with-invalid-multiple-comment
48+
"E117", # over-indented
49+
"ISC001", # single-line-implicit-string-concatenation
50+
"ISC002", # multi-line-implicit-string-concatenation
51+
"Q000", # bad-quotes-inline-string
52+
"Q001", # bad-quotes-multiline-string
53+
"Q002", # bad-quotes-docstring
54+
"Q003", # avoidable-escaped-quote
55+
"W191", # tab-indentation
56+
]
57+
58+
# Attempt to auto-fix if running `ruff check . --fix`
59+
fixable = [
60+
"I", # isort
61+
"UP", # pyupgrade
62+
]
63+
64+
# Avoid fixing these when using `--fix`.
65+
unfixable = [
66+
"B", # flake8-bugbear
67+
]
68+
69+
# Skip checking any files matching glob patterns:
70+
# exclude = ["**/test/**/*.*"]
71+
72+
# Exclude rules from being applied to files matching glob patterns:
73+
[lint.per-file-ignores]
74+
"**/*test.py" = [
75+
"F811", # redefined-while-unused
76+
"N", # pep8-naming
77+
"PLR2004", # magic-value-comparison
78+
]

2024/python/src/grice_py_aoc_2024/day01/main.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

3+
import time
4+
import typing
35
from collections import defaultdict
46
from pathlib import Path
5-
import bisect
6-
import typing
77

88
if typing.TYPE_CHECKING:
99
from io import TextIOWrapper
@@ -17,10 +17,12 @@ def part1(inputs: TextIOWrapper) -> int:
1717
rights: list[int] = []
1818
for line in inputs:
1919
left, right = line.strip().split()
20-
bisect.insort(lefts, int(left))
21-
bisect.insort(rights, int(right))
20+
lefts.append(int(left))
21+
rights.append(int(right))
22+
lefts.sort()
23+
rights.sort()
2224

23-
for lnum, rnum in zip(lefts, rights):
25+
for lnum, rnum in zip(lefts, rights, strict=False):
2426
total += abs(rnum - lnum)
2527
return total
2628

@@ -41,11 +43,17 @@ def part2(inputs: TextIOWrapper) -> int:
4143

4244

4345
def main() -> None:
46+
_start1 = time.perf_counter()
4447
with open(FILE) as f:
45-
print(f"PART 1: {part1(f)}")
48+
result1 = part1(f)
49+
_delta1 = time.perf_counter() - _start1
50+
print(f">> Part 1: {result1} ({_delta1:.6f}s)")
4651

52+
_start2 = time.perf_counter()
4753
with open(FILE) as f:
48-
print(f"PART 2: {part2(f)}")
54+
result2 = part2(f)
55+
_delta2 = time.perf_counter() - _start2
56+
print(f">> Part 2: {result2} ({_delta2:.6f}s)")
4957

5058

5159
if __name__ == "__main__":

2024/python/src/grice_py_aoc_2024/day01/test_day01.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
from .main import part1, part2
66

77
TEST_FILE = Path(__file__).parent / "test_inputs.txt"
8+
RESULT_PART_1 = 11
9+
RESULT_PART_2 = 31
810

911

1012
def test_part1():
1113
with open(TEST_FILE) as f:
1214
result = part1(f)
13-
assert result == 11
15+
assert result == RESULT_PART_1
1416

1517

1618
def test_part2():
1719
with open(TEST_FILE) as f:
1820
result = part2(f)
19-
assert result == 31
21+
assert result == RESULT_PART_2

2024/python/src/grice_py_aoc_2024/day02/main.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
3+
import time
44
import typing
5+
from pathlib import Path
56

67
if typing.TYPE_CHECKING:
78
from io import TextIOWrapper
@@ -18,11 +19,17 @@ def part2(inputs: TextIOWrapper):
1819

1920

2021
def main():
22+
_start1 = time.perf_counter()
2123
with open(FILE) as f:
22-
print(f"PART 1: {part1(f)}")
24+
result1 = part1(f)
25+
_delta1 = time.perf_counter() - _start1
26+
print(f">> Part 1: {result1} ({_delta1:.6f}s)")
2327

28+
_start2 = time.perf_counter()
2429
with open(FILE) as f:
25-
print(f"PART 2: {part2(f)}")
30+
result2 = part2(f)
31+
_delta2 = time.perf_counter() - _start2
32+
print(f">> Part 2: {result2} ({_delta2:.6f}s)")
2633

2734

2835
if __name__ == "__main__":

2024/python/src/grice_py_aoc_2024/day02/test_day02.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from .main import part1, part2
66

77
TEST_FILE = Path(__file__).parent / "test_inputs.txt"
8-
98
RESULT_PART_1 = 123
109
RESULT_PART_2 = 456
1110

11+
1212
def test_part1():
1313
with open(TEST_FILE) as f:
1414
result = part1(f)

2024/python/src/grice_py_aoc_2024/main.py-tpl

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
3+
import time
44
import typing
5+
from pathlib import Path
56

67
if typing.TYPE_CHECKING:
78
from io import TextIOWrapper
@@ -18,11 +19,17 @@ def part2(inputs: TextIOWrapper):
1819

1920

2021
def main():
22+
_start1 = time.perf_counter()
2123
with open(FILE) as f:
22-
print(f"PART 1: {part1(f)}")
24+
result1 = part1(f)
25+
_delta1 = time.perf_counter() - _start1
26+
print(f">> Part 1: {result1} ({_delta1:.6f}s)")
2327

28+
_start2 = time.perf_counter()
2429
with open(FILE) as f:
25-
print(f"PART 2: {part2(f)}")
30+
result2 = part2(f)
31+
_delta2 = time.perf_counter() - _start2
32+
print(f">> Part 2: {result2} ({_delta2:.6f}s)")
2633

2734

2835
if __name__ == "__main__":

2024/python/src/grice_py_aoc_2024/test_day.py-tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ from pathlib import Path
55
from .main import part1, part2
66

77
TEST_FILE = Path(__file__).parent / "test_inputs.txt"
8-
98
RESULT_PART_1 = 123
109
RESULT_PART_2 = 456
1110

11+
1212
def test_part1():
1313
with open(TEST_FILE) as f:
1414
result = part1(f)

0 commit comments

Comments
 (0)