Skip to content

Commit

Permalink
refactor: merge collapsible with statements (#25)
Browse files Browse the repository at this point in the history
Nested `with` statements can be merged into a single compound `with` statement.

This issue is raised only when there are no other statements between the nested `with` statements.
Merging collapsible `with` statements can decrease cognitive complexity, improving code readability.

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
deepsource-autofix[bot] authored Jan 9, 2024
1 parent bf16eb5 commit 077c876
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/plugins/ncm/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,9 @@ async def download(self, ids: list, check=False, lid=0, is_zip=False): # 下载
music.update(config, Q["id"] == nid)
else:
music.insert(config)
async with httpx.AsyncClient() as client: # 下载歌曲
async with client.stream("GET", url=url) as r:
async with async_open(file, "wb") as out_file:
async for chunk in r.aiter_bytes():
await out_file.write(chunk)
async with httpx.AsyncClient() as client, client.stream("GET", url=url) as r, async_open(file, "wb") as out_file:
async for chunk in r.aiter_bytes():
await out_file.write(chunk)
logger.debug(f"Download:{filename}")
if is_zip:
await self.get_zip(lid=lid, filenames=filenames)
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/nonebot_plugin_charpic/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ async def get_char_frame(text: str, w: int, h: int, font_):
async def get_img(img_url: str):
if not img_url:
return
async with aiohttp.ClientSession() as session:
async with session.get(img_url) as resp:
result = await resp.read()
async with aiohttp.ClientSession() as session, session.get(img_url) as resp:
result = await resp.read()
return Image.open(io.BytesIO(result)) if result else None
13 changes: 6 additions & 7 deletions src/plugins/nonebot_plugin_githubcard/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ async def get_github_reposity_information(url: str) -> str:
UserName, RepoName = url.replace("https://github.com/", "").split("/")
except Exception:
UserName, RepoName = url.replace("github.com/", "").split("/")
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.github.com/users/{UserName}",
headers=headers,
timeout=5) as response:
RawData = await response.json()
AvatarUrl = RawData["avatar_url"]
return f"https://image.thum.io/get/width/1280/crop/640/viewportWidth/1280/png/noanimate/https://socialify.git.ci/{UserName}/{RepoName}/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&language=1&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark&logo={AvatarUrl}"
async with aiohttp.ClientSession() as session, session.get(f"https://api.github.com/users/{UserName}",
headers=headers,
timeout=5) as response:
RawData = await response.json()
AvatarUrl = RawData["avatar_url"]
return f"https://image.thum.io/get/width/1280/crop/640/viewportWidth/1280/png/noanimate/https://socialify.git.ci/{UserName}/{RepoName}/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&language=1&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark&logo={AvatarUrl}"

0 comments on commit 077c876

Please sign in to comment.