Skip to content

Used f-strings instead of percent formatting. #1934

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

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions PROJECTS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Shells:
- `saws <https://github.com/donnemartin/saws>`_: A Supercharged AWS Command Line Interface.
- `cycli <https://github.com/nicolewhite/cycli>`_: A Command Line Interface for Cypher.
- `crash <https://github.com/crate/crash>`_: Crate command line client.
- `vcli <https://github.com/dbcli/vcli>`_: Vertica client.
- `vcli <https://github.com/dbcli/vcli>`_: Vertical client.
- `aws-shell <https://github.com/awslabs/aws-shell>`_: An integrated shell for working with the AWS CLI.
- `softlayer-python <https://github.com/softlayer/softlayer-python>`_: A command-line interface to manage various SoftLayer products and services.
- `ipython <http://github.com/ipython/ipython/>`_: The IPython REPL
Expand Down Expand Up @@ -52,7 +52,7 @@ Full screen applications:
- `sanctuary-zero <https://github.com/t0xic0der/sanctuary-zero>`_: A secure chatroom with zero logging and total transience.
- `Hummingbot <https://github.com/CoinAlpha/hummingbot>`_: A Cryptocurrency Algorithmic Trading Platform
- `git-bbb <https://github.com/MrMino/git-bbb>`_: A `git blame` browser.
- `ass <https://github.com/mlang/ass`_: An OpenAI Assistants API client.
- `ass <https://github.com/mlang/ass>`_: An OpenAI Assistants API client.

Libraries:

Expand Down
26 changes: 13 additions & 13 deletions docs/pages/asking_for_input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and returns the text. Just like ``(raw_)input``.
from prompt_toolkit import prompt

text = prompt('Give me some input: ')
print('You said: %s' % text)
print(f'You said: {text}')

.. image:: ../images/hello-world-prompt.png

Expand Down Expand Up @@ -86,7 +86,7 @@ base class.
from prompt_toolkit.lexers import PygmentsLexer

text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer))
print('You said: %s' % text)
print(f'You said: {text}')

.. image:: ../images/html-input.png

Expand All @@ -105,7 +105,7 @@ you can do the following:
style = style_from_pygments_cls(get_style_by_name('monokai'))
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer), style=style,
include_default_pygments_style=False)
print('You said: %s' % text)
print(f'You said: {text}')

We pass ``include_default_pygments_style=False``, because otherwise, both
styles will be merged, possibly giving slightly different colors in the outcome
Expand Down Expand Up @@ -266,7 +266,7 @@ a completer that implements that interface.

html_completer = WordCompleter(['<html>', '<body>', '<head>', '<title>'])
text = prompt('Enter HTML: ', completer=html_completer)
print('You said: %s' % text)
print(f'You said: {text}')

:class:`~prompt_toolkit.completion.WordCompleter` is a simple completer that
completes the last word before the cursor with any of the given words.
Expand Down Expand Up @@ -310,7 +310,7 @@ levels. :class:`~prompt_toolkit.completion.NestedCompleter` solves this issue:
})

text = prompt('# ', completer=completer)
print('You said: %s' % text)
print(f'You said: {text}')

Whenever there is a ``None`` value in the dictionary, it means that there is no
further nested completion at that point. When all values of a dictionary would
Expand Down Expand Up @@ -476,7 +476,7 @@ takes a :class:`~prompt_toolkit.document.Document` as input and raises
cursor_position=i)

number = int(prompt('Give a number: ', validator=NumberValidator()))
print('You said: %i' % number)
print(f'You said: {number}')

.. image:: ../images/number-validator.png

Expand Down Expand Up @@ -515,7 +515,7 @@ follows:
move_cursor_to_end=True)

number = int(prompt('Give a number: ', validator=validator))
print('You said: %i' % number)
print(f'You said: {number}')

We define a function that takes a string, and tells whether it's valid input or
not by returning a boolean.
Expand Down Expand Up @@ -592,7 +592,7 @@ Example:

while True:
text = session.prompt('> ', auto_suggest=AutoSuggestFromHistory())
print('You said: %s' % text)
print(f'You said: {text}')

.. image:: ../images/auto-suggestion.png

Expand Down Expand Up @@ -627,7 +627,7 @@ of the foreground.
return HTML('This is a <b><style bg="ansired">Toolbar</style></b>!')

text = prompt('> ', bottom_toolbar=bottom_toolbar)
print('You said: %s' % text)
print(f'You said: {text}')

.. image:: ../images/bottom-toolbar.png

Expand All @@ -646,7 +646,7 @@ Similar, we could use a list of style/text tuples.
})

text = prompt('> ', bottom_toolbar=bottom_toolbar, style=style)
print('You said: %s' % text)
print(f'You said: {text}')

The default class name is ``bottom-toolbar`` and that will also be used to fill
the background of the toolbar.
Expand Down Expand Up @@ -734,7 +734,7 @@ An example of a prompt that prints ``'hello world'`` when :kbd:`Control-T` is pr
event.app.exit()

text = prompt('> ', key_bindings=bindings)
print('You said: %s' % text)
print(f'You said: {text}')


Note that we use
Expand Down Expand Up @@ -892,7 +892,7 @@ A default value can be given:
from prompt_toolkit import prompt
import getpass

prompt('What is your name: ', default='%s' % getpass.getuser())
prompt('What is your name: ', default=f"{getpass.getuser()}")


Mouse support
Expand Down Expand Up @@ -990,7 +990,7 @@ returns a coroutines and is awaitable.
while True:
with patch_stdout():
result = await session.prompt_async('Say something: ')
print('You said: %s' % result)
print(f'You said: {result}')

The :func:`~prompt_toolkit.patch_stdout.patch_stdout` context manager is
optional, but it's recommended, because other coroutines could print to stdout.
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ and returns the text. Just like ``(raw_)input``.
from prompt_toolkit import prompt

text = prompt('Give me some input: ')
print('You said: %s' % text)
print(f'You said: {text}')


Learning `prompt_toolkit`
Expand Down
9 changes: 3 additions & 6 deletions examples/full-screen/simple-demos/line-prefixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@

def get_line_prefix(lineno, wrap_count):
if wrap_count == 0:
return HTML('[%s] <style bg="orange" fg="black">--&gt;</style> ') % lineno

return HTML(f'[{lineno}] <style bg="orange" fg="black">--&gt;</style> ')
text = str(lineno) + "-" + "*" * (lineno // 2) + ": "
return HTML('[%s.%s] <style bg="ansigreen" fg="ansiblack">%s</style>') % (
lineno,
wrap_count,
text,
return HTML(
f'[{lineno}.{wrap_count}] <style bg="ansigreen" fg="ansiblack">{text}</style>'
)


Expand Down
2 changes: 1 addition & 1 deletion examples/progress-bar/a-lot-of-parallel-tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def stop_task(label, total, sleep_time):
threads = []

for i in range(160):
label = "Task %i" % i
label = f"Task {i}"
total = random.randrange(50, 200)
sleep_time = random.randrange(5, 20) / 100.0

Expand Down
2 changes: 1 addition & 1 deletion examples/prompts/asyncio-prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def print_counter():
try:
i = 0
while True:
print("Counter: %i" % i)
print(f"Counter: {i}")
i += 1
await asyncio.sleep(3)
except asyncio.CancelledError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ def get_completions(self, document, complete_event):
family_color = family_colors.get(family, "default")

display = HTML(
"%s<b>:</b> <ansired>(<"
f"{animal}<b>:</b> <ansired>(<"
+ family_color
+ ">%s</"
+ f">{family}</"
+ family_color
+ ">)</ansired>"
) % (animal, family)
)
else:
display = animal

Expand Down
6 changes: 3 additions & 3 deletions examples/prompts/fancy-zsh-prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def get_prompt() -> HTML:
"<right-part> "
"<branch> master<exclamation-mark>!</exclamation-mark> </branch> "
" <env> py36 </env> "
" <time>%s</time> "
f" <time>{datetime.datetime.now().isoformat()}</time> "
"</right-part>"
) % (datetime.datetime.now().isoformat(),)
)

used_width = sum(
[
Expand All @@ -65,7 +65,7 @@ def get_prompt() -> HTML:
total_width = get_app().output.get_size().columns
padding_size = total_width - used_width

padding = HTML("<padding>%s</padding>") % (" " * padding_size,)
padding = HTML(f"<padding>{" " * padding_size}</padding>")

return merge_formatted_text([left_part, padding, right_part, "\n", "# "])

Expand Down
4 changes: 2 additions & 2 deletions examples/prompts/get-multiline-input.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def prompt_continuation(width, line_number, wrap_count):
if wrap_count > 0:
return " " * (width - 3) + "-> "
else:
text = ("- %i - " % (line_number + 1)).rjust(width)
return HTML("<strong>%s</strong>") % text
text = (f"- {line_number + 1} - ").rjust(width)
return HTML(f"<strong>{text}</strong>")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/prompts/patch-stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def thread():
i = 0
while running:
i += 1
print("i=%i" % i)
print(f"i={i}")
time.sleep(1)

t = threading.Thread(target=thread)
Expand Down
11 changes: 4 additions & 7 deletions examples/prompts/swap-light-and-dark-colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ def bottom_toolbar():
else:
on = "on=false"

return (
HTML(
'Press <style bg="#222222" fg="#ff8888">[control-t]</style> '
"to swap between dark/light colors. "
'<style bg="ansiblack" fg="ansiwhite">[%s]</style>'
)
% on
return HTML(
'Press <style bg="#222222" fg="#ff8888">[control-t]</style> '
"to swap between dark/light colors. "
f'<style bg="ansiblack" fg="ansiwhite">[{on}]</style>'
)

text = prompt(
Expand Down
Loading