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.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 normaldatetime.timedelta
, but the return value is always an instance of this class. If you prefer doing math using a normaldatetime.timedelta
object, you can use theto_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
-
to_timedelta
()[source]¶ Converts this
Duration
object intodatetime.timedelta
.
-
classmethod
from_timedelta
(delta)[source]¶ Returns a
Duration
instance constructed from adatetime.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
andcount
special methods for ease of use. The types specified refer to:LookupKeyType
by which you can query, andLookupType
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 aCacheObject
or 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
0
to1
.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 leastcutoff
similarity score, sorted in descending order by their similarity score.If
with_scores
is set toTrue
, returns a list of up tolimit
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
TypeError –
name
,limit
orcutoff
arguments are of incorrect typeValueError –
limit
orcutoff
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 receiveNone
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
TypeError –
name
orcutoff
arguments are of incorrect typeValueError –
cutoff
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 thatget
,get_fuzzy
andget_fuzzy_matches
methods return a groupedlist
of the specified type instances, instead of a single instance. Right now, this is used solely for thePartialPlayer.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.
-
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:
- Parameters
iterable (Iterable) – The iterable containing partial objects.
- Returns
An async generator yielding expanded versions of each partial object.
- Return type
AsyncGenerator