Skip to content

Commit c315453

Browse files
authored
Merge pull request #30 from freelawproject/ruff
chore: Move formatting and linting to Ruff
2 parents 9dd9ca8 + 5f67d42 commit c315453

File tree

7 files changed

+69
-46
lines changed

7 files changed

+69
-46
lines changed

.pre-commit-config.yaml

+6-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ repos:
99
- id: check-yaml
1010
- id: check-added-large-files
1111

12-
- repo: https://github.com/psf/black
13-
rev: 25.1.0 # Use the latest stable version
14-
hooks:
15-
- id: black
16-
17-
- repo: https://github.com/PyCQA/isort
18-
rev: 6.0.1
19-
hooks:
20-
- id: isort
21-
name: isort (python)
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.11.8
14+
hooks:
15+
- id: ruff
16+
args: [ --fix ]
17+
- id: ruff-format

judge_pics/scrapers/cand_judges.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
token = ""
1515
if token:
16-
headers = {"Authorization": "Token %s" % token}
16+
headers = {"Authorization": f"Token {token}"}
1717
else:
1818
print(
1919
"Warning: No CourtListener token used. You'll run out of free "
@@ -60,8 +60,7 @@ def make_slug(name: str) -> str | None:
6060

6161
try:
6262
result_json = requests.get(
63-
"https://www.courtlistener.com/api/rest/v4/search/?type=p&name=%s&court=cand"
64-
% name,
63+
f"https://www.courtlistener.com/api/rest/v4/search/?type=p&name={name}&court=cand",
6564
headers=headers,
6665
timeout=10,
6766
).json()
@@ -76,24 +75,18 @@ def make_slug(name: str) -> str | None:
7675
return None
7776

7877
if result_json["count"] > 1:
79-
print(
80-
"Warning: Got back %s results for %s"
81-
% (
82-
result_json["count"],
83-
name,
84-
)
85-
)
78+
print(f"Warning: Got back {result_json['count']} results for {name}")
8679
return None
8780
if result_json["count"] < 1:
88-
print("Warning: Got back no results for %s" % name)
81+
print(f"Warning: Got back no results for {name}")
8982
name_parts = name.split()
9083
if len(name_parts) == 2:
9184
return f"{name_parts[1].lower()}-{name_parts[0].lower()}"
9285
return None
9386

9487
result_id = result_json["results"][0]["id"]
9588
result_json = requests.get(
96-
"https://www.courtlistener.com/api/rest/v4/people/?id=%s" % result_id,
89+
f"https://www.courtlistener.com/api/rest/v4/people/?id={result_id}",
9790
headers=headers,
9891
timeout=10,
9992
).json()
@@ -145,7 +138,6 @@ def run_things():
145138
judge_info.append((name, url))
146139

147140
for judge_name, judge_link in judge_info:
148-
149141
try:
150142
judge_r = requests.get(judge_link, timeout=10)
151143
except Timeout:
@@ -165,7 +157,7 @@ def run_things():
165157
'//div[@class = "judge_portrait"]//img/@src'
166158
)[0]
167159
except IndexError:
168-
print("Failed to find image for %s" % judge_link)
160+
print(f"Failed to find image for {judge_link}")
169161
continue
170162

171163
try:
@@ -197,12 +189,15 @@ def run_things():
197189
"hash": img_hash,
198190
}
199191

200-
json.dump(
201-
judge_pics,
202-
open(os.path.join(judge_root, "judges.json"), "w", encoding="utf-8"),
203-
sort_keys=True,
204-
indent=2,
205-
)
192+
with open(
193+
os.path.join(judge_root, "judges.json"), "w", encoding="utf-8"
194+
) as fp:
195+
json.dump(
196+
judge_pics,
197+
fp,
198+
sort_keys=True,
199+
indent=2,
200+
)
206201

207202

208203
if __name__ == "__main__":

judge_pics/scrapers/dc_circuit_judges.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def run_things():
8181
)[0]
8282
full_img_src = root_url + img_path
8383
except IndexError:
84-
print("Failed to find image for %s" % full_url)
84+
print(f"Failed to find image for {full_url}")
8585
continue
8686
except Timeout:
8787
print(
@@ -122,12 +122,15 @@ def run_things():
122122
"hash": img_hash,
123123
}
124124

125-
json.dump(
126-
judge_pics,
127-
open(os.path.join(judge_root, "judges.json"), "w", encoding="utf-8"),
128-
sort_keys=True,
129-
indent=2,
130-
)
125+
with open(
126+
os.path.join(judge_root, "judges.json"), "w", encoding="utf-8"
127+
) as fp:
128+
json.dump(
129+
judge_pics,
130+
fp,
131+
sort_keys=True,
132+
indent=2,
133+
)
131134

132135

133136
if __name__ == "__main__":

judge_pics/search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def portrait(
5454
person: Union[str, int], size: SIZES = ImageSizes.ORIGINAL
5555
) -> Optional[str]:
5656
"""Get URL for portait on free.law"""
57-
if type(person) == int:
57+
if isinstance(person, int):
5858
paths = [x for x in judges if x["person"] == person]
5959
if len(paths) > 0:
6060
return f"https://portraits.free.law/v2/{size.value}/{paths[0]['path']}.jpeg"

judge_pics/update.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def update_judge_json(
6969
raise ValueError("Judge already exists")
7070
strip_metadata_from_file(filename)
7171
sha_hash = get_hash_from_file(filename)
72-
if any([x["hash"] == sha_hash for x in judges_json]):
72+
if any(x["hash"] == sha_hash for x in judges_json):
7373
raise ValueError(
7474
f"Image with hash '{sha_hash}' already exists in the database"
7575
)

judge_pics/upload.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ def validate_json() -> bool:
7070
with Path(ROOT_DIR, "data", "people.json").open(encoding="utf-8") as f:
7171
judges = json.load(f)
7272

73-
judges_in_json = [f'{v["path"]}.jpeg' for v in judges]
73+
judges_in_json = [f"{v['path']}.jpeg" for v in judges]
7474

7575
portraits = [
7676
x.split("/")[-1] for x in glob.glob(f"{ROOT_DIR}/data/orig/*.jpeg")
7777
]
7878

79-
missing_judges = sorted(list(set(judges_in_json) ^ set(portraits)))
79+
missing_judges = sorted(set(judges_in_json) ^ set(portraits))
8080
if not missing_judges:
8181
return True
8282

@@ -102,7 +102,7 @@ def find_new_portraits(access_key: str, secret_key: str) -> list:
102102
x.split("/")[-1] for x in glob.glob(f"{ROOT_DIR}/data/orig/*.jpeg")
103103
]
104104
judges_to_upload = set(aws_portraits) ^ set(local_portraits)
105-
return sorted(list(judges_to_upload))
105+
return sorted(judges_to_upload)
106106

107107

108108
def main(access_key: str, secret_key: str) -> None:

pyproject.toml

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1-
[tool.black]
2-
line-length = 79
3-
include = '''.*\.pyi?$'''
4-
51
[build-system]
62
requires = ["setuptools >= 40.8.0", "wheel"]
73
build-backend = "setuptools.build_meta"
4+
5+
[tool.ruff]
6+
line-length = 79
7+
lint.select = [
8+
# flake8-bugbear
9+
"B",
10+
# flake8-comprehensions
11+
"C4",
12+
# pycodestyle
13+
"E",
14+
# Pyflakes errors
15+
"F",
16+
# isort
17+
"I",
18+
# flake8-simplify
19+
"SIM",
20+
# flake8-tidy-imports
21+
"TID",
22+
# pyupgrade
23+
"UP",
24+
# Pyflakes warnings
25+
"W",
26+
]
27+
lint.ignore = [
28+
# flake8-bugbear opinionated rules
29+
"B9",
30+
# line-too-long
31+
"E501",
32+
# suppressible-exception
33+
"SIM105",
34+
# if-else-block-instead-of-if-exp
35+
"SIM108",
36+
]

0 commit comments

Comments
 (0)