Skip to content

Commit 5db9856

Browse files
authored
Improve performance by not formatting hidden code unless requested (#74)
* avoid blackening for better performance * update test branches
1 parent 065e1fe commit 5db9856

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

.github/workflows/run-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ jobs:
177177
- region: ActivitySim 1-Zone Example (MTC)
178178
region-org: ActivitySim
179179
region-repo: activitysim-prototype-mtc
180-
region-branch: extended
180+
region-branch: pandas2
181181
- region: ActivitySim 2-Zone Example (SANDAG)
182182
region-org: ActivitySim
183183
region-repo: sandag-abm3-example
184-
region-branch: main
184+
region-branch: pandas2
185185
fail-fast: false
186186
defaults:
187187
run:

sharrow/filewrite.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,47 @@
33
import filecmp
44
import os
55
import uuid
6+
from pathlib import Path
67

78
from filelock import FileLock, Timeout
89

10+
# by default, blackening is disabled for performance reasons,
11+
# but it can be enabled by setting the environment variable SHARROW_BLACKEN=1
12+
SHARROW_BLACKEN = os.environ.get("SHARROW_BLACKEN", "0") == "1"
13+
914

1015
def blacken(code):
16+
if not SHARROW_BLACKEN:
17+
return code
18+
19+
# Ruff is much faster than black, so we try to use it first.
20+
import subprocess
21+
22+
which_ruff = subprocess.run(["which", "ruff"], capture_output=True, text=True)
23+
which_ruff.check_returncode()
24+
ruff_bin = which_ruff.stdout.strip()
25+
if ruff_bin:
26+
try:
27+
result = subprocess.run(
28+
[ruff_bin, "format", "-"],
29+
input=code,
30+
text=True,
31+
capture_output=True,
32+
check=True,
33+
cwd=Path.cwd(),
34+
)
35+
result.check_returncode()
36+
return result.stdout
37+
except subprocess.CalledProcessError as err:
38+
import warnings
39+
40+
warnings.warn(f"error in ruffen: {err!r}", stacklevel=2)
41+
return code
42+
else:
43+
import warnings
44+
45+
warnings.warn("ruff not found, trying black instead", stacklevel=2)
46+
1147
try:
1248
import black
1349
except ImportError:

0 commit comments

Comments
 (0)