Skip to content

Commit

Permalink
Begin removing .format in favor of f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dbieber committed Sep 20, 2024
1 parent ca4e80b commit da2fa86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ the program to the command line.
import fire

def hello(name):
return 'Hello {name}!'.format(name=name)
return f'Hello {name}!'

if __name__ == '__main__':
fire.Fire()
Expand All @@ -52,7 +52,7 @@ command line.
import fire

def hello(name):
return 'Hello {name}!'.format(name=name)
return f'Hello {name}!'

if __name__ == '__main__':
fire.Fire(hello)
Expand All @@ -76,7 +76,7 @@ We can alternatively write this program like this:
import fire

def hello(name):
return 'Hello {name}!'.format(name=name)
return f'Hello {name}!'

def main():
fire.Fire(hello)
Expand All @@ -93,7 +93,7 @@ then simply this:
import fire

def hello(name):
return 'Hello {name}!'.format(name=name)
return f'Hello {name}!'

def main():
fire.Fire(hello)
Expand All @@ -105,7 +105,7 @@ If you have a file `example.py` that doesn't even import fire:

```python
def hello(name):
return 'Hello {name}!'.format(name=name)
return f'Hello {name}!'
```

Then you can use it with Fire like this:
Expand Down
32 changes: 16 additions & 16 deletions fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ def Fire(component=None, command=None, name=None, serialize=None):
_DisplayError(component_trace)
raise FireExit(2, component_trace)
if component_trace.show_trace and component_trace.show_help:
output = ['Fire trace:\n{trace}\n'.format(trace=component_trace)]
output = [f'Fire trace:\n{component_trace}\n']
result = component_trace.GetResult()
help_text = helptext.HelpText(
result, trace=component_trace, verbose=component_trace.verbose)
output.append(help_text)
Display(output, out=sys.stderr)
raise FireExit(0, component_trace)
if component_trace.show_trace:
output = ['Fire trace:\n{trace}'.format(trace=component_trace)]
output = [f'Fire trace:\n{component_trace}']
Display(output, out=sys.stderr)
raise FireExit(0, component_trace)
if component_trace.show_help:
Expand Down Expand Up @@ -231,9 +231,9 @@ def _IsHelpShortcut(component_trace, remaining_args):

if show_help:
component_trace.show_help = True
command = '{cmd} -- --help'.format(cmd=component_trace.GetCommand())
print('INFO: Showing help with the command {cmd}.\n'.format(
cmd=shlex.quote(command)), file=sys.stderr)
command = f'{component_trace.GetCommand()} -- --help'
print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
file=sys.stderr)
return show_help


Expand Down Expand Up @@ -287,9 +287,9 @@ def _DisplayError(component_trace):
show_help = True

if show_help:
command = '{cmd} -- --help'.format(cmd=component_trace.GetCommand())
print('INFO: Showing help with the command {cmd}.\n'.format(
cmd=shlex.quote(command)), file=sys.stderr)
command = f'{component_trace.GetCommand()} -- --help'
print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
file=sys.stderr)
help_text = helptext.HelpText(result, trace=component_trace,
verbose=component_trace.verbose)
output.append(help_text)
Expand Down Expand Up @@ -327,14 +327,13 @@ def _DictAsString(result, verbose=False):
return '{}'

longest_key = max(len(str(key)) for key in result_visible.keys())
format_string = '{{key:{padding}s}} {{value}}'.format(padding=longest_key + 1)
format_string = f'{{key:{longest_key + 1}s}} {{value}}'

lines = []
for key, value in result.items():
if completion.MemberVisible(result, key, value, class_attrs=class_attrs,
verbose=verbose):
line = format_string.format(key=str(key) + ':',
value=_OneLineResult(value))
line = format_string.format(key=f'{key}:', value=_OneLineResult(value))
lines.append(line)
return '\n'.join(lines)

Expand All @@ -348,10 +347,10 @@ def _OneLineResult(result):
# TODO(dbieber): Show a small amount of usage information about the function
# or module if it fits cleanly on the line.
if inspect.isfunction(result):
return '<function {name}>'.format(name=result.__name__)
return f'<function {result.__name__}>'

if inspect.ismodule(result):
return '<module {name}>'.format(name=result.__name__)
return f'<module {result.__name__}>'

try:
# Don't force conversion to ascii.
Expand Down Expand Up @@ -890,9 +889,10 @@ def _ParseKeywordArgs(args, fn_spec):
if len(matching_fn_args) == 1:
keyword = matching_fn_args[0]
elif len(matching_fn_args) > 1:
raise FireError("The argument '{}' is ambiguous as it could "
"refer to any of the following arguments: {}".format(
argument, matching_fn_args))
raise FireError(
f"The argument '{argument}' is ambiguous as it could "
f"refer to any of the following arguments: {matching_fn_args}"
)

# Determine the value.
if not keyword:
Expand Down

0 comments on commit da2fa86

Please sign in to comment.