Miscellaneous

class arez.CacheObject(*, id=0, name='Unknown')[source]

Base class representing objects that can be returned from the data cache. You will sometimes find these on objects returned from the API, when the cache was either incomplete or disabled.

id

The object’s ID.

Defaults to 0 if not set.

Type

int

name

The object’s name.

Defaults to Unknown if not set.

Type

str

class arez.utils.Duration(**kwargs)[source]

Represents a duration. Allows for easy conversion between time units.

This object isn’t a subclass of datetime.timedelta, but behaves as such - it’s also immutable, and anything you’d normally be able to do on a datetime.timedelta object, should be doable on this as well. This includes addition, substraction, multiplication, division (true and floor), modulo, divmod, negation and getting absolute value. Operations support the second argument being a normal datetime.timedelta, but the return value is always an instance of this class. If you prefer doing math using a normal datetime.timedelta object, you can use the to_timedelta method to convert it to such.

days

Returns days as an integer.

Note: It is possible for this number to be negative, if it’s been constructed from a negative datetime.timedelta.

hours

Returns hours in range 0-23.

minutes

Returns minutes in range 0-59.

seconds

Returns seconds in range of 0-59.

microseconds

Returns microseconds in range 0-999999

total_days()[source]

The total amount of days within the duration, as a float.

total_hours()[source]

The total amount of hours within the duration, as a float.

total_minutes()[source]

The total amount of minutes within the duration, as a float.

total_seconds()[source]

The total amount of seconds within the duration, as a float.

to_timedelta()[source]

Converts this Duration object into datetime.timedelta.

classmethod from_timedelta(delta)[source]

Returns a Duration instance constructed from a datetime.timedelta object.

class arez.utils.Lookup(iterable, *, key=lambda item: ...)[source]

A helper class utilizing an internal list and two dictionaries, allowing for easy indexing and lookup of CacheObject and it’s subclasses, based on the Name and ID attributes. Supports fuzzy Name searches too.

This object resembles an immutable sequence, and thus exposes __len__, __iter__, __getitem__, __contains__, __reversed__, index and count special methods for ease of use. The types specified refer to: LookupKeyType by which you can query, and LookupType which is returned from the query.

If you’d prefer a normal list instead, use: list(lookup).

Parameters
  • iterable (Iterable[LookupType]) – The iterable to objects to transform into a lookup.

  • key (Callable[[LookupType], LookupKeyType]) –

    The lookup key function, mapping each object to a CacheObject or it’s subclass, by which the lookup should be indexed.

    Defaults to an identity function (lambda item: item), meaning objects passed as the iterable have to be a CacheObject or it’s subclass already.

get(name_or_id)[source]

Allows you to quickly lookup an element by it’s Name or ID.

Parameters

name_or_id (Union[int, str]) –

The name or ID of the element you want to lookup.

Note

The name lookup is case-insensitive.

Returns

The element requested.

None is returned if the requested element couldn’t be found.

Return type

Optional[LookupType]

get_fuzzy_matches(name, *, limit=3, cutoff=0.6, with_scores=False)[source]

Performs a fuzzy lookup of an element by it’s name, by calculating the similarity score between each item. Case-insensitive.

See also: get_fuzzy.

Parameters
  • name (str) – The name of the element you want to lookup.

  • limit (int) –

    The maximum amount of elements to return in the list. Has to be greater than 0.

    Defaults to 3.

  • cutoff (float) –

    The similarity score cutoff range, below which matches will be excluded from the output. Lower values have a better chance of yielding correct results, but also a higher chance of false-positives. Accepted range is 0 to 1.

    Defaults to 0.6.

  • with_scores (bool) –

    If set to True, returns a list of 2-item tuples, with the similar element as the first item, and its score as the second.

    Defaults to False.

Returns

A list of up to limit matching elements, with at least cutoff similarity score, sorted in descending order by their similarity score.

If with_scores is set to True, returns a list of up to limit 2-item tuples, where the first item of each tuple is the element, and the second item is the similarity score it has.

Return type

Union[List[LookupType], List[Tuple[LookupType, float]]]

Raises
  • TypeErrorname, limit or cutoff arguments are of incorrect type

  • ValueErrorlimit or cutoff arguments have an incorrect value

get_fuzzy(name, *, cutoff=0.6)[source]

Simplified version of get_fuzzy_matches, allowing you to search for a single element, or receive None if no matching element was found.

Parameters
  • name (str) – The name of the element you want to lookup.

  • cutoff (float, optional) –

    The similarity score cutoff range. See: get_fuzzy_matches for more information.

    Defaults to 0.6.

Returns

The element requested.

None is returned if the requested element couldn’t be found.

Return type

Optional[LookupType]

Raises
  • TypeErrorname or cutoff arguments are of incorrect type

  • ValueErrorcutoff argument has an incorrect value

class arez.utils.LookupGroup(iterable, *, key=lambda item: ...)[source]

This class is indentical to the Lookup class functionality-wise, but it’s been made a separate class due to typing collisions. The only difference here is that get, get_fuzzy and get_fuzzy_matches methods return a grouped list of the specified type instances, instead of a single instance. Right now, this is used solely for the PartialPlayer.get_loadouts method return type, to be able to return a list of loadouts for each champion.

arez.utils.get(iterable, **attrs)[source]

Returns the first object from the iterable which attributes match the keyword arguments passed.

You can use __ to search in nested attributes.

Parameters
  • iterable (Iterable) – The iterable to search in.

  • **attrs – The attributes to search for.

Returns

The first object from the iterable with attributes matching the keyword arguments passed.

None is returned if the desired object couldn’t be found in the iterable.

Return type

Any

arez.utils.group_by(iterable, key)[source]

A helper function for grouping elements of an iterable into a dictionary, where each key represents a common value, and the value represents a list of elements having said common value.

Parameters
  • iterable (Iterable[X]) – An iterable of elements to group.

  • key (Callable[[X], Y]) – A function that takes each element from the provided iterable as it’s parameter, and outputs a group to which said element belongs to.

Returns

A mapping of groups to lists of grouped elements.

Return type

Dict[Y, List[X]]

for ... in arez.utils.chunk(list_to_chunk, chunk_length)[source]

A helper generator that divides the input list into chunks of chunk_length length. The last chunk may be shorter than specified.

Parameters
  • list_to_chunk (List[X]) – The list you want to divide into chunks.

  • chunk_length (int) – The length of each chunk.

Returns

A generator yielding chunks of the given length.

Return type

Generator[List[X], None, None]

async for ... in arez.utils.expand_partial(iterable)[source]

A helper async generator that can be used to automatically expand partial objects for you. Any other object found in the iterable is passed unchanged.

The following classes are converted:

PartialPlayer -> Player

PartialMatch -> Match

Parameters

iterable (Iterable) – The iterable containing partial objects.

Returns

An async generator yielding expanded versions of each partial object.

Return type

AsyncGenerator