Skip to content

Commit d7713f6

Browse files
authored
Merge pull request #904 from minrk/nbqa
run ruff via nbqa on notebooks
2 parents 067c417 + 05dc87c commit d7713f6

11 files changed

+92
-93
lines changed

Diff for: .pre-commit-config.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ repos:
88
- id: ruff
99
args:
1010
- "--fix"
11+
exclude_types:
12+
- jupyter
1113
- id: ruff-format
14+
exclude_types:
15+
- jupyter
16+
# run ruff via nbqa, which has better and configurable handling of cell magics
17+
# than ruff itself
18+
- repo: https://github.com/nbQA-dev/nbQA
19+
rev: 1.9.0
20+
hooks:
21+
- id: nbqa-ruff-format
22+
- id: nbqa-ruff-check
23+
args:
24+
- --fix
1225
- repo: https://github.com/pre-commit/mirrors-prettier
1326
rev: v4.0.0-alpha.8
1427
hooks:

Diff for: docs/source/examples/Futures.ipynb

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"%%px --local --block\n",
6868
"import os\n",
6969
"import time\n",
70+
"\n",
7071
"import numpy as np\n",
7172
"from numpy.linalg import norm"
7273
]
@@ -186,8 +187,6 @@
186187
}
187188
],
188189
"source": [
189-
"import os\n",
190-
"\n",
191190
"f = rc[-1].apply(os.getpid)\n",
192191
"f.add_done_callback(lambda _: print(\"I got PID: %i\" % _.result()))\n",
193192
"f.result()"

Diff for: docs/source/examples/Monitoring an MPI Simulation - 1.ipynb

+8-6
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@
119119
"source": [
120120
"%%px --block\n",
121121
"# MPI initialization, library imports and sanity checks on all engines\n",
122-
"from mpi4py import MPI\n",
123-
"import numpy as np\n",
124122
"import time\n",
125123
"\n",
124+
"from mpi4py import MPI\n",
125+
"\n",
126126
"mpi = MPI.COMM_WORLD\n",
127127
"bcast = mpi.bcast\n",
128128
"barrier = mpi.barrier\n",
129129
"rank = mpi.rank\n",
130-
"print(\"MPI rank: %i/%i\" % (mpi.rank,mpi.size))"
130+
"print(\"MPI rank: %i/%i\" % (mpi.rank, mpi.size))"
131131
]
132132
},
133133
{
@@ -229,7 +229,7 @@
229229
" )\n",
230230
" X, Y = np.meshgrid(Xax, Yax)\n",
231231
" f = freqs[j]\n",
232-
" Z = np.cos(f * (X ** 2 + Y ** 2))\n",
232+
" Z = np.cos(f * (X**2 + Y**2))\n",
233233
" # We add a small delay to simulate that a real-world computation\n",
234234
" # would take much longer, and we ensure all nodes are synchronized\n",
235235
" time.sleep(delay)\n",
@@ -533,7 +533,9 @@
533533
],
534534
"source": [
535535
"%%px --target 0 --block\n",
536-
"from ipyparallel import bind_kernel; bind_kernel()\n",
536+
"from ipyparallel import bind_kernel\n",
537+
"\n",
538+
"bind_kernel()\n",
537539
"%connect_info"
538540
]
539541
},
@@ -564,7 +566,7 @@
564566
"name": "python",
565567
"nbconvert_exporter": "python",
566568
"pygments_lexer": "ipython3",
567-
"version": "3.9.6"
569+
"version": "3.11.9"
568570
},
569571
"widgets": {
570572
"application/vnd.jupyter.widget-state+json": {

Diff for: docs/source/examples/Monitoring an MPI Simulation - 2.ipynb

+6-5
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,18 @@
108108
"source": [
109109
"%%px --block\n",
110110
"# MPI initialization, library imports and sanity checks on all engines\n",
111+
"import time\n",
112+
"\n",
111113
"from mpi4py import MPI\n",
114+
"\n",
112115
"# Load data publication API so engines can send data to notebook client\n",
113116
"from ipyparallel.datapub import publish_data\n",
114-
"import numpy as np\n",
115-
"import time\n",
116117
"\n",
117118
"mpi = MPI.COMM_WORLD\n",
118119
"bcast = mpi.bcast\n",
119120
"barrier = mpi.barrier\n",
120121
"rank = mpi.rank\n",
121-
"print(\"MPI rank: %i/%i\" % (mpi.rank,mpi.size))"
122+
"print(\"MPI rank: %i/%i\" % (mpi.rank, mpi.size))"
122123
]
123124
},
124125
{
@@ -196,7 +197,7 @@
196197
" )\n",
197198
" X, Y = np.meshgrid(Xax, Yax)\n",
198199
" f = freqs[j]\n",
199-
" Z = np.cos(f * (X ** 2 + Y ** 2))\n",
200+
" Z = np.cos(f * (X**2 + Y**2))\n",
200201
"\n",
201202
" # We are now going to publish data to the clients. We take advantage of fast\n",
202203
" # MPI communications and gather the Z mesh at the rank 0 node in the Zcat variable:\n",
@@ -419,7 +420,7 @@
419420
"name": "python",
420421
"nbconvert_exporter": "python",
421422
"pygments_lexer": "ipython3",
422-
"version": "3.9.6"
423+
"version": "3.11.9"
423424
},
424425
"widgets": {
425426
"application/vnd.jupyter.widget-state+json": {

Diff for: docs/source/examples/Parallel Magics.ipynb

+13-10
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@
568568
"source": [
569569
"%%px\n",
570570
"import psutil\n",
571+
"\n",
571572
"psutil.Process().nice(20 if psutil.POSIX else psutil.IDLE_PRIORITY_CLASS)"
572573
]
573574
},
@@ -578,8 +579,8 @@
578579
"outputs": [],
579580
"source": [
580581
"%%px\n",
581-
"import numpy as np\n",
582-
"import matplotlib.pyplot as plt"
582+
"import matplotlib.pyplot as plt\n",
583+
"import numpy as np"
583584
]
584585
},
585586
{
@@ -619,10 +620,10 @@
619620
],
620621
"source": [
621622
"%%px --noblock\n",
622-
"x = np.linspace(0,np.pi,1000)\n",
623-
"for n in range(id,12, stride):\n",
623+
"x = np.linspace(0, np.pi, 1000)\n",
624+
"for n in range(id, 12, stride):\n",
624625
" print(n)\n",
625-
" plt.plot(x,np.sin(n*x))\n",
626+
" plt.plot(x, np.sin(n * x))\n",
626627
"plt.title(\"Plot %i\" % id)"
627628
]
628629
},
@@ -1318,11 +1319,11 @@
13181319
],
13191320
"source": [
13201321
"%%px --group-outputs=engine\n",
1321-
"x = np.linspace(0,np.pi,1000)\n",
1322-
"for n in range(id+1,12, stride):\n",
1322+
"x = np.linspace(0, np.pi, 1000)\n",
1323+
"for n in range(id + 1, 12, stride):\n",
13231324
" print(n)\n",
13241325
" plt.figure()\n",
1325-
" plt.plot(x,np.sin(n*x))\n",
1326+
" plt.plot(x, np.sin(n * x))\n",
13261327
" plt.title(\"Plot %i\" % n)"
13271328
]
13281329
},
@@ -3261,6 +3262,7 @@
32613262
"source": [
32623263
"%%px\n",
32633264
"from numpy.random import random\n",
3265+
"\n",
32643266
"A = random((100, 100, 'invalid shape'))"
32653267
]
32663268
},
@@ -3343,10 +3345,10 @@
33433345
"source": [
33443346
"%%px\n",
33453347
"import time\n",
3348+
"\n",
33463349
"if rank == 0:\n",
33473350
" raise RuntimeError(\"rank 0 failed!\")\n",
3348-
"time.sleep(10)\n",
3349-
" "
3351+
"time.sleep(10)"
33503352
]
33513353
},
33523354
{
@@ -3469,6 +3471,7 @@
34693471
"source": [
34703472
"%%px --local\n",
34713473
"import os\n",
3474+
"\n",
34723475
"thispid = os.getpid()\n",
34733476
"print(thispid)"
34743477
]

Diff for: docs/source/examples/Using MPI with IPython Parallel.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@
181181
"rank = comm.Get_rank()\n",
182182
"\n",
183183
"if rank == 0:\n",
184-
" data = [(i+1)**2 for i in range(size)]\n",
184+
" data = [(i + 1) ** 2 for i in range(size)]\n",
185185
"else:\n",
186-
" data = None\n",
186+
" data = None\n",
187187
"data = comm.scatter(data, root=0)\n",
188188
"\n",
189-
"assert data == (rank+1)**2, 'data=%s, rank=%s' % (data, rank)\n",
189+
"assert data == (rank + 1) ** 2, f'data={data}, rank={rank}'\n",
190190
"{\n",
191191
" 'data': data,\n",
192192
" 'rank': rank,\n",

Diff for: docs/source/examples/broadcast/MPI Broadcast.ipynb

+4-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
}
249249
],
250250
"source": [
251-
"%%time \n",
251+
"%%time\n",
252252
"ar = eall.push({'data': data}, block=False)\n",
253253
"ar.wait_interactive()"
254254
]
@@ -2568,7 +2568,8 @@
25682568
"source": [
25692569
"%%px\n",
25702570
"import numpy as np\n",
2571-
"np.linalg.norm(data, 2)\n"
2571+
"\n",
2572+
"np.linalg.norm(data, 2)"
25722573
]
25732574
}
25742575
],
@@ -2588,7 +2589,7 @@
25882589
"name": "python",
25892590
"nbconvert_exporter": "python",
25902591
"pygments_lexer": "ipython3",
2591-
"version": "3.9.6"
2592+
"version": "3.11.9"
25922593
},
25932594
"widgets": {
25942595
"application/vnd.jupyter.widget-state+json": {

Diff for: docs/source/examples/progress.ipynb

+8-3
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,22 @@
353353
"n_steps = 10\n",
354354
"\n",
355355
"progress = W.IntProgress(max=n_steps, description=f\"rank {rank}: 0\")\n",
356+
"\n",
357+
"\n",
356358
"def _set_color(change):\n",
357359
" if change.new == progress.max:\n",
358360
" progress.bar_style = \"success\"\n",
359361
" else:\n",
360362
" progress.bar_style = \"\"\n",
363+
"\n",
364+
"\n",
361365
"progress.observe(_set_color, \"value\")\n",
362366
"display(progress)\n",
363367
"\n",
364368
"for i in range(n_steps):\n",
365369
" time.sleep(random.random())\n",
366370
" progress.value += 1\n",
367-
" progress.description = f\"rank {rank}: {progress.value}/{progress.max}\"\n",
368-
"\n"
371+
" progress.description = f\"rank {rank}: {progress.value}/{progress.max}\""
369372
]
370373
},
371374
{
@@ -448,11 +451,13 @@
448451
"\n",
449452
"if rank == 0:\n",
450453
" progress = W.IntProgress(max=n_steps, description=\"Step 0\")\n",
454+
"\n",
451455
" def _set_color(change):\n",
452456
" if change.new == progress.max:\n",
453457
" progress.bar_style = \"success\"\n",
454458
" else:\n",
455459
" progress.bar_style = \"\"\n",
460+
"\n",
456461
" progress.observe(_set_color, \"value\")\n",
457462
" display(progress)\n",
458463
"\n",
@@ -585,7 +590,7 @@
585590
],
586591
"source": [
587592
"%%px\n",
588-
"button = W.Button(description=f\"step\")\n",
593+
"button = W.Button(description=\"step\")\n",
589594
"progress = W.IntProgress(max=5, description=\"0 / 5\")\n",
590595
"count = 0\n",
591596
"\n",

0 commit comments

Comments
 (0)