Formatting a regular string which could be a f-string
114 original_fps = video_track.original_frame_rate
115 subprocess.check_output([
116 "mkvmerge", "--output", out_path,
117 "--default-duration", "%d:%sfps" % (video_track.track_id - 1, original_fps),118 file_path
119 ], cwd=file_path.parent)
120 return out_path
Formatting a regular string which could be a f-string
109 # TODO: could be untrusted, user might just make a file named this
110 return out_path
111 if video_track.framerate_original_num and video_track.framerate_original_den:
112 original_fps = "%s/%s" % (video_track.framerate_original_num, video_track.framerate_original_den)113 else:
114 original_fps = video_track.original_frame_rate
115 subprocess.check_output([
Formatting a regular string which could be a f-string
96 prop_src=core.vivtc.VDecimate(clip, cycle=cycle, dryrun=True)
97 )
98 return core.vivtc.VDecimate(clip, cycle=cycle)
99 raise ValueError("pvsfunc.decimate: Incorrect mode (%d), it must be an int value between 0-1" % mode)
Formatting a regular string which could be a f-string
86 functools.partial(
87 lambda n, f, c: core.text.Text(
88 c,
89 " mode=%d cycle=%d \n"90 " Important: Please consider another mode. More information: git.io/avoid-tdecimate. \n"
91 " decimated_frame=%s \n" % (mode, cycle, f.props['VDecimateDrop'] == 1),
92 alignment=1
Formatting a regular string which could be a f-string
71 " mode=%d cycle=%d offsets=%s fps=%d/%d \n" % (
72 mode, cycle, offsets, res.fps.numerator, res.fps.denominator
73 ) +
74 " offset=%d decimate=%s \n" % (n % cycle, (n % cycle) not in offsets),75 alignment=1
76 ),
77 c=clip
Formatting a regular string which could be a f-string
68 functools.partial(
69 lambda n, f, c: core.text.Text(
70 c,
71 " mode=%d cycle=%d offsets=%s fps=%d/%d \n" % (72 mode, cycle, offsets, res.fps.numerator, res.fps.denominator
73 ) +
74 " offset=%d decimate=%s \n" % (n % cycle, (n % cycle) not in offsets),
Formatting a regular string which could be a f-string
20def calculate_aspect_ratio(width: int, height: int) -> str:
21 """Calculate the aspect-ratio gcd string from resolution."""
22 r = math.gcd(width, height)
23 return "%d:%d" % (int(width / r), int(height / r))24
25
26def calculate_par(width: int, height: int, aspect_ratio_w: int, aspect_ratio_h: int) -> str:
Formatting a regular string which could be a f-string
30 par_gcd = math.gcd(par_w, par_h)
31 par_w = int(par_w / par_gcd)
32 par_h = int(par_h / par_gcd)
33 return "%d:%d" % (par_w, par_h)34
35
36def list_select_every(data: list, cycle: int, offsets: (set, Iterable[int]), inverse: bool = False) -> list:
Description
f-strings are the fastest way to format strings as compared to the following methods:
- using format specifiers
%
- using
format()
- using
str.join
- using
+
operator to concatinate string - using
Template.substitute
Bad practice
Some less preferred ways to format strings are the following:
from string import Template
menu = ('eggs', 'spam', 42.4)
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = " and ".join(menu[:2])
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2])
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2])
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
Recommended
Consider using f-strings as shown below:
menu = ('eggs', 'spam', 42.4)
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"