Skip to content

Commit 56d8683

Browse files
committedDec 6, 2023
More f-strings
1 parent a1afc00 commit 56d8683

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed
 

‎run_tests.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def _testsuite_from_tests(tests):
66
suite = unittest.TestSuite()
77
loader = unittest.defaultTestLoader
88
for t in tests:
9-
test = loader.loadTestsFromName('tests.%s' % (t))
9+
test = loader.loadTestsFromName(f'tests.{t}')
1010
suite.addTest(test)
1111
return suite
1212

@@ -32,7 +32,7 @@ def _testsuite_from_tests(tests):
3232

3333
#tests = ['test_functionality.BasicUsage.test_run_as_script']
3434

35-
print("Running following tests: %s" % (tests))
35+
print(f"Running following tests: {tests}")
3636

3737
result = test_runner.run(test_suite)
3838
sys.exit(not result.wasSuccessful())

‎tests/manual/_test_performance.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ def generate_func(code):
2222
for _, func in enumerate(funcs):
2323
for i in range(int(sys.argv[2])):
2424
func(i)
25-
print("Elapsed %0.6f secs." % (time.time() - t0))
25+
print(f"Elapsed {time.time() - t0:0.6f} secs.")

‎tests/manual/_test_performance2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ def generate_func(func_name, code):
3131
#yappi.start()
3232
for f in top_level_funcs:
3333
f(i)
34-
print("Elapsed %0.6f secs" % (time.time() - t0))
34+
print(f"Elapsed {time.time() - t0:0.6f} secs")
3535
#yappi.get_func_stats().print_all()

‎tests/test_functionality.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def test_get_clock(self):
277277
def test_profile_decorator(self):
278278

279279
def aggregate(func, stats):
280-
fname = "tests/%s.profile" % (func.__name__)
280+
fname = f"tests/{func.__name__}.profile"
281281
try:
282282
stats.add(fname)
283283
except OSError:
@@ -1191,7 +1191,7 @@ def run(self):
11911191
# TODO: I put dummy() to fix below, remove the comments after a while.
11921192
self.assertTrue( # FIX: I see this fails sometimes?
11931193
tsm is not None,
1194-
'Could not find "_MainThread". Found: %s' % (', '.join(utils.get_stat_names(tstats))))
1194+
f"Could not find \"_MainThread\". Found: {', '.join(utils.get_stat_names(tstats))}")
11951195

11961196
def test_ctx_stats(self):
11971197
from threading import Thread
@@ -1271,7 +1271,7 @@ def d():
12711271
# TODO: I put dummy() to fix below, remove the comments after a while.
12721272
self.assertTrue( # FIX: I see this fails sometimes
12731273
tsmain is not None,
1274-
'Could not find "_MainThread". Found: %s' % (', '.join(utils.get_stat_names(stats))))
1274+
f"Could not find \"_MainThread\". Found: {', '.join(utils.get_stat_names(stats))}")
12751275
self.assertTrue(1.0 > tst2.ttot >= 0.5)
12761276
self.assertTrue(1.0 > tst1.ttot >= 0.5)
12771277

@@ -1328,7 +1328,7 @@ def test():
13281328
ts = []
13291329
for i in (0.01, 0.05, 0.1):
13301330
t = threading.Thread(target=burn_cpu, args=(i, ))
1331-
t.name = "burn_cpu-%s" % str(i)
1331+
t.name = f"burn_cpu-{str(i)}"
13321332
t.start()
13331333
ts.append(t)
13341334
for t in ts:

‎yappi/yappi.py

+23-24
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ class YappiError(Exception):
8282
def _validate_sorttype(sort_type, list):
8383
sort_type = sort_type.lower()
8484
if sort_type not in list:
85-
raise YappiError("Invalid SortType parameter: '%s'" % (sort_type))
85+
raise YappiError(f"Invalid SortType parameter: '{sort_type}'")
8686
return sort_type
8787

8888

8989
def _validate_sortorder(sort_order):
9090
sort_order = sort_order.lower()
9191
if sort_order not in SORT_ORDERS:
92-
raise YappiError("Invalid SortOrder parameter: '%s'" % (sort_order))
92+
raise YappiError(f"Invalid SortOrder parameter: '{sort_order}'")
9393
return sort_order
9494

9595

9696
def _validate_columns(name, list):
9797
name = name.lower()
9898
if name not in list:
99-
raise YappiError("Invalid Column name: '%s'" % (name))
99+
raise YappiError(f"Invalid Column name: '{name}'")
100100

101101

102102
def _ctx_name_callback():
@@ -134,7 +134,7 @@ def _create_greenlet_callbacks():
134134
try:
135135
from greenlet import getcurrent
136136
except ImportError as exc:
137-
raise YappiError("'greenlet' import failed with: %s" % repr(exc))
137+
raise YappiError(f"'greenlet' import failed with: {repr(exc)}")
138138

139139
def _get_greenlet_id():
140140
curr_greenlet = getcurrent()
@@ -175,12 +175,12 @@ def module_matches(stat, modules):
175175

176176
if not isinstance(stat, YStat):
177177
raise YappiError(
178-
"Argument 'stat' shall be a YStat object. (%s)" % (stat)
178+
f"Argument 'stat' shall be a YStat object. ({stat})"
179179
)
180180

181181
if not isinstance(modules, list):
182182
raise YappiError(
183-
"Argument 'modules' is not a list object. (%s)" % (modules)
183+
f"Argument 'modules' is not a list object. ({modules})"
184184
)
185185

186186
if not len(modules):
@@ -192,7 +192,7 @@ def module_matches(stat, modules):
192192
modules = set(modules)
193193
for module in modules:
194194
if not isinstance(module, types.ModuleType):
195-
raise YappiError("Non-module item in 'modules'. (%s)" % (module))
195+
raise YappiError(f"Non-module item in 'modules'. ({module})")
196196
return inspect.getmodule(_fn_descriptor_dict[stat.full_name]) in modules
197197

198198

@@ -211,12 +211,12 @@ def func_matches(stat, funcs):
211211

212212
if not isinstance(stat, YStat):
213213
raise YappiError(
214-
"Argument 'stat' shall be a YStat object. (%s)" % (stat)
214+
f"Argument 'stat' shall be a YStat object. ({stat})"
215215
)
216216

217217
if not isinstance(funcs, list):
218218
raise YappiError(
219-
"Argument 'funcs' is not a list object. (%s)" % (funcs)
219+
f"Argument 'funcs' is not a list object. ({funcs})"
220220
)
221221

222222
if not len(funcs):
@@ -228,7 +228,7 @@ def func_matches(stat, funcs):
228228
funcs = set(funcs)
229229
for func in funcs.copy():
230230
if not callable(func):
231-
raise YappiError("Non-callable item in 'funcs'. (%s)" % (func))
231+
raise YappiError(f"Non-callable item in 'funcs'. ({func})")
232232

233233
# If there is no CodeObject found, use func itself. It might be a
234234
# method descriptor, builtin func..etc.
@@ -877,8 +877,7 @@ def _add_from_YSTAT(self, file):
877877
saved_stats, saved_clock_type = pickle.load(file)
878878
except:
879879
raise YappiError(
880-
"Unable to load the saved profile information from %s." %
881-
(file.name)
880+
f"Unable to load the saved profile information from {file.name}."
882881
)
883882

884883
# check if we really have some stats to be merged?
@@ -996,7 +995,7 @@ def add(self, files, type="ystat"):
996995
]
997996
for fd in files:
998997
with open(fd, "rb") as f:
999-
add_func = getattr(self, "_add_from_%s" % (type))
998+
add_func = getattr(self, f"_add_from_{type}")
1000999
add_func(file=f)
10011000

10021001
return self.sort(DEFAULT_SORT_TYPE, DEFAULT_SORT_ORDER)
@@ -1005,10 +1004,10 @@ def save(self, path, type="ystat"):
10051004
type = type.upper()
10061005
if type not in self._SUPPORTED_SAVE_FORMATS:
10071006
raise NotImplementedError(
1008-
'Saving in "%s" format is not possible currently.' % (type)
1007+
f'Saving in "{type}" format is not possible currently.'
10091008
)
10101009

1011-
save_func = getattr(self, "_save_as_%s" % (type))
1010+
save_func = getattr(self, f"_save_as_{type}")
10121011
save_func(path=path)
10131012

10141013
def print_all(
@@ -1032,7 +1031,7 @@ def print_all(
10321031
_validate_columns(col[0], COLUMNS_FUNCSTATS)
10331032

10341033
out.write(LINESEP)
1035-
out.write("Clock type: %s" % (self._clock_type.upper()))
1034+
out.write(f"Clock type: {self._clock_type.upper()}")
10361035
out.write(LINESEP)
10371036
out.write(f"Ordered by: {self._sort_type}, {self._sort_order}")
10381037
out.write(LINESEP)
@@ -1062,13 +1061,13 @@ def debug_print(self):
10621061
for stat in self:
10631062
console.write("index: %d" % stat.index)
10641063
console.write(LINESEP)
1065-
console.write("full_name: %s" % stat.full_name)
1064+
console.write(f"full_name: {stat.full_name}")
10661065
console.write(LINESEP)
10671066
console.write("ncall: %d/%d" % (stat.ncall, stat.nactualcall))
10681067
console.write(LINESEP)
1069-
console.write("ttot: %s" % _fft(stat.ttot))
1068+
console.write(f"ttot: {_fft(stat.ttot)}")
10701069
console.write(LINESEP)
1071-
console.write("tsub: %s" % _fft(stat.tsub))
1070+
console.write(f"tsub: {_fft(stat.tsub)}")
10721071
console.write(LINESEP)
10731072
console.write("children: ")
10741073
console.write(LINESEP)
@@ -1078,18 +1077,18 @@ def debug_print(self):
10781077
console.write("index: %d" % child_stat.index)
10791078
console.write(LINESEP)
10801079
console.write(" " * CHILD_STATS_LEFT_MARGIN)
1081-
console.write("child_full_name: %s" % child_stat.full_name)
1080+
console.write(f"child_full_name: {child_stat.full_name}")
10821081
console.write(LINESEP)
10831082
console.write(" " * CHILD_STATS_LEFT_MARGIN)
10841083
console.write(
10851084
"ncall: %d/%d" % (child_stat.ncall, child_stat.nactualcall)
10861085
)
10871086
console.write(LINESEP)
10881087
console.write(" " * CHILD_STATS_LEFT_MARGIN)
1089-
console.write("ttot: %s" % _fft(child_stat.ttot))
1088+
console.write(f"ttot: {_fft(child_stat.ttot)}")
10901089
console.write(LINESEP)
10911090
console.write(" " * CHILD_STATS_LEFT_MARGIN)
1092-
console.write("tsub: %s" % _fft(child_stat.tsub))
1091+
console.write(f"tsub: {_fft(child_stat.tsub)}")
10931092
console.write(LINESEP)
10941093
console.write(LINESEP)
10951094

@@ -1353,7 +1352,7 @@ def set_clock_type(type):
13531352
"""
13541353
type = type.upper()
13551354
if type not in CLOCK_TYPES:
1356-
raise YappiError("Invalid clock type:%s" % (type))
1355+
raise YappiError(f"Invalid clock type:{type}")
13571356

13581357
_yappi.set_clock_type(CLOCK_TYPES[type])
13591358

@@ -1391,7 +1390,7 @@ def set_context_backend(type):
13911390
"""
13921391
type = type.upper()
13931392
if type not in BACKEND_TYPES:
1394-
raise YappiError("Invalid backend type: %s" % (type))
1393+
raise YappiError(f"Invalid backend type: {type}")
13951394

13961395
if type == GREENLET:
13971396
id_cbk, name_cbk = _create_greenlet_callbacks()

0 commit comments

Comments
 (0)
Please sign in to comment.