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.
-
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 adatetime.timedeltaobject, 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 normaldatetime.timedelta, but the return value is always an instance of this class. If you prefer doing math using a normaldatetime.timedeltaobject, you can use theto_timedeltamethod 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
-
to_timedelta()[source]¶ Converts this
Durationobject intodatetime.timedelta.
-
classmethod
from_timedelta(delta)[source]¶ Returns a
Durationinstance constructed from adatetime.timedeltaobject.
-
-
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
CacheObjectand 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__,indexandcountspecial methods for ease of use. The types specified refer to:LookupKeyTypeby which you can query, andLookupTypewhich 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
CacheObjector 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 aCacheObjector it’s subclass already.
-
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
0to1.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
limitmatching elements, with at leastcutoffsimilarity score, sorted in descending order by their similarity score.If
with_scoresis set toTrue, returns a list of up tolimit2-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
TypeError –
name,limitorcutoffarguments are of incorrect typeValueError –
limitorcutoffarguments 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 receiveNoneif 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_matchesfor more information.Defaults to
0.6.
- Returns
The element requested.
Noneis returned if the requested element couldn’t be found.- Return type
Optional[LookupType]
- Raises
TypeError –
nameorcutoffarguments are of incorrect typeValueError –
cutoffargument has an incorrect value
-
class
arez.utils.LookupGroup(iterable, *, key=lambda item: ...)[source]¶ This class is indentical to the
Lookupclass functionality-wise, but it’s been made a separate class due to typing collisions. The only difference here is thatget,get_fuzzyandget_fuzzy_matchesmethods return a groupedlistof the specified type instances, instead of a single instance. Right now, this is used solely for thePartialPlayer.get_loadoutsmethod 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
iterablewhich 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.
Noneis 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_lengthlength. The last chunk may be shorter than specified.
-
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
iterableis passed unchanged.- The following classes are converted:
- Parameters
iterable (Iterable) – The iterable containing partial objects.
- Returns
An async generator yielding expanded versions of each partial object.
- Return type
AsyncGenerator