-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add backpack with find method (#1657)
* feat: add backpack with find method * code review
- Loading branch information
1 parent
1e59903
commit d79a354
Showing
4 changed files
with
91 additions
and
10 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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
from collections import namedtuple | ||
from simulation.location import Location | ||
from typing import Dict | ||
|
||
from .backpack import Backpack | ||
from .location import Location | ||
|
||
|
||
def create_avatar_state(avatar_state_json: Dict): | ||
avatar_state_dict = avatar_state_json.copy() | ||
avatar_state_dict["location"] = Location(**avatar_state_json["location"]) | ||
|
||
# check backpack as it doesn't always exist (i.e. worksheet 1) | ||
if avatar_state_json.get("backpack"): | ||
if "backpack" in avatar_state_json: | ||
# use namedtuple for artefacts to allow accessing fields by name | ||
avatar_state_dict["backpack"] = [ | ||
namedtuple("Artefact", artefact.keys())(*artefact.values()) | ||
for artefact in avatar_state_json["backpack"] | ||
] | ||
avatar_state_dict["backpack"] = Backpack( | ||
[namedtuple("Artefact", artefact.keys())(*artefact.values()) for artefact in avatar_state_json["backpack"]] | ||
) | ||
|
||
return namedtuple("AvatarState", avatar_state_dict.keys())( | ||
*avatar_state_dict.values() | ||
) | ||
return namedtuple("AvatarState", avatar_state_dict.keys())(*avatar_state_dict.values()) |
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,24 @@ | ||
from collections import namedtuple | ||
from multiprocessing.sharedctypes import Value | ||
from typing import TypeVar, List | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Backpack(List[T]): | ||
""" | ||
A list of artefacts that has a `find` method to find the first index of an artefact type. | ||
""" | ||
|
||
def find(self, artefact_type: str) -> int: | ||
""" | ||
Finds the first artefact of type `artefact_type` and returns its index or -1 if there's no artefact of that | ||
type. | ||
Args: | ||
artefact_type: The type of artefact to find. | ||
Returns: | ||
The index of the artefact or -1 if it doesn't exist. | ||
""" | ||
return next((i for i, artefact in enumerate(self) if artefact.type == artefact_type), -1) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from collections import namedtuple | ||
import pytest | ||
|
||
from simulation.backpack import Backpack | ||
|
||
Artefact = namedtuple("Artefact", "type") | ||
|
||
|
||
@pytest.fixture | ||
def keyboard_artefact(): | ||
return Artefact("keyboard") | ||
|
||
|
||
@pytest.fixture | ||
def phone_artefact(): | ||
return Artefact("phone") | ||
|
||
|
||
@pytest.fixture | ||
def coins_artefact(): | ||
return Artefact("coins") | ||
|
||
|
||
@pytest.fixture | ||
def orb_artefact(): | ||
return Artefact("orb") | ||
|
||
|
||
@pytest.fixture | ||
def empty_backpack(): | ||
return Backpack() | ||
|
||
|
||
@pytest.fixture | ||
def backpack(keyboard_artefact, phone_artefact, coins_artefact): | ||
return Backpack( | ||
[keyboard_artefact, keyboard_artefact, phone_artefact, coins_artefact, phone_artefact, coins_artefact] | ||
) | ||
|
||
|
||
def test_backpack_find(empty_backpack, backpack, keyboard_artefact, phone_artefact, coins_artefact, orb_artefact): | ||
assert empty_backpack.find("test") == -1 | ||
assert backpack.find(keyboard_artefact.type) == 0 | ||
assert backpack.find(phone_artefact.type) == 2 | ||
assert backpack.find(coins_artefact.type) == 3 | ||
assert backpack.find(orb_artefact.type) == -1 |