Skip to content

Commit

Permalink
refactor: use walrus operator
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Feb 18, 2025
1 parent ef7aba9 commit bad612b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion topgg/autopost.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _fut_done_callback(self, future: "asyncio.Future") -> None:

async def _internal_loop(self) -> None:
try:
while 1:
while True:
stats = await self.client._invoke_callback(self._stats)
try:
await self.client.post_guild_count(stats)
Expand Down
9 changes: 5 additions & 4 deletions topgg/ratelimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ async def __aenter__(self) -> "AsyncRateLimiter":
until = time() + self.period - self._timespan
if self.callback:
asyncio.ensure_future(self.callback(until))
sleep_time = until - time()
if sleep_time > 0:
elif (sleep_time := until - time()) > 0:
await asyncio.sleep(sleep_time)
return self

Expand Down Expand Up @@ -97,7 +96,9 @@ def __init__(self, rate_limiters: List[AsyncRateLimiter]):
self.rate_limiters = rate_limiters

async def __aenter__(self) -> "AsyncRateLimiterManager":
[await manager.__aenter__() for manager in self.rate_limiters]
for manager in self.rate_limiters:
await manager.__aenter__()

return self

async def __aexit__(
Expand All @@ -106,4 +107,4 @@ async def __aexit__(
exc_val: BaseException,
exc_tb: TracebackType,
) -> None:
await asyncio.gather(*[manager.__aexit__(exc_type, exc_val, exc_tb) for manager in self.rate_limiters])
await asyncio.gather(*(manager.__aexit__(exc_type, exc_val, exc_tb) for manager in self.rate_limiters))
38 changes: 17 additions & 21 deletions topgg/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def camel_to_snake(string: str) -> str:
def parse_vote_dict(d: dict) -> dict:
data = d.copy()

query = data.get("query", "").lstrip("?")
if query:
if query := data.get("query", "").lstrip("?"):
query_dict = {k: v for k, v in [pair.split("=") for pair in query.split("&")]}
data["query"] = DataDict(**query_dict)
else:
Expand All @@ -55,8 +54,7 @@ def parse_vote_dict(d: dict) -> dict:
data["guild"] = int(data["guild"])

for key, value in data.copy().items():
converted_key = camel_to_snake(key)
if key != converted_key:
if key != (converted_key := camel_to_snake(key)):
del data[key]
data[converted_key] = value

Expand All @@ -67,20 +65,17 @@ def parse_dict(d: dict) -> dict:
data = d.copy()

for key, value in data.copy().items():
if "id" in key.lower():
if value == "":
value = None
else:
if isinstance(value, str) and value.isdigit():
value = int(value)
else:
continue
elif value == "":
if value == "":
value = None
elif "id" in key.lower():
if isinstance(value, str) and value.isdigit():
value = int(value)
else:
continue

converted_key = camel_to_snake(key)
if key != converted_key:
if key != (converted_key := camel_to_snake(key)):
del data[key]

data[converted_key] = value

return data
Expand All @@ -89,13 +84,14 @@ def parse_dict(d: dict) -> dict:
def parse_bot_dict(d: dict) -> dict:
data = parse_dict(d.copy())

if data.get("date") and not isinstance(data["date"], datetime):
data["date"] = datetime.fromisoformat(data["date"].replace("Z", "+00:00"))
if (date := data.get("date")) and not isinstance(date, datetime):
data["date"] = datetime.fromisoformat(date.replace("Z", "+00:00"))

if owners := data.get("owners"):
data["owners"] = [int(e) for e in owners]

if data.get("owners"):
data["owners"] = [int(e) for e in data["owners"]]
if data.get("guilds"):
data["guilds"] = [int(e) for e in data["guilds"]]
# TODO: remove this soon
data["guilds"] = []

for key, value in data.copy().items():
converted_key = camel_to_snake(key)
Expand Down

0 comments on commit bad612b

Please sign in to comment.