Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-45619: [Python] Use f-string instead of string.format #45629

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/build-support/asan_symbolize.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def symbolize(self, addr, binary, offset):
# foo(type1, type2) (in object.name) (filename.cc:80)
match = re.match(r'^(.*) \(in (.*)\) \((.*:\d*)\)$', atos_line)
if DEBUG:
print('atos_line: {0}'.format(atos_line))
print(f'atos_line: {atos_line}')
if match:
function_name = match.group(1)
function_name = re.sub(r'\(.*?\)', '', function_name)
Expand Down
7 changes: 3 additions & 4 deletions cpp/build-support/fuzzing/pack_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ def process_dir(corpus_dir, zip_output):

for child in corpus_dir.iterdir():
if not child.is_file():
raise IOError("Not a file: {0}".format(child))
raise IOError(f"Not a file: {child}")
with child.open('rb') as f:
data = f.read()
arcname = hashlib.sha1(data).hexdigest()
if arcname in seen:
raise ValueError("Duplicate hash: {0} (in file {1})"
.format(arcname, child))
raise ValueError(f"Duplicate hash: {arcname} (in file {child})")
zip_output.writestr(str(arcname), data)
seen.add(arcname)

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

if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: {0} <corpus dir> <output zip file>".format(sys.argv[0]))
print(f"Usage: {sys.argv[0]} <corpus dir> <output zip file>")
sys.exit(1)
main(sys.argv[1], sys.argv[2])
2 changes: 1 addition & 1 deletion cpp/build-support/iwyu/iwyu_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def main(compilation_db_path, source_files, verbose, formatter, iwyu_args):
if matches:
entries.extend(matches)
else:
print("{} not in compilation database".format(source))
print(f"{source} not in compilation database")
# TODO: As long as there is no complete compilation database available this check cannot be performed
pass
#print('WARNING: \'%s\' not found in compilation database.', source)
Expand Down
4 changes: 2 additions & 2 deletions cpp/build-support/lint_cpp_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def lint_files():
if __name__ == '__main__':
failures = list(lint_files())
for path, why, i, line in failures:
print('File {0} failed C++/CLI lint check: {1}\n'
'Line {2}: {3}'.format(path, why, i + 1, line))
print(f'File {path} failed C++/CLI lint check: {why}\n'
f'Line {i + 1}: {line}')
if failures:
exit(1)
10 changes: 4 additions & 6 deletions cpp/build-support/run_clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def _check_one_file(filename, formatted):
original.decode('utf8').splitlines(True),
formatted.decode('utf8').splitlines(True),
fromfile=filename,
tofile="{} (after clang format)".format(
filename)))
tofile=f"{filename} (after clang format)"))
else:
diff = None

Expand Down Expand Up @@ -85,8 +84,7 @@ def _check_one_file(filename, formatted):

if arguments.fix:
if not arguments.quiet:
print("\n".join(map(lambda x: "Formatting {}".format(x),
formatted_filenames)))
print("\n".join(f"Formatting {x}" for x in formatted_filenames))

# Break clang-format invocations into chunks: each invocation formats
# 16 files. Wait for all processes to complete
Expand Down Expand Up @@ -122,9 +120,9 @@ def _check_one_file(filename, formatted):
# check the output from each invocation of clang-format in parallel
for filename, diff in pool.starmap(_check_one_file, checker_args):
if not arguments.quiet:
print("Checking {}".format(filename))
print(f"Checking {filename}")
if diff:
print("{} had clang-format style issues".format(filename))
print(f"{filename} had clang-format style issues")
# Print out the diff to stderr
error = True
# pad with a newline
Expand Down
2 changes: 1 addition & 1 deletion cpp/build-support/run_cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _check_some_files(completed_processes, filenames):
if arguments.quiet:
cmd.append('--quiet')
else:
print("\n".join(map(lambda x: "Linting {}".format(x),
print("\n".join(map(lambda x: f"Linting {x}",
linted_filenames)))

# lint files in chunks: each invocation of cpplint will process 16 files
Expand Down
37 changes: 17 additions & 20 deletions cpp/src/arrow/util/bpacking64_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,55 +73,52 @@ def howmanybytes(bit):


print("inline const uint8_t* unpack0_64(const uint8_t* in, uint64_t* out) {")
print(" for(int k = 0; k < {0} ; k += 1) {{".format(howmany(0)))
print(f" for(int k = 0; k < {howmany(0)} ; k += 1) {{{{")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check that this script still runs fine according to the instructions above?

print(" out[k] = 0;")
print(" }")
print(" return in;")
print("}")

for bit in range(1, 65):
print("")
print(
"inline const uint8_t* unpack{0}_64(const uint8_t* in, uint64_t* out) {{".format(bit))
print(f"inline const uint8_t* unpack{bit}_64(const uint8_t* in, uint64_t* out) {{{{")

if(bit < 64):
print(" const uint64_t mask = {0}ULL;".format((1 << bit)-1))
print(f" const uint64_t mask = {((1 << bit)-1)}ULL;")
maskstr = " & mask"
if (bit == 64):
maskstr = "" # no need

for k in range(howmanywords(bit)-1):
print(" uint64_t w{0} = util::SafeLoadAs<uint64_t>(in);".format(k))
print(" w{0} = arrow::BitUtil::FromLittleEndian(w{0});".format(k))
print(" in += 8;".format(k))
print(f" uint64_t w{k} = util::SafeLoadAs<uint64_t>(in);")
print(f" w{k} = arrow::BitUtil::FromLittleEndian(w{k});")
print(f" in += 8;")
k = howmanywords(bit) - 1
if (bit % 2 == 0):
print(" uint64_t w{0} = util::SafeLoadAs<uint64_t>(in);".format(k))
print(" w{0} = arrow::BitUtil::FromLittleEndian(w{0});".format(k))
print(" in += 8;".format(k))
print(f" uint64_t w{k} = util::SafeLoadAs<uint64_t>(in);")
print(f" w{k} = arrow::BitUtil::FromLittleEndian(w{k});")
print(f" in += 8;")
else:
print(" uint64_t w{0} = util::SafeLoadAs<uint32_t>(in);".format(k))
print(" w{0} = arrow::BitUtil::FromLittleEndian(w{0});".format(k))
print(" in += 4;".format(k))
print(f" uint64_t w{k} = util::SafeLoadAs<uint32_t>(in);")
print(f" w{k} = arrow::BitUtil::FromLittleEndian(w{k});")
print(f" in += 4;")

for j in range(howmany(bit)):
firstword = j * bit // 64
secondword = (j * bit + bit - 1)//64
firstshift = (j*bit) % 64
firstshiftstr = " >> {0}".format(firstshift)
firstshiftstr = f" >> {firstshift}"
if(firstshift == 0):
firstshiftstr = "" # no need
if(firstword == secondword):
if(firstshift + bit == 64):
print(" out[{0}] = w{1}{2};".format(
j, firstword, firstshiftstr, firstshift))
print(f" out[{j}] = w{firstword}{firstshiftstr};")
else:
print(" out[{0}] = (w{1}{2}){3};".format(
j, firstword, firstshiftstr, maskstr))
print(f" out[{j}] = (w{firstword}{firstshiftstr}){maskstr};")
else:
secondshift = (64-firstshift)
print(" out[{0}] = ((w{1}{2}) | (w{3} << {4})){5};".format(
j, firstword, firstshiftstr, firstword+1, secondshift, maskstr))
print(f" out[{j}] = ((w{firstword}{firstshiftstr}) | "
f"(w{firstword+1} << {secondshift})){maskstr};")
print("")
print(" return in;")
print("}")
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/make_precompiled_bitcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def apply_template(template, data):

if __name__ == "__main__":
if len(sys.argv) != 4:
raise ValueError("Usage: {0} <template file> <data file> "
"<output file>".format(sys.argv[0]))
raise ValueError(f"Usage: {sys.argv[0]} <template file> <data file> "
f"<output file>")
with open(sys.argv[1], "rb") as f:
template = f.read()
with open(sys.argv[2], "rb") as f:
Expand Down
20 changes: 10 additions & 10 deletions dev/archery/archery/benchmark/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@

def items_per_seconds_fmt(value):
if value < 1000:
return "{} items/sec".format(value)
return f"{value} items/sec"
if value < 1000**2:
return "{:.3f}K items/sec".format(value / 1000)
return f"{value / 1000:.3f}K items/sec"
if value < 1000**3:
return "{:.3f}M items/sec".format(value / 1000**2)
return f"{value / 1000**2:.3f}M items/sec"
else:
return "{:.3f}G items/sec".format(value / 1000**3)
return f"{value / 1000**3:.3f}G items/sec"


def bytes_per_seconds_fmt(value):
if value < 1024:
return "{} bytes/sec".format(value)
return f"{value} bytes/sec"
if value < 1024**2:
return "{:.3f} KiB/sec".format(value / 1024)
return f"{value / 1024:.3f} KiB/sec"
if value < 1024**3:
return "{:.3f} MiB/sec".format(value / 1024**2)
return f"{value / 1024**2:.3f} MiB/sec"
if value < 1024**4:
return "{:.3f} GiB/sec".format(value / 1024**3)
return f"{value / 1024**3:.3f} GiB/sec"
else:
return "{:.3f} TiB/sec".format(value / 1024**4)
return f"{value / 1024**4:.3f} TiB/sec"


def change_fmt(value):
return "{:.3%}".format(value)
return f"{value:.3%}"


def formatter_for_unit(unit):
Expand Down
6 changes: 2 additions & 4 deletions dev/archery/archery/benchmark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def value(self):
return self.median

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


class BenchmarkSuite:
Expand All @@ -52,6 +52,4 @@ def __init__(self, name, benchmarks):
self.benchmarks = benchmarks

def __repr__(self):
return "BenchmarkSuite[name={}, benchmarks={}]".format(
self.name, self.benchmarks
)
return f"BenchmarkSuite[name={self.name}, benchmarks={self.benchmarks}]"
4 changes: 2 additions & 2 deletions dev/archery/archery/benchmark/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, benchmark_bin, benchmark_filter=None, benchmark_extras=None):
def list_benchmarks(self):
argv = ["--benchmark_list_tests"]
if self.benchmark_filter:
argv.append("--benchmark_filter={}".format(self.benchmark_filter))
argv.append(f"--benchmark_filter={self.benchmark_filter}")
result = self.run(*argv, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return str.splitlines(result.stdout.decode("utf-8"))
Expand Down Expand Up @@ -166,7 +166,7 @@ def __init__(self, name, runs):
counters)

def __repr__(self):
return "GoogleBenchmark[name={},runs={}]".format(self.names, self.runs)
return f"GoogleBenchmark[name={self.name},runs={self.runs}]"

@classmethod
def from_json(cls, payload):
Expand Down
13 changes: 5 additions & 8 deletions dev/archery/archery/benchmark/jmh.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, build, benchmark_filter=None):
def list_benchmarks(self):
argv = []
if self.benchmark_filter:
argv.append("-Dbenchmark.filter={}".format(self.benchmark_filter))
argv.append(f"-Dbenchmark.filter={self.benchmark_filter}")
result = self.build.list(
*argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Expand All @@ -77,13 +77,11 @@ def list_benchmarks(self):

def results(self, repetitions):
with NamedTemporaryFile(suffix=".json") as out:
argv = ["-Dbenchmark.runs={}".format(repetitions),
"-Dbenchmark.resultfile={}".format(out.name),
argv = [f"-Dbenchmark.runs={repetitions}",
f"-Dbenchmark.resultfile={out.name}",
"-Dbenchmark.resultformat=json"]
if self.benchmark_filter:
argv.append(
"-Dbenchmark.filter={}".format(self.benchmark_filter)
)
argv.append(f"-Dbenchmark.filter={self.benchmark_filter}")

self.build.benchmark(*argv, check=True)
return json.load(out)
Expand Down Expand Up @@ -187,8 +185,7 @@ def __init__(self, name, runs):
counters)

def __repr__(self):
return "JavaMicrobenchmark[name={},runs={}]".format(
self.name, self.runs)
return f"JavaMicrobenchmark[name={self.name},runs={self.runs}]"

@classmethod
def from_json(cls, payload):
Expand Down
16 changes: 7 additions & 9 deletions dev/archery/archery/benchmark/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, suites, **kwargs):
def list_benchmarks(self):
for suite in self._suites:
for benchmark in suite.benchmarks:
yield "{}.{}".format(suite.name, benchmark.name)
yield f"{suite.name}.{benchmark.name}"

@property
def suites(self):
Expand Down Expand Up @@ -102,7 +102,7 @@ def from_json(path_or_str, **kwargs):
return BenchmarkRunnerCodec.decode(loaded, **kwargs)

def __repr__(self):
return "BenchmarkRunner[suites={}]".format(list(self.suites))
return f"BenchmarkRunner[suites={list(self.suites)}]"


class CppBenchmarkRunner(BenchmarkRunner):
Expand Down Expand Up @@ -165,7 +165,7 @@ def list_benchmarks(self):
for suite_name, suite_bin in self.suites_binaries.items():
suite_cmd = GoogleBenchmarkCommand(suite_bin)
for benchmark_name in suite_cmd.list_benchmarks():
yield "{}.{}".format(suite_name, benchmark_name)
yield f"{suite_name}.{benchmark_name}"

@property
def suites(self):
Expand All @@ -176,16 +176,15 @@ def suites(self):
suite_and_binaries = self.suites_binaries
for suite_name in suite_and_binaries:
if not suite_matcher(suite_name):
logger.debug("Ignoring suite {}".format(suite_name))
logger.debug(f"Ignoring suite {suite_name}")
continue

suite_bin = suite_and_binaries[suite_name]
suite = self.suite(suite_name, suite_bin)

# Filter may exclude all benchmarks
if not suite:
logger.debug("Suite {} executed but no results"
.format(suite_name))
logger.debug(f"Suite {suite_name} executed but no results")
continue

suite_found = True
Expand Down Expand Up @@ -274,7 +273,7 @@ def list_benchmarks(self):
suite_cmd = JavaMicrobenchmarkHarnessCommand(self.build)
benchmark_names = suite_cmd.list_benchmarks()
for benchmark_name in benchmark_names:
yield "{}".format(benchmark_name)
yield f"{benchmark_name}"

@property
def suites(self):
Expand All @@ -284,8 +283,7 @@ def suites(self):

# Filter may exclude all benchmarks
if not suite:
logger.debug("Suite {} executed but no results"
.format(suite_name))
logger.debug(f"Suite {suite_name} executed but no results")
return

yield suite
Expand Down
14 changes: 6 additions & 8 deletions dev/archery/archery/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def __init__(self, name, handler, token=None):
self.github = github.Github(**kwargs)

def parse_command(self, payload):
mention = '@{}'.format(self.name)
mention = f'@{self.name}'
comment = payload['comment']

if payload['sender']['login'] == self.name:
Expand Down Expand Up @@ -261,7 +261,7 @@ def handle(self, event, payload):
elif event == 'pull_request_review_comment':
return self.handle_review_comment(command, payload)
else:
raise ValueError("Unexpected event type {}".format(event))
raise ValueError(f"Unexpected event type {event}")

def handle_issue_comment(self, command, payload):
repo = self.github.get_repo(payload['repository']['id'], lazy=True)
Expand Down Expand Up @@ -291,11 +291,9 @@ def handle_issue_comment(self, command, payload):
comment=comment)
except Exception as e:
logger.exception(e)
url = "{server}/{repo}/actions/runs/{run_id}".format(
server=os.environ["GITHUB_SERVER_URL"],
repo=os.environ["GITHUB_REPOSITORY"],
run_id=os.environ["GITHUB_RUN_ID"],
)
url = (f"{os.environ['GITHUB_SERVER_URL']}/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually prefer the old version here, I think is is more readable with the placeholders.

f"{os.environ['GITHUB_REPOSITORY']}/"
f"actions/runs/{os.environ['GITHUB_RUN_ID']}")
pull.create_issue_comment(
f"```\n{e}\nThe Archery job run can be found at: {url}\n```")
comment.create_reaction('-1')
Expand Down Expand Up @@ -355,7 +353,7 @@ def _clone_arrow_and_crossbow(dest, crossbow_repo, arrow_repo_url, pr_number):
git.checkout(local_branch, git_dir=arrow_path)

# 2. clone crossbow repository
crossbow_url = 'https://github.com/{}'.format(crossbow_repo)
crossbow_url = f'https://github.com/{crossbow_repo}'
git.clone(crossbow_url, str(queue_path))

# 3. initialize crossbow objects
Expand Down
Loading
Loading