Skip to content

Commit 992bee2

Browse files
chilin0525raulcd
andauthored
GH-45619: [Python] Use f-string instead of string.format (#45629)
### Rationale for this change See #45619. ### What changes are included in this PR? Refactor using f-string instead of `string.format`. But do not use f-string for following case, `string.format` allows passing parameters, making the code more reusable. https://github.com/apache/arrow/blob/0fbf9823542233c5f32c26534c34cc97ce3f0be2/python/pyarrow/parquet/core.py#L1624-L1695 ### Are these changes tested? Via CI. ### Are there any user-facing changes? No. * GitHub Issue: #45619 Lead-authored-by: Chilin Chiou <chilin.chiou@gmail.com> Co-authored-by: Raúl Cumplido <raulcumplido@gmail.com> Signed-off-by: Raúl Cumplido <raulcumplido@gmail.com>
1 parent c3855b9 commit 992bee2

File tree

110 files changed

+754
-949
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+754
-949
lines changed

cpp/build-support/asan_symbolize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def symbolize(self, addr, binary, offset):
171171
# foo(type1, type2) (in object.name) (filename.cc:80)
172172
match = re.match(r'^(.*) \(in (.*)\) \((.*:\d*)\)$', atos_line)
173173
if DEBUG:
174-
print('atos_line: {0}'.format(atos_line))
174+
print(f'atos_line: {atos_line}')
175175
if match:
176176
function_name = match.group(1)
177177
function_name = re.sub(r'\(.*?\)', '', function_name)

cpp/build-support/fuzzing/pack_corpus.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ def process_dir(corpus_dir, zip_output):
3131

3232
for child in corpus_dir.iterdir():
3333
if not child.is_file():
34-
raise IOError("Not a file: {0}".format(child))
34+
raise IOError(f"Not a file: {child}")
3535
with child.open('rb') as f:
3636
data = f.read()
3737
arcname = hashlib.sha1(data).hexdigest()
3838
if arcname in seen:
39-
raise ValueError("Duplicate hash: {0} (in file {1})"
40-
.format(arcname, child))
39+
raise ValueError(f"Duplicate hash: {arcname} (in file {child})")
4140
zip_output.writestr(str(arcname), data)
4241
seen.add(arcname)
4342

@@ -49,6 +48,6 @@ def main(corpus_dir, zip_output_name):
4948

5049
if __name__ == "__main__":
5150
if len(sys.argv) != 3:
52-
print("Usage: {0} <corpus dir> <output zip file>".format(sys.argv[0]))
51+
print(f"Usage: {sys.argv[0]} <corpus dir> <output zip file>")
5352
sys.exit(1)
5453
main(sys.argv[1], sys.argv[2])

cpp/build-support/iwyu/iwyu_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def main(compilation_db_path, source_files, verbose, formatter, iwyu_args):
204204
if matches:
205205
entries.extend(matches)
206206
else:
207-
print("{} not in compilation database".format(source))
207+
print(f"{source} not in compilation database")
208208
# TODO: As long as there is no complete compilation database available this check cannot be performed
209209
pass
210210
#print('WARNING: \'%s\' not found in compilation database.', source)

cpp/build-support/lint_cpp_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def lint_files():
121121
if __name__ == '__main__':
122122
failures = list(lint_files())
123123
for path, why, i, line in failures:
124-
print('File {0} failed C++/CLI lint check: {1}\n'
125-
'Line {2}: {3}'.format(path, why, i + 1, line))
124+
print(f'File {path} failed C++/CLI lint check: {why}\n'
125+
f'Line {i + 1}: {line}')
126126
if failures:
127127
exit(1)

cpp/build-support/run_clang_format.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def _check_one_file(filename, formatted):
3838
original.decode('utf8').splitlines(True),
3939
formatted.decode('utf8').splitlines(True),
4040
fromfile=filename,
41-
tofile="{} (after clang format)".format(
42-
filename)))
41+
tofile=f"{filename} (after clang format)"))
4342
else:
4443
diff = None
4544

@@ -85,8 +84,7 @@ def _check_one_file(filename, formatted):
8584

8685
if arguments.fix:
8786
if not arguments.quiet:
88-
print("\n".join(map(lambda x: "Formatting {}".format(x),
89-
formatted_filenames)))
87+
print("\n".join(f"Formatting {x}" for x in formatted_filenames))
9088

9189
# Break clang-format invocations into chunks: each invocation formats
9290
# 16 files. Wait for all processes to complete
@@ -122,9 +120,9 @@ def _check_one_file(filename, formatted):
122120
# check the output from each invocation of clang-format in parallel
123121
for filename, diff in pool.starmap(_check_one_file, checker_args):
124122
if not arguments.quiet:
125-
print("Checking {}".format(filename))
123+
print(f"Checking {filename}")
126124
if diff:
127-
print("{} had clang-format style issues".format(filename))
125+
print(f"{filename} had clang-format style issues")
128126
# Print out the diff to stderr
129127
error = True
130128
# pad with a newline

cpp/build-support/run_cpplint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _check_some_files(completed_processes, filenames):
7878
if arguments.quiet:
7979
cmd.append('--quiet')
8080
else:
81-
print("\n".join(map(lambda x: "Linting {}".format(x),
81+
print("\n".join(map(lambda x: f"Linting {x}",
8282
linted_filenames)))
8383

8484
# lint files in chunks: each invocation of cpplint will process 16 files

cpp/src/arrow/util/bpacking64_codegen.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,55 +73,52 @@ def howmanybytes(bit):
7373

7474

7575
print("inline const uint8_t* unpack0_64(const uint8_t* in, uint64_t* out) {")
76-
print(" for(int k = 0; k < {0} ; k += 1) {{".format(howmany(0)))
76+
print(f" for(int k = 0; k < {howmany(0)} ; k += 1) {{")
7777
print(" out[k] = 0;")
7878
print(" }")
7979
print(" return in;")
8080
print("}")
8181

8282
for bit in range(1, 65):
8383
print("")
84-
print(
85-
"inline const uint8_t* unpack{0}_64(const uint8_t* in, uint64_t* out) {{".format(bit))
84+
print(f"inline const uint8_t* unpack{bit}_64(const uint8_t* in, uint64_t* out) {{")
8685

8786
if(bit < 64):
88-
print(" const uint64_t mask = {0}ULL;".format((1 << bit)-1))
87+
print(f" const uint64_t mask = {((1 << bit)-1)}ULL;")
8988
maskstr = " & mask"
9089
if (bit == 64):
9190
maskstr = "" # no need
9291

9392
for k in range(howmanywords(bit)-1):
94-
print(" uint64_t w{0} = util::SafeLoadAs<uint64_t>(in);".format(k))
95-
print(" w{0} = arrow::BitUtil::FromLittleEndian(w{0});".format(k))
96-
print(" in += 8;".format(k))
93+
print(f" uint64_t w{k} = util::SafeLoadAs<uint64_t>(in);")
94+
print(f" w{k} = arrow::BitUtil::FromLittleEndian(w{k});")
95+
print(" in += 8;")
9796
k = howmanywords(bit) - 1
9897
if (bit % 2 == 0):
99-
print(" uint64_t w{0} = util::SafeLoadAs<uint64_t>(in);".format(k))
100-
print(" w{0} = arrow::BitUtil::FromLittleEndian(w{0});".format(k))
101-
print(" in += 8;".format(k))
98+
print(f" uint64_t w{k} = util::SafeLoadAs<uint64_t>(in);")
99+
print(f" w{k} = arrow::BitUtil::FromLittleEndian(w{k});")
100+
print(" in += 8;")
102101
else:
103-
print(" uint64_t w{0} = util::SafeLoadAs<uint32_t>(in);".format(k))
104-
print(" w{0} = arrow::BitUtil::FromLittleEndian(w{0});".format(k))
105-
print(" in += 4;".format(k))
102+
print(f" uint64_t w{k} = util::SafeLoadAs<uint32_t>(in);")
103+
print(f" w{k} = arrow::BitUtil::FromLittleEndian(w{k});")
104+
print(" in += 4;")
106105

107106
for j in range(howmany(bit)):
108107
firstword = j * bit // 64
109108
secondword = (j * bit + bit - 1)//64
110109
firstshift = (j*bit) % 64
111-
firstshiftstr = " >> {0}".format(firstshift)
110+
firstshiftstr = f" >> {firstshift}"
112111
if(firstshift == 0):
113112
firstshiftstr = "" # no need
114113
if(firstword == secondword):
115114
if(firstshift + bit == 64):
116-
print(" out[{0}] = w{1}{2};".format(
117-
j, firstword, firstshiftstr, firstshift))
115+
print(f" out[{j}] = w{firstword}{firstshiftstr};")
118116
else:
119-
print(" out[{0}] = (w{1}{2}){3};".format(
120-
j, firstword, firstshiftstr, maskstr))
117+
print(f" out[{j}] = (w{firstword}{firstshiftstr}){maskstr};")
121118
else:
122119
secondshift = (64-firstshift)
123-
print(" out[{0}] = ((w{1}{2}) | (w{3} << {4})){5};".format(
124-
j, firstword, firstshiftstr, firstword+1, secondshift, maskstr))
120+
print(f" out[{j}] = ((w{firstword}{firstshiftstr}) | "
121+
f"(w{firstword+1} << {secondshift})){maskstr};")
125122
print("")
126123
print(" return in;")
127124
print("}")

cpp/src/gandiva/make_precompiled_bitcode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def apply_template(template, data):
3737

3838
if __name__ == "__main__":
3939
if len(sys.argv) != 4:
40-
raise ValueError("Usage: {0} <template file> <data file> "
41-
"<output file>".format(sys.argv[0]))
40+
raise ValueError(f"Usage: {sys.argv[0]} <template file> <data file> "
41+
"<output file>")
4242
with open(sys.argv[1], "rb") as f:
4343
template = f.read()
4444
with open(sys.argv[2], "rb") as f:

dev/archery/archery/benchmark/compare.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@
2323

2424
def items_per_seconds_fmt(value):
2525
if value < 1000:
26-
return "{} items/sec".format(value)
26+
return f"{value} items/sec"
2727
if value < 1000**2:
28-
return "{:.3f}K items/sec".format(value / 1000)
28+
return f"{value / 1000:.3f}K items/sec"
2929
if value < 1000**3:
30-
return "{:.3f}M items/sec".format(value / 1000**2)
30+
return f"{value / 1000**2:.3f}M items/sec"
3131
else:
32-
return "{:.3f}G items/sec".format(value / 1000**3)
32+
return f"{value / 1000**3:.3f}G items/sec"
3333

3434

3535
def bytes_per_seconds_fmt(value):
3636
if value < 1024:
37-
return "{} bytes/sec".format(value)
37+
return f"{value} bytes/sec"
3838
if value < 1024**2:
39-
return "{:.3f} KiB/sec".format(value / 1024)
39+
return f"{value / 1024:.3f} KiB/sec"
4040
if value < 1024**3:
41-
return "{:.3f} MiB/sec".format(value / 1024**2)
41+
return f"{value / 1024**2:.3f} MiB/sec"
4242
if value < 1024**4:
43-
return "{:.3f} GiB/sec".format(value / 1024**3)
43+
return f"{value / 1024**3:.3f} GiB/sec"
4444
else:
45-
return "{:.3f} TiB/sec".format(value / 1024**4)
45+
return f"{value / 1024**4:.3f} TiB/sec"
4646

4747

4848
def change_fmt(value):
49-
return "{:.3%}".format(value)
49+
return f"{value:.3%}"
5050

5151

5252
def formatter_for_unit(unit):

dev/archery/archery/benchmark/core.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def value(self):
4343
return self.median
4444

4545
def __repr__(self):
46-
return "Benchmark[name={},value={}]".format(self.name, self.value)
46+
return f"Benchmark[name={self.name},value={self.value}]"
4747

4848

4949
class BenchmarkSuite:
@@ -52,6 +52,4 @@ def __init__(self, name, benchmarks):
5252
self.benchmarks = benchmarks
5353

5454
def __repr__(self):
55-
return "BenchmarkSuite[name={}, benchmarks={}]".format(
56-
self.name, self.benchmarks
57-
)
55+
return f"BenchmarkSuite[name={self.name}, benchmarks={self.benchmarks}]"

0 commit comments

Comments
 (0)