From e942264698af61ea62d41f911fea299d86d52fc4 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 30 Oct 2021 14:56:10 +0000 Subject: [PATCH 1/2] Merge collapsible `with` statements --- minato_namikaze/bot_files/cogs/weather.py | 5 ++--- .../bot_files/lib/classes/games/typeracer.py | 13 ++++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/minato_namikaze/bot_files/cogs/weather.py b/minato_namikaze/bot_files/cogs/weather.py index 6eb52dae..70829c35 100644 --- a/minato_namikaze/bot_files/cogs/weather.py +++ b/minato_namikaze/bot_files/cogs/weather.py @@ -133,9 +133,8 @@ async def get_weather( params["q"] = str(location) url = "https://api.openweathermap.org/data/2.5/weather?{0}".format( urlencode(params)) - async with aiohttp.ClientSession() as session: - async with session.get(url) as resp: - data = await resp.json() + async with aiohttp.ClientSession() as session, session.get(url) as resp: + data = await resp.json() try: if data["message"] == "city not found": await ctx.send("City not found.") diff --git a/minato_namikaze/bot_files/lib/classes/games/typeracer.py b/minato_namikaze/bot_files/lib/classes/games/typeracer.py index a5410031..28ea72d9 100644 --- a/minato_namikaze/bot_files/lib/classes/games/typeracer.py +++ b/minato_namikaze/bot_files/lib/classes/games/typeracer.py @@ -332,13 +332,12 @@ async def start( ): if mode == "sentence": - async with aiohttp.ClientSession() as session: - async with session.get(self.SENTENCE_URL) as r: - if r.status in range(200, 299): - text = await r.json() - text = text["content"] - else: - return await ctx.send("Oops an error occured") + async with aiohttp.ClientSession() as session, session.get(self.SENTENCE_URL) as r: + if r.status in range(200, 299): + text = await r.json() + text = text["content"] + else: + return await ctx.send("Oops an error occured") elif mode == "random": text = " ".join( [random.choice(self.GRAMMAR_WORDS).lower() for _ in range(15)] From a4db9b4cdd89d503931858aeaf975a980e2df98f Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 30 Oct 2021 14:56:25 +0000 Subject: [PATCH 2/2] Format code with black, yapf, autopep8 and isort --- minato_namikaze/bot_files/cogs/weather.py | 3 +- .../bot_files/lib/classes/games/typeracer.py | 36 +++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/minato_namikaze/bot_files/cogs/weather.py b/minato_namikaze/bot_files/cogs/weather.py index 70829c35..bfb82eb9 100644 --- a/minato_namikaze/bot_files/cogs/weather.py +++ b/minato_namikaze/bot_files/cogs/weather.py @@ -133,7 +133,8 @@ async def get_weather( params["q"] = str(location) url = "https://api.openweathermap.org/data/2.5/weather?{0}".format( urlencode(params)) - async with aiohttp.ClientSession() as session, session.get(url) as resp: + async with aiohttp.ClientSession() as session, session.get( + url) as resp: data = await resp.json() try: if data["message"] == "city not found": diff --git a/minato_namikaze/bot_files/lib/classes/games/typeracer.py b/minato_namikaze/bot_files/lib/classes/games/typeracer.py index 28ea72d9..de0fa96e 100644 --- a/minato_namikaze/bot_files/lib/classes/games/typeracer.py +++ b/minato_namikaze/bot_files/lib/classes/games/typeracer.py @@ -286,9 +286,8 @@ def _tr_img(text: str, font: str): buffer.seek(0) return buffer - async def wait_for_tr_response( - self, ctx: commands.Context, text: str, *, timeout: int, start - ): + async def wait_for_tr_response(self, ctx: commands.Context, text: str, *, + timeout: int, start): text = text.lower().replace("\n", " ").strip(".") @@ -296,8 +295,8 @@ async def wait_for_tr_response( message = await ctx.bot.wait_for( "message", timeout=timeout, - check=lambda m: m.channel == ctx.channel - and m.content.lower().replace("\n", " ").strip(".") == text, + check=lambda m: m.channel == ctx.channel and m.content.lower(). + replace("\n", " ").strip(".") == text, ) except asyncio.TimeoutError: return await ctx.reply( @@ -315,9 +314,8 @@ async def wait_for_tr_response( ) await message.add_reaction("\U00002705") - await message.reply( - embed=embed, allowed_mentions=discord.AllowedMentions.none() - ) + await message.reply(embed=embed, + allowed_mentions=discord.AllowedMentions.none()) return True async def start( @@ -332,7 +330,8 @@ async def start( ): if mode == "sentence": - async with aiohttp.ClientSession() as session, session.get(self.SENTENCE_URL) as r: + async with aiohttp.ClientSession() as session, session.get( + self.SENTENCE_URL) as r: if r.status in range(200, 299): text = await r.json() text = text["content"] @@ -340,25 +339,26 @@ async def start( return await ctx.send("Oops an error occured") elif mode == "random": text = " ".join( - [random.choice(self.GRAMMAR_WORDS).lower() for _ in range(15)] - ) + [random.choice(self.GRAMMAR_WORDS).lower() for _ in range(15)]) else: raise TypeError( "Invalid game mode , must be either 'random' or 'sentence'") - buffer = await ctx.bot.loop.run_in_executor( - None, self._tr_img, text, path_to_text_font - ) + buffer = await ctx.bot.loop.run_in_executor(None, self._tr_img, text, + path_to_text_font) - embed = discord.Embed( - title=embed_title, color=embed_color, timestamp=dt.utcnow() - ) + embed = discord.Embed(title=embed_title, + color=embed_color, + timestamp=dt.utcnow()) embed.set_image(url="attachment://tr.png") embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) await ctx.send(embed=embed, file=discord.File(buffer, "tr.png")) start = time.perf_counter() - await self.wait_for_tr_response(ctx, text, start=start, timeout=timeout) + await self.wait_for_tr_response(ctx, + text, + start=start, + timeout=timeout) return True