Skip to content

Commit

Permalink
Stop get_x from emptying the iterable of members/heroes/troops for …
Browse files Browse the repository at this point in the history
…later calls.
  • Loading branch information
mathsman5133 committed Apr 22, 2021
1 parent 971ffe6 commit e1bae56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
7 changes: 3 additions & 4 deletions coc/clans.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,11 @@ def get_member(self, tag: str) -> typing.Optional[ClanMember]:
The member who matches the tag provided: Optional[:class:`ClanMember`]
"""
tag = correct_tag(tag)
dict_members = self._members
if not dict_members:
dict_members = self._members = {m.tag: m for m in self._iter_members}
if not self._members:
_ = self.members

try:
return dict_members[tag]
return self._members[tag]
except KeyError:
return None

Expand Down
20 changes: 8 additions & 12 deletions coc/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,11 @@ def get_achievement(self, name: str, default_value=None) -> typing.Optional[Achi
Optional[:class:`Achievement`]
The returned achievement or the ``default_value`` if not found, which defaults to ``None``..
"""
dict_achievements = self._achievements
if dict_achievements is None:
if not self._achievements:
_ = self.achievements
dict_achievements = self._achievements

try:
return dict_achievements[name]
return self._achievements[name]
except KeyError:
return default_value

Expand Down Expand Up @@ -479,12 +477,11 @@ def get_hero(self, name: str, default_value=None) -> typing.Optional[Hero]:
Optional[:class:`Hero`]
The returned hero or the ``default_value`` if not found, which defaults to ``None``..
"""
dict_heroes = self._heroes
if dict_heroes is None:
dict_heroes = self._heroes = {h.name: h for h in self._iter_heroes}
if not self._heroes:
_ = self.heroes

try:
return dict_heroes[name]
return self._heroes[name]
except KeyError:
return default_value

Expand Down Expand Up @@ -513,11 +510,10 @@ def get_spell(self, name: str, default_value=None) -> typing.Optional[Spell]:
Optional[:class:`Spell`]
The returned spell or the ``default_value`` if not found, which defaults to ``None``..
"""
dict_spells = self._spells
if dict_spells is None:
dict_spells = self._spells = {s.name: s for s in self._iter_spells}
if not self._spells:
_ = self.spells

try:
return dict_spells[name]
return self._spells[name]
except KeyError:
return default_value

0 comments on commit e1bae56

Please sign in to comment.