Skip to content

berry

pokelance.ext.berry ⚓︎

Berry(client) ⚓︎

Bases: BaseExtension

Extension for berry related endpoints.

Attributes:

Name Type Description
cache Berry

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_berry(name) async ⚓︎

Fetches a berry from the API.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the berry.

required

Returns:

Type Description
Berry

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

Raises:

Type Description
ResourceNotFound

The name or id of the berry is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     berry = await client.berry.fetch_berry("cheri")
...     print(berry.name)
...     await client.close()
>>> asyncio.run(main())
'cheri'
Source code in pokelance/ext/berry.py
Python
async def fetch_berry(self, name: t.Union[str, int]) -> BerryModel:
    """Fetches a berry from the API.

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

    Returns
    -------
    BerryModel
        The berry if it exists in the API, else raises ResourceNotFound.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     berry = await client.berry.fetch_berry("cheri")
    ...     print(berry.name)
    ...     await client.close()
    >>> asyncio.run(main())
    'cheri'
    """
    route = Endpoint.get_berry(name)
    self._validate_resource(self.cache.berry, name, route)
    data = await self._client.request(route)
    return self.cache.berry.setdefault(route, BerryModel.from_payload(data))

fetch_berry_firmness(name) async ⚓︎

Fetches a berry firmness from the API.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the berry firmness.

required

Returns:

Type Description
BerryFirmness

The berry firmness if it exists in the API, else raises ResourceNotFound.

Raises:

Type Description
ResourceNotFound

The name or id of the berry firmness is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     berry_firmness = await client.berry.fetch_berry_firmness("very-soft")
...     print(berry_firmness.name)
...     await client.close()
>>> asyncio.run(main())
'very-soft'
Source code in pokelance/ext/berry.py
Python
async def fetch_berry_firmness(self, name: t.Union[str, int]) -> BerryFirmness:
    """Fetches a berry firmness from the API.

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

    Returns
    -------
    BerryFirmness
        The berry firmness if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the berry firmness is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     berry_firmness = await client.berry.fetch_berry_firmness("very-soft")
    ...     print(berry_firmness.name)
    ...     await client.close()
    >>> asyncio.run(main())
    'very-soft'
    """
    route = Endpoint.get_berry_firmness(name)
    self._validate_resource(self.cache.berry_firmness, name, route)
    data = await self._client.request(route)
    return self.cache.berry_firmness.setdefault(route, BerryFirmness.from_payload(data))

fetch_berry_flavor(name) async ⚓︎

Fetches a berry flavor from the API.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the berry flavor.

required

Returns:

Type Description
BerryFlavor

The berry flavor if it exists in the API, else raises ResourceNotFound.

Raises:

Type Description
ResourceNotFound

The name or id of the berry flavor is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> import asyncio
>>> client = PokeLance()
>>> async def main() -> None:
...     berry_flavor = await client.berry.fetch_berry_flavor("spicy")
...     print(berry_flavor.name)
...     await client.close()
>>> asyncio.run(main())
'spicy'
Source code in pokelance/ext/berry.py
Python
async def fetch_berry_flavor(self, name: t.Union[str, int]) -> BerryFlavor:
    """Fetches a berry flavor from the API.

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

    Returns
    -------
    BerryFlavor
        The berry flavor if it exists in the API, else raises ResourceNotFound.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the berry flavor is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> import asyncio
    >>> client = PokeLance()
    >>> async def main() -> None:
    ...     berry_flavor = await client.berry.fetch_berry_flavor("spicy")
    ...     print(berry_flavor.name)
    ...     await client.close()
    >>> asyncio.run(main())
    'spicy'
    """
    route = Endpoint.get_berry_flavor(name)
    self._validate_resource(self.cache.berry_flavor, name, route)
    data = await self._client.request(route)
    return self.cache.berry_flavor.setdefault(route, BerryFlavor.from_payload(data))

get_berry(name) ⚓︎

Gets a berry from the cache.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the berry.

required

Returns:

Type Description
Optional[Berry]

The berry if it exists in the cache, else None.

Raises:

Type Description
ResourceNotFound

The name or id of the berry is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> berry = client.berry.get_berry("cheri")  # None if not cached
>>> berry.name
'cheri'
Source code in pokelance/ext/berry.py
Python
def get_berry(self, name: t.Union[str, int]) -> t.Optional[BerryModel]:
    """Gets a berry from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.Berry]
        The berry if it exists in the cache, else None.

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

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> berry = client.berry.get_berry("cheri")  # None if not cached
    >>> berry.name
    'cheri'
    """
    route = Endpoint.get_berry(name)
    self._validate_resource(self.cache.berry, name, route)
    return self.cache.berry.get(route, None)

get_berry_firmness(name) ⚓︎

Gets a berry firmness from the cache.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the berry firmness.

required

Returns:

Type Description
Optional[BerryFirmness]

The berry firmness if it exists in the cache, else None.

Raises:

Type Description
ResourceNotFound

The name or id of the berry firmness is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> berry_firmness = client.berry.get_berry_firmness("very-soft")  # None if not cached
>>> berry_firmness.name
'very-soft'
Source code in pokelance/ext/berry.py
Python
def get_berry_firmness(self, name: t.Union[str, int]) -> t.Optional[BerryFirmness]:
    """Gets a berry firmness from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.BerryFirmness]
        The berry firmness if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the berry firmness is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> berry_firmness = client.berry.get_berry_firmness("very-soft")  # None if not cached
    >>> berry_firmness.name
    'very-soft'
    """
    route = Endpoint.get_berry_firmness(name)
    self._validate_resource(self.cache.berry_firmness, name, route)
    return self.cache.berry_firmness.get(route, None)

get_berry_flavor(name) ⚓︎

Gets a berry flavor from the cache.

Parameters:

Name Type Description Default
name Union[str, int]

The name or id of the berry flavor.

required

Returns:

Type Description
Optional[BerryFlavor]

The berry flavor if it exists in the cache, else None.

Raises:

Type Description
ResourceNotFound

The name or id of the berry flavor is invalid.

Examples:

Python Console Session
>>> from pokelance import PokeLance
>>> client = PokeLance()
>>> berry_flavor = client.berry.get_berry_flavor("spicy")  # None if not cached
>>> berry_flavor.name
'spicy'
Source code in pokelance/ext/berry.py
Python
def get_berry_flavor(self, name: t.Union[str, int]) -> t.Optional[BerryFlavor]:
    """Gets a berry flavor from the cache.

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

    Returns
    -------
    typing.Optional[pokelance.models.BerryFlavor]
        The berry flavor if it exists in the cache, else None.

    Raises
    ------
    pokelance.exceptions.ResourceNotFound
        The name or id of the berry flavor is invalid.

    Examples
    --------

    >>> from pokelance import PokeLance
    >>> client = PokeLance()
    >>> berry_flavor = client.berry.get_berry_flavor("spicy")  # None if not cached
    >>> berry_flavor.name
    'spicy'
    """
    route = Endpoint.get_berry_flavor(name)
    self._validate_resource(self.cache.berry_flavor, name, route)
    return self.cache.berry_flavor.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 berry cog.

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