This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add NearbyArtefactsList (#1581)
* feat: add NearbyArtefactsList * use const * cover NearbyArtefactsList IndexError * code review * comment
- Loading branch information
1 parent
3002ffa
commit bcab6df
Showing
5 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Error(Exception): | ||
"""Base class for other exceptions""" | ||
|
||
pass | ||
|
||
|
||
class NoNearbyArtefactsError(Error): | ||
"""Raised when there are no nearby artefacts""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import TypeVar, List | ||
|
||
from .errors import NoNearbyArtefactsError | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class NearbyArtefactsList(List[T]): | ||
""" | ||
A list-like object that raises NoNearbyArtefactsError when trying to access an element and the list is empty. | ||
""" | ||
|
||
def __getitem__(self, i): | ||
try: | ||
return super().__getitem__(i) | ||
except IndexError: | ||
if len(self) == 0: | ||
raise NoNearbyArtefactsError( | ||
"There aren't any nearby artefacts, you need to move closer!" | ||
) from None | ||
else: | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters