From 877361353d29c52e2fdb3695965ea08199f3be59 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 5 Sep 2023 11:15:27 +0100 Subject: [PATCH] ruff auto fixes --- api/__init__.py | 0 api/main.py | 10 +++++----- api/models.py | 9 ++++----- quackstack/colors.py | 7 +++---- quackstack/ducky.py | 17 ++++++++--------- quackstack/manducky.py | 37 ++++++++++++++++++------------------- 6 files changed, 38 insertions(+), 42 deletions(-) create mode 100644 api/__init__.py diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/main.py b/api/main.py index 53fdb0a..6bdd569 100644 --- a/api/main.py +++ b/api/main.py @@ -3,7 +3,7 @@ from os import getenv from pathlib import Path from time import time -from typing import Optional, Union +from typing import Union from fastapi import FastAPI, Request, Response from fastapi.exceptions import HTTPException @@ -42,7 +42,7 @@ def make_file_path(dh: str, request_url: URL) -> str: @app.get("/duck", response_model=DuckResponse, status_code=201) -async def get_duck(request: Request, response: Response, seed: Optional[int] = None) -> DuckResponse: +async def get_duck(request: Request, response: Response, seed: int | None = None) -> DuckResponse: """Create a new random duck, with an optional seed.""" dh = sha1(str(time()).encode()).hexdigest() file = CACHE / f"{dh}.png" @@ -74,7 +74,7 @@ async def post_duck(request: Request, response: Response, duck: DuckRequest = No @app.get("/manduck", response_model=DuckResponse, status_code=201) -async def get_man_duck(request: Request, response: Response, seed: Optional[int] = None) -> DuckResponse: +async def get_man_duck(request: Request, response: Response, seed: int | None = None) -> DuckResponse: """Create a new man_duck, with an optional seed.""" dh = sha1(str(time()).encode()).hexdigest() @@ -108,7 +108,7 @@ async def post_man_duck(request: Request, response: Response, manduck: ManDuckRe @app.get("/details/{type}", response_model=Union[ManDuckDetails, DuckyDetails]) -async def get_details(type: str) -> Union[ManDuckDetails, DuckyDetails]: +async def get_details(type: str) -> ManDuckDetails | DuckyDetails: """Get details about accessories which can be used to build ducks/man-ducks.""" details = { "ducky": DuckyDetails( @@ -127,7 +127,7 @@ async def get_details(type: str) -> Union[ManDuckDetails, DuckyDetails]: variation_2=list(ManDuckBuilder.EQUIPMENTS["variation_2"]), ), variations=list(ManDuckBuilder.VARIATIONS), - ) + ), } return details.get(type, Response("Requested type is not available", 400)) diff --git a/api/models.py b/api/models.py index e99b74f..e42fbbb 100644 --- a/api/models.py +++ b/api/models.py @@ -1,4 +1,3 @@ -from typing import Optional from pydantic import BaseModel @@ -25,15 +24,15 @@ class DressColors(BaseModel): """Valid options for a man ducky dress colors.""" shirt: PartOption - pants: Optional[PartOption] + pants: PartOption | None class Accessories(BaseModel): """Valid accessories for a duck.""" - hat: Optional[str] - outfit: Optional[str] - equipment: Optional[str] + hat: str | None + outfit: str | None + equipment: str | None class DuckRequest(BaseModel): diff --git a/quackstack/colors.py b/quackstack/colors.py index 63df031..7a79bf1 100644 --- a/quackstack/colors.py +++ b/quackstack/colors.py @@ -1,13 +1,12 @@ from collections import namedtuple from colorsys import hls_to_rgb, rgb_to_hls from random import Random -from typing import Tuple DuckyColors = namedtuple("DuckyColors", "eye eye_wing wing body beak") DressColors = namedtuple("DressColors", "shirt pants") -def make_color(random: Random, hue: float, dark_variant: bool) -> Tuple[float, float, float]: +def make_color(random: Random, hue: float, dark_variant: bool) -> tuple[float, float, float]: """Make a nice hls color to use in a duck.""" saturation = 1 lightness = random.uniform(.7, .85) @@ -53,10 +52,10 @@ def make_man_duck_colors(ducky: tuple) -> DressColors: second_varient = [((hls_[0] * 360 + 240) % 360) / 360, hls_[1], hls_[2]] first = tuple( - map(lambda x: round(x * 255), hls_to_rgb(first_varient[0], first_varient[1], first_varient[2])) + round(x * 255) for x in hls_to_rgb(first_varient[0], first_varient[1], first_varient[2]) ) second = tuple( - map(lambda x: round(x * 255), hls_to_rgb(second_varient[0], second_varient[1], second_varient[2])) + round(x * 255) for x in hls_to_rgb(second_varient[0], second_varient[1], second_varient[2]) ) return DressColors(first, second) diff --git a/quackstack/ducky.py b/quackstack/ducky.py index 6563003..66e0ee3 100644 --- a/quackstack/ducky.py +++ b/quackstack/ducky.py @@ -2,7 +2,6 @@ from collections import namedtuple from pathlib import Path from random import Random -from typing import Optional, Tuple from PIL import Image, ImageChops @@ -32,13 +31,13 @@ class DuckBuilder: filename.stem: filename for filename in (ASSETS_PATH / "accessories/outfits").iterdir() } - def __init__(self, seed: Optional[int] = None): + def __init__(self, seed: int | None = None) -> None: self.random = Random(seed) self.output: Image.Image = Image.new("RGBA", DUCK_SIZE, color=(0, 0, 0, 0)) def generate_template( - self, colors: DuckyColors, hat: str, outfit: str, equipment: str - ) -> Tuple[dict, DuckyColors, str, str, str]: + self, colors: DuckyColors, hat: str, outfit: str, equipment: str, + ) -> tuple[dict, DuckyColors, str, str, str]: """Generate a duck structure from given configuration.""" template = { "beak": (self.templates[5], colors.beak), @@ -57,14 +56,14 @@ def generate_template( return template, colors, hat, outfit, equipment - def generate_from_options(self, options: dict) -> Tuple[dict, DuckyColors, str, str, str]: + def generate_from_options(self, options: dict) -> tuple[dict, DuckyColors, str, str, str]: """Generate a duck from the provided request.""" return self.generate_template( colors=DuckyColors(**options["colors"]), - **options["accessories"] + **options["accessories"], ) - def generate(self, *, options: Optional[dict] = None) -> ProceduralDucky: + def generate(self, *, options: dict | None = None) -> ProceduralDucky: """Actually generate the ducky from the provided request, else generate a random one.""" if options: template, colors, hat, outfit, equipment = self.generate_from_options(options) @@ -73,7 +72,7 @@ def generate(self, *, options: Optional[dict] = None) -> ProceduralDucky: make_duck_colors(self.random), self.random.choice([*list(self.hats), None]), self.random.choice([*list(self.outfits), None]), - self.random.choice([*list(self.equipments), None]) + self.random.choice([*list(self.equipments), None]), ) for key in ["beak", "body", "eye", "equipment", "wing", "eye_wing", "hat", "outfit"]: @@ -82,7 +81,7 @@ def generate(self, *, options: Optional[dict] = None) -> ProceduralDucky: return ProceduralDucky(self.output, colors, hat, equipment, outfit) - def apply_layer(self, layer_path: str, recolor: Optional[Tuple[int, int, int]] = None) -> None: + def apply_layer(self, layer_path: str, recolor: tuple[int, int, int] | None = None) -> None: """Add the given layer on top of the ducky. Can be recolored with the recolor argument.""" try: layer = Image.open(layer_path) diff --git a/quackstack/manducky.py b/quackstack/manducky.py index a5cfa77..a615e7f 100644 --- a/quackstack/manducky.py +++ b/quackstack/manducky.py @@ -2,7 +2,6 @@ from collections import namedtuple from pathlib import Path from random import Random -from typing import Optional, Tuple from PIL import Image, ImageChops @@ -12,7 +11,7 @@ from .ducky import ProceduralDucky ManDucky = namedtuple("ManDucky", "image") -Color = Tuple[int, int, int] +Color = tuple[int, int, int] ASSETS_PATH = Path(qs_file).parent / Path("assets", "manduck") MAN_DUCKY_SIZE = (600, 1194) @@ -31,7 +30,7 @@ class ManDuckBuilder: }, "variation_2": { filename.stem: filename for filename in (ASSETS_PATH / "accessories/outfits/variation_2").iterdir() - } + }, } EQUIPMENTS = { "variation_1": { @@ -39,15 +38,15 @@ class ManDuckBuilder: }, "variation_2": { filename.stem: filename for filename in (ASSETS_PATH / "accessories/equipment/variation_2").iterdir() - } + }, } - def __init__(self, seed: Optional[int] = None): + def __init__(self, seed: int | None = None) -> None: self.random = Random(seed) self.output: Image.Image = Image.new("RGBA", MAN_DUCKY_SIZE, color=(0, 0, 0, 0)) def generate_template( - self, ducky: ProceduralDucky, dress_colors: DressColors, variation_: int + self, ducky: ProceduralDucky, dress_colors: DressColors, variation_: int, ) -> dict: """Generate a man duck structure from given configuration.""" variation = f"variation_{variation_}" @@ -55,35 +54,35 @@ def generate_template( template = { "bill": ( ASSETS_PATH / "templates/bill.png", - ducky.colors.beak + ducky.colors.beak, ), "head": ( ASSETS_PATH / "templates/head.png", - ducky.colors.body + ducky.colors.body, ), "eye": ( ASSETS_PATH / "templates/eye.png", - ducky.colors.eye + ducky.colors.eye, ), "hands": ( ASSETS_PATH / f"templates/{variation}/hands.png", - ducky.colors.wing - ) + ducky.colors.wing, + ), } if variation_ == 1: template["dress"] = ( ASSETS_PATH / "templates/variation_1/dress.png", - dress_colors.shirt + dress_colors.shirt, ) else: template["shirt"] = ( ASSETS_PATH / "templates/variation_2/shirt.png", - dress_colors.shirt + dress_colors.shirt, ) template["pants"] = ( ASSETS_PATH / "templates/variation_2/pants.png", - dress_colors.pants + dress_colors.pants, ) if ducky.hat: @@ -102,21 +101,21 @@ def generate_from_options(self, options: dict) -> dict: return self.generate_template( ducky=ProceduralDucky(None, colors, **accessories), dress_colors=DressColors(**options["dress_colors"]), - variation_=options["variation"] + variation_=options["variation"], ) def generate( self, *, - options: Optional[dict] = None, - ducky: Optional[ProceduralDucky] = None + options: dict | None = None, + ducky: ProceduralDucky | None = None, ) -> ManDucky: """Actually generate the man ducky from the provided request, else generate a random one..""" if options: template = self.generate_from_options(options) else: template = self.generate_template( - ducky, make_man_duck_colors(ducky.colors.body), self.random.choice(self.VARIATIONS) + ducky, make_man_duck_colors(ducky.colors.body), self.random.choice(self.VARIATIONS), ) for item in template.values(): @@ -124,7 +123,7 @@ def generate( return ManDucky(self.output) - def apply_layer(self, layer_path: str, recolor: Optional[Color] = None) -> None: + def apply_layer(self, layer_path: str, recolor: Color | None = None) -> None: """Add the given layer on top of the ducky. Can be recolored with the recolor argument.""" try: layer = Image.open(layer_path)