Skip to content

Commit

Permalink
built-in function next instead of a for-loop ✨ (#213)
Browse files Browse the repository at this point in the history
* chore(dev): refactor code & improve some exceptions

* chore(dev): Use the built-in function next instead of a for-loop
  • Loading branch information
yezz123 authored Feb 11, 2022
1 parent d834a30 commit 0967e8b
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ def _increment_id(self) -> int:
return self._incr

def _get(self, field: str, value) -> Optional[dict]:
for item in self._users:
if item.get(field) == value:
return item

return None
return next((item for item in self._users if item.get(field) == value), None)

async def get(self, id: int) -> Optional[dict]:
return self._get("id", id)
Expand All @@ -124,11 +120,14 @@ async def get_by_username(self, username: str) -> Optional[dict]:
return self._get("username", username)

async def get_by_social(self, provider: str, sid: str) -> Optional[dict]:
for item in self._users:
if item.get("provider") == provider and item.get("sid") == sid:
return item

return None
return next(
(
item
for item in self._users
if item.get("provider") == provider and item.get("sid") == sid
),
None,
)

async def create(self, obj: dict) -> int:
id = self._increment_id()
Expand Down

0 comments on commit 0967e8b

Please sign in to comment.