Skip to content

Commit e3961ec

Browse files
committed
STYLE: Call write once instead of looping
Call `write` once instead of looping by concatenating all strings that need to be written. Avoid using `writelines` as `ImageOpener` objects do not have that attribute. Fixes: ``` - for val in bvals: - fid.write(f'{val} ') + fid.writelines(f'{val} ' for val in bvals) ``` and other similar errors raised for example in: https://github.com/nipy/nibabel/actions/runs/16092302666/job/45410655457?pr=1422#step:7:23 Documentation: https://docs.astral.sh/ruff/rules/for-loop-writes/
1 parent b5ecf48 commit e3961ec

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

nibabel/cmdline/parrec2nii.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ def proc_file(infile, opts):
358358
)
359359
with open(basefilename + '.bvals', 'w') as fid:
360360
# np.savetxt could do this, but it's just a loop anyway
361-
for val in bvals:
362-
fid.write(f'{val} ')
361+
fid.write(' '.join(str(val) for val in bvals) + ' ')
363362
fid.write('\n')
364363
else:
365364
verbose('Writing .bvals and .bvecs files')
@@ -369,13 +368,11 @@ def proc_file(infile, opts):
369368
bvecs = apply_affine(bv_reorient, bvecs)
370369
with open(basefilename + '.bvals', 'w') as fid:
371370
# np.savetxt could do this, but it's just a loop anyway
372-
for val in bvals:
373-
fid.write(f'{val} ')
371+
fid.write(' '.join(str(val) for val in bvals) + ' ')
374372
fid.write('\n')
375373
with open(basefilename + '.bvecs', 'w') as fid:
376374
for row in bvecs.T:
377-
for val in row:
378-
fid.write(f'{val} ')
375+
fid.write(' '.join(str(val) for val in row) + ' ')
379376
fid.write('\n')
380377

381378
# export data labels varying along the 4th dimensions if requested

0 commit comments

Comments
 (0)