From b444e6355ec50417546f42138bb833d1e9e8b306 Mon Sep 17 00:00:00 2001 From: David Lord Date: Sun, 19 Apr 2020 22:22:49 -0700 Subject: apply pyupgrade --py36-plus --- examples/aliases/aliases.py | 6 ++--- examples/bashcompletion/bashcompletion.py | 4 +-- examples/colors/colors.py | 10 +++---- examples/complex/complex/cli.py | 6 ++--- examples/imagepipe/imagepipe.py | 43 ++++++++++++++----------------- examples/naval/naval.py | 10 +++---- examples/repo/repo.py | 18 ++++++------- examples/termui/termui.py | 15 +++++------ examples/validation/validation.py | 6 ++--- 9 files changed, 53 insertions(+), 65 deletions(-) (limited to 'examples') diff --git a/examples/aliases/aliases.py b/examples/aliases/aliases.py index bf09500..c01672b 100644 --- a/examples/aliases/aliases.py +++ b/examples/aliases/aliases.py @@ -4,7 +4,7 @@ import os import click -class Config(object): +class Config: """The config in this example only holds aliases.""" def __init__(self): @@ -121,7 +121,7 @@ def commit(): @pass_config def status(config): """Shows the status.""" - click.echo("Status for {}".format(config.path)) + click.echo(f"Status for {config.path}") @cli.command() @@ -135,4 +135,4 @@ def alias(config, alias_, cmd, config_file): """Adds an alias to the specified configuration file.""" config.add_alias(alias_, cmd) config.write_config(config_file) - click.echo("Added '{}' as alias for '{}'".format(alias_, cmd)) + click.echo(f"Added '{alias_}' as alias for '{cmd}'") diff --git a/examples/bashcompletion/bashcompletion.py b/examples/bashcompletion/bashcompletion.py index 0502dbc..592bf19 100644 --- a/examples/bashcompletion/bashcompletion.py +++ b/examples/bashcompletion/bashcompletion.py @@ -18,7 +18,7 @@ def get_env_vars(ctx, args, incomplete): @cli.command(help="A command to print environment variables") @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars) def cmd1(envvar): - click.echo("Environment variable: {}".format(envvar)) + click.echo(f"Environment variable: {envvar}") click.echo("Value: {}".format(os.environ[envvar])) @@ -39,7 +39,7 @@ def list_users(ctx, args, incomplete): @group.command(help="Choose a user") @click.argument("user", type=click.STRING, autocompletion=list_users) def subcmd(user): - click.echo("Chosen user is {}".format(user)) + click.echo(f"Chosen user is {user}") cli.add_command(group) diff --git a/examples/colors/colors.py b/examples/colors/colors.py index 012538d..cb7af31 100644 --- a/examples/colors/colors.py +++ b/examples/colors/colors.py @@ -30,15 +30,11 @@ def cli(): Give it a try! """ for color in all_colors: - click.echo(click.style("I am colored {}".format(color), fg=color)) + click.echo(click.style(f"I am colored {color}", fg=color)) for color in all_colors: - click.echo( - click.style("I am colored {} and bold".format(color), fg=color, bold=True) - ) + click.echo(click.style(f"I am colored {color} and bold", fg=color, bold=True)) for color in all_colors: - click.echo( - click.style("I am reverse colored {}".format(color), fg=color, reverse=True) - ) + click.echo(click.style(f"I am reverse colored {color}", fg=color, reverse=True)) click.echo(click.style("I am blinking", blink=True)) click.echo(click.style("I am underlined", underline=True)) diff --git a/examples/complex/complex/cli.py b/examples/complex/complex/cli.py index bb43613..5d00dba 100644 --- a/examples/complex/complex/cli.py +++ b/examples/complex/complex/cli.py @@ -7,7 +7,7 @@ import click CONTEXT_SETTINGS = dict(auto_envvar_prefix="COMPLEX") -class Environment(object): +class Environment: def __init__(self): self.verbose = False self.home = os.getcwd() @@ -39,9 +39,7 @@ class ComplexCLI(click.MultiCommand): def get_command(self, ctx, name): try: - mod = __import__( - "complex.commands.cmd_{}".format(name), None, None, ["cli"] - ) + mod = __import__(f"complex.commands.cmd_{name}", None, None, ["cli"]) except ImportError: return return mod.cli diff --git a/examples/imagepipe/imagepipe.py b/examples/imagepipe/imagepipe.py index d46c33f..95f5c42 100644 --- a/examples/imagepipe/imagepipe.py +++ b/examples/imagepipe/imagepipe.py @@ -60,10 +60,8 @@ def generator(f): @processor def new_func(stream, *args, **kwargs): - for item in stream: - yield item - for item in f(*args, **kwargs): - yield item + yield from stream + yield from f(*args, **kwargs) return update_wrapper(new_func, f) @@ -89,7 +87,7 @@ def open_cmd(images): """ for image in images: try: - click.echo("Opening '{}'".format(image)) + click.echo(f"Opening '{image}'") if image == "-": img = Image.open(click.get_binary_stdin()) img.filename = "-" @@ -97,7 +95,7 @@ def open_cmd(images): img = Image.open(image) yield img except Exception as e: - click.echo("Could not open image '{}': {}".format(image, e), err=True) + click.echo(f"Could not open image '{image}': {e}", err=True) @cli.command("save") @@ -114,12 +112,10 @@ def save_cmd(images, filename): for idx, image in enumerate(images): try: fn = filename.format(idx + 1) - click.echo("Saving '{}' as '{}'".format(image.filename, fn)) + click.echo(f"Saving '{image.filename}' as '{fn}'") yield image.save(fn) except Exception as e: - click.echo( - "Could not save image '{}': {}".format(image.filename, e), err=True - ) + click.echo(f"Could not save image '{image.filename}': {e}", err=True) @cli.command("display") @@ -127,7 +123,7 @@ def save_cmd(images, filename): def display_cmd(images): """Opens all images in an image viewer.""" for image in images: - click.echo("Displaying '{}'".format(image.filename)) + click.echo(f"Displaying '{image.filename}'") image.show() yield image @@ -142,7 +138,7 @@ def resize_cmd(images, width, height): """ for image in images: w, h = (width or image.size[0], height or image.size[1]) - click.echo("Resizing '{}' to {}x{}".format(image.filename, w, h)) + click.echo(f"Resizing '{image.filename}' to {w}x{h}") image.thumbnail((w, h)) yield image @@ -160,7 +156,7 @@ def crop_cmd(images, border): if border is not None: for idx, val in enumerate(box): box[idx] = max(0, val - border) - click.echo("Cropping '{}' by {}px".format(image.filename, border)) + click.echo(f"Cropping '{image.filename}' by {border}px") yield copy_filename(image.crop(box), image) else: yield image @@ -176,7 +172,7 @@ def convert_rotation(ctx, param, value): return (Image.ROTATE_180, 180) if value in ("-90", "270", "l", "left"): return (Image.ROTATE_270, 270) - raise click.BadParameter("invalid rotation '{}'".format(value)) + raise click.BadParameter(f"invalid rotation '{value}'") def convert_flip(ctx, param, value): @@ -187,7 +183,7 @@ def convert_flip(ctx, param, value): return (Image.FLIP_LEFT_RIGHT, "left to right") if value in ("tb", "topbottom", "upsidedown", "ud"): return (Image.FLIP_LEFT_RIGHT, "top to bottom") - raise click.BadParameter("invalid flip '{}'".format(value)) + raise click.BadParameter(f"invalid flip '{value}'") @cli.command("transpose") @@ -201,11 +197,11 @@ def transpose_cmd(images, rotate, flip): for image in images: if rotate is not None: mode, degrees = rotate - click.echo("Rotate '{}' by {}deg".format(image.filename, degrees)) + click.echo(f"Rotate '{image.filename}' by {degrees}deg") image = copy_filename(image.transpose(mode), image) if flip is not None: mode, direction = flip - click.echo("Flip '{}' {}".format(image.filename, direction)) + click.echo(f"Flip '{image.filename}' {direction}") image = copy_filename(image.transpose(mode), image) yield image @@ -217,7 +213,7 @@ def blur_cmd(images, radius): """Applies gaussian blur.""" blur = ImageFilter.GaussianBlur(radius) for image in images: - click.echo("Blurring '{}' by {}px".format(image.filename, radius)) + click.echo(f"Blurring '{image.filename}' by {radius}px") yield copy_filename(image.filter(blur), image) @@ -248,7 +244,7 @@ def smoothen_cmd(images, iterations): def emboss_cmd(images): """Embosses an image.""" for image in images: - click.echo("Embossing '{}'".format(image.filename)) + click.echo(f"Embossing '{image.filename}'") yield copy_filename(image.filter(ImageFilter.EMBOSS), image) @@ -260,7 +256,7 @@ def emboss_cmd(images): def sharpen_cmd(images, factor): """Sharpens an image.""" for image in images: - click.echo("Sharpen '{}' by {}".format(image.filename, factor)) + click.echo(f"Sharpen '{image.filename}' by {factor}") enhancer = ImageEnhance.Sharpness(image) yield copy_filename(enhancer.enhance(max(1.0, factor)), image) @@ -282,13 +278,12 @@ def paste_cmd(images, left, right): yield image return - click.echo("Paste '{}' on '{}'".format(to_paste.filename, image.filename)) + click.echo(f"Paste '{to_paste.filename}' on '{image.filename}'") mask = None if to_paste.mode == "RGBA" or "transparency" in to_paste.info: mask = to_paste image.paste(to_paste, (left, right), mask) - image.filename += "+{}".format(to_paste.filename) + image.filename += f"+{to_paste.filename}" yield image - for image in imageiter: - yield image + yield from imageiter diff --git a/examples/naval/naval.py b/examples/naval/naval.py index b8d31e1..7310e6d 100644 --- a/examples/naval/naval.py +++ b/examples/naval/naval.py @@ -21,7 +21,7 @@ def ship(): @click.argument("name") def ship_new(name): """Creates a new ship.""" - click.echo("Created ship {}".format(name)) + click.echo(f"Created ship {name}") @ship.command("move") @@ -31,7 +31,7 @@ def ship_new(name): @click.option("--speed", metavar="KN", default=10, help="Speed in knots.") def ship_move(ship, x, y, speed): """Moves SHIP to the new location X,Y.""" - click.echo("Moving ship {} to {},{} with speed {}".format(ship, x, y, speed)) + click.echo(f"Moving ship {ship} to {x},{y} with speed {speed}") @ship.command("shoot") @@ -40,7 +40,7 @@ def ship_move(ship, x, y, speed): @click.argument("y", type=float) def ship_shoot(ship, x, y): """Makes SHIP fire to X,Y.""" - click.echo("Ship {} fires to {},{}".format(ship, x, y)) + click.echo(f"Ship {ship} fires to {x},{y}") @cli.group("mine") @@ -61,7 +61,7 @@ def mine(): @click.option("ty", "--drifting", flag_value="drifting", help="Drifting mine.") def mine_set(x, y, ty): """Sets a mine at a specific coordinate.""" - click.echo("Set {} mine at {},{}".format(ty, x, y)) + click.echo(f"Set {ty} mine at {x},{y}") @mine.command("remove") @@ -69,4 +69,4 @@ def mine_set(x, y, ty): @click.argument("y", type=float) def mine_remove(x, y): """Removes a mine at a specific coordinate.""" - click.echo("Removed mine at {},{}".format(x, y)) + click.echo(f"Removed mine at {x},{y}") diff --git a/examples/repo/repo.py b/examples/repo/repo.py index 5fd6ead..ff44d55 100644 --- a/examples/repo/repo.py +++ b/examples/repo/repo.py @@ -5,7 +5,7 @@ import sys import click -class Repo(object): +class Repo: def __init__(self, home): self.home = home self.config = {} @@ -14,10 +14,10 @@ class Repo(object): def set_config(self, key, value): self.config[key] = value if self.verbose: - click.echo(" config[{}] = {}".format(key, value), file=sys.stderr) + click.echo(f" config[{key}] = {value}", file=sys.stderr) def __repr__(self): - return "".format(self.home) + return f"" pass_repo = click.make_pass_decorator(Repo) @@ -82,7 +82,7 @@ def clone(repo, src, dest, shallow, rev): repo.home = dest if shallow: click.echo("Making shallow checkout") - click.echo("Checking out revision {}".format(rev)) + click.echo(f"Checking out revision {rev}") @cli.command() @@ -93,7 +93,7 @@ def delete(repo): This will throw away the current repository. """ - click.echo("Destroying repo {}".format(repo.home)) + click.echo(f"Destroying repo {repo.home}") click.echo("Deleted!") @@ -136,7 +136,7 @@ def commit(repo, files, message): marker = "# Files to be committed:" hint = ["", "", marker, "#"] for file in files: - hint.append("# U {}".format(file)) + hint.append(f"# U {file}") message = click.edit("\n".join(hint)) if message is None: click.echo("Aborted!") @@ -147,8 +147,8 @@ def commit(repo, files, message): return else: msg = "\n".join(message) - click.echo("Files to be committed: {}".format(files)) - click.echo("Commit message:\n{}".format(msg)) + click.echo(f"Files to be committed: {files}") + click.echo(f"Commit message:\n{msg}") @cli.command(short_help="Copies files.") @@ -163,4 +163,4 @@ def copy(repo, src, dst, force): files from SRC to DST. """ for fn in src: - click.echo("Copy from {} -> {}".format(fn, dst)) + click.echo(f"Copy from {fn} -> {dst}") diff --git a/examples/termui/termui.py b/examples/termui/termui.py index 7b3da43..b772f13 100644 --- a/examples/termui/termui.py +++ b/examples/termui/termui.py @@ -1,4 +1,3 @@ -# coding: utf-8 import math import random import time @@ -16,8 +15,8 @@ def cli(): def colordemo(): """Demonstrates ANSI color support.""" for color in "red", "green", "blue": - click.echo(click.style("I am colored {}".format(color), fg=color)) - click.echo(click.style("I am background colored {}".format(color), bg=color)) + click.echo(click.style(f"I am colored {color}", fg=color)) + click.echo(click.style(f"I am background colored {color}", bg=color)) @cli.command() @@ -56,7 +55,7 @@ def progress(count): def show_item(item): if item is not None: - return "Item #{}".format(item) + return f"Item #{item}" with click.progressbar( filter(items), @@ -71,7 +70,7 @@ def progress(count): length=count, label="Counting", bar_template="%(label)s %(bar)s | %(info)s", - fill_char=click.style(u"█", fg="cyan"), + fill_char=click.style("█", fg="cyan"), empty_char=" ", ) as bar: for item in bar: @@ -94,7 +93,7 @@ def progress(count): length=count, show_percent=False, label="Slowing progress bar", - fill_char=click.style(u"█", fg="green"), + fill_char=click.style("█", fg="green"), ) as bar: for item in steps: time.sleep(item) @@ -119,13 +118,13 @@ def locate(url): def edit(): """Opens an editor with some text in it.""" MARKER = "# Everything below is ignored\n" - message = click.edit("\n\n{}".format(MARKER)) + message = click.edit(f"\n\n{MARKER}") if message is not None: msg = message.split(MARKER, 1)[0].rstrip("\n") if not msg: click.echo("Empty message!") else: - click.echo("Message:\n{}".format(msg)) + click.echo(f"Message:\n{msg}") else: click.echo("You did not enter anything!") diff --git a/examples/validation/validation.py b/examples/validation/validation.py index 3ca5745..6f87eb0 100644 --- a/examples/validation/validation.py +++ b/examples/validation/validation.py @@ -44,6 +44,6 @@ def cli(count, foo, url): 'If a value is provided it needs to be the value "wat".', param_hint=["--foo"], ) - click.echo("count: {}".format(count)) - click.echo("foo: {}".format(foo)) - click.echo("url: {!r}".format(url)) + click.echo(f"count: {count}") + click.echo(f"foo: {foo}") + click.echo(f"url: {url!r}") -- cgit v1.2.1