Skip to content

evolution

pokelance.ext.evolution ⚓︎

Evolution(client) ⚓︎

Bases: BaseExtension

Extension for evolution related endpoints.

Attributes:

Name Type Description
cache Evolution

The cache for this extension.

Initializes the extension.

Parameters:

Name Type Description Default
client HttpClient

The client to use for requests.

required
Source code in pokelance/ext/_base.py
Python
def __init__(self, client: "HttpClient") -> None:
    """Initializes the extension.

    Parameters
    ----------
    client: pokelance.http.HttpClient
        The client to use for requests.
    """
    self._client = client
    self._cache = self._client.cache
    self.cache = getattr(self._cache, self.__class__.__name__.lower())

fetch_evolution_chain(id_) async ⚓︎

Fetches an evolution chain from the API.

Parameters:

Name Type Description Default
id_ int

The name or id of the encounter method.

required

Returns:

Type Description
EvolutionChain

The evolution chain if it exists in the cache, else raises ResourceNotFound.

Raises:

Type Description
ResourceNotFound

The name or id of the evolution chain is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     chain = await client.evolution.fetch_evolution_chain(1)
...     print(chain.id)
...     await client.close()
>>> asyncio.run(main())
1
Source code in pokelance/ext/evolution.py
Python
async def fetch_evolution_chain(self, id_: int) -> EvolutionChain:
    """Fetches an evolution chain from the API.

    Parameters
    ----------
    id_: int
        The name or id of the encounter method.

    Returns
    -------
    EvolutionChain
        The evolution chain if it exists in the cache, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the evolution chain is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     chain = await client.evolution.fetch_evolution_chain(1)
    ...     print(chain.id)
    ...     await client.close()
    >>> asyncio.run(main())
    1
    """
    route = Endpoint.get_evolution_chain(id_)
    self._validate_resource(self.cache.evolution_chain, id_, route)
    data = await self._client.request(route)
    return self.cache.evolution_chain.setdefault(route, EvolutionChain.from_payload(data))

fetch_evolution_trigger(name) async ⚓︎

Fetches an evolution trigger from the API.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the encounter method.

required

Returns:

Type Description
EvolutionTrigger

The evolution trigger if it exists in the API, else raises ResourceNotFound.

Raises:

Type Description
ResourceNotFound

The name or id of the evolution trigger is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     trigger = await client.evolution.fetch_evolution_trigger(1)
...     print(trigger.name)
...     await client.close()
>>> asyncio.run(main())
level-up
Source code in pokelance/ext/evolution.py
Python
async def fetch_evolution_trigger(self, name: t.Union[str, int]) -> EvolutionTrigger:
    """Fetches an evolution trigger from the API.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the encounter method.

    Returns
    -------
    EvolutionTrigger
        The evolution trigger if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the evolution trigger is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     trigger = await client.evolution.fetch_evolution_trigger(1)
    ...     print(trigger.name)
    ...     await client.close()
    >>> asyncio.run(main())
    level-up
    """
    route = Endpoint.get_evolution_trigger(name)
    self._validate_resource(self.cache.evolution_trigger, name, route)
    data = await self._client.request(route)
    return self.cache.evolution_trigger.setdefault(route, EvolutionTrigger.from_payload(data))

get_evolution_chain(id_) ⚓︎

Gets an evolution chain from the cache.

Parameters:

Name Type Description Default
id_ int

The name or id of the encounter method.

required

Returns:

Type Description
Optional[EvolutionChain]

The evolution chain if it exists in the cache, else None.

Raises:

Type Description
ResourceNotFound

The name or id of the evolution chain is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> chain = client.evolution.get_evolution_chain(1)
>>> chain.id
1
Source code in pokelance/ext/evolution.py
Python
def get_evolution_chain(self, id_: int) -> t.Optional[EvolutionChain]:
    """Gets an evolution chain from the cache.

    Parameters
    ----------
    id_: int
        The name or id of the encounter method.

    Returns
    -------
    typing.Optional[pokelance.models.EvolutionChain]
        The evolution chain if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the evolution chain is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> chain = client.evolution.get_evolution_chain(1)
    >>> chain.id
    1
    """
    route = Endpoint.get_evolution_chain(id_)
    self._validate_resource(self.cache.evolution_chain, id_, route)
    return self.cache.evolution_chain.get(route, None)

get_evolution_trigger(name) ⚓︎

Gets an evolution trigger from the cache.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the encounter method.

required

Returns:

Type Description
Optional[EvolutionTrigger]

The evolution trigger if it exists in the cache, else None.

Raises:

Type Description
ResourceNotFound

The name or id of the evolution trigger is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> trigger = client.evolution.get_evolution_trigger(1)
>>> trigger.name
'level-up'
Source code in pokelance/ext/evolution.py
Python
def get_evolution_trigger(self, name: t.Union[str, int]) -> t.Optional[EvolutionTrigger]:
    """Gets an evolution trigger from the cache.

    Parameters
    ----------
    name: typing.Union[str, int]
        The name or id of the encounter method.

    Returns
    -------
    typing.Optional[pokelance.models.EvolutionTrigger]
        The evolution trigger if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the evolution trigger is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> trigger = client.evolution.get_evolution_trigger(1)
    >>> trigger.name
    'level-up'
    """
    route = Endpoint.get_evolution_trigger(name)
    self._validate_resource(self.cache.evolution_trigger, name, route)
    return self.cache.evolution_trigger.get(route, None)

get_message(case, data) staticmethod ⚓︎

Gets the error message for a resource not found error.

Parameters:

Name Type Description Default
case str

The case to use for the error message.

required
data Set[str]

The data to use for the error message.

required

Returns:

Type Description
str

The error message.

Source code in pokelance/ext/_base.py
Python
@staticmethod
def get_message(case: str, data: t.Set[str]) -> str:
    """Gets the error message for a resource not found error.

    Parameters
    ----------
    case: str
        The case to use for the error message.
    data: typing.Set[str]
        The data to use for the error message.

    Returns
    -------
    str
        The error message.
    """
    matches = get_close_matches(case, data, n=10, cutoff=0.5)
    if matches:
        return f"Resource not found. Did you mean {', '.join(matches)}?"
    return "Resource not found."

setup() async ⚓︎

Sets up the extension.

Source code in pokelance/ext/_base.py
Python
async def setup(self) -> None:
    """Sets up the extension."""
    for item in dir(self):
        if item.startswith("fetch_"):
            data = await self._client.request(
                t.cast(t.Callable[[], "Route"], getattr(Endpoint, f"get_{item[6:]}_endpoints"))()
            )
            self._cache.load_documents(str(self.__class__.__name__), item[6:], data["results"])

setup(lance) ⚓︎

Sets up the evolution cog.

Source code in pokelance/ext/evolution.py
Python
def setup(lance: "PokeLance") -> None:
    """Sets up the evolution cog."""
    lance.add_extension("evolution", Evolution(lance.http))