Utility Functions¶
Object¶
get¶
- moddb.utils.get(iterable, **attrs)[source]¶
A helper that returns the first element in the iterable that meets all the traits passed in
attrs
. This is an alternative formoddb.utils.find()
.When multiple attributes are specified, they are checked using logical AND, not logical OR. Meaning they have to meet every attribute passed in and not one of them.
To have a nested attribute search (i.e. search by
x.y
) then pass inx__y
as the keyword argument.If nothing is found that matches the attributes passed, then
None
is returned.Examples
Basic usage:
article = moddb.utils.get(mod.get_articles(), name='Version 3.5 Released')
Multiple attribute matching:
comment = moddb.utils.get(mod.get_comments(2), content='Test', karma=3)
Nested attribute matching:
comment = moddb.utils.get(article.get_comments(), author__name='SilverElf', content='Best article ever')
find¶
- moddb.utils.find(predicate, seq)[source]¶
A helper to return the first element found in the sequence that meets the predicate. For example:
comment = find(lambda comment: comment.author.name == 'SilverElf', mod.comments.flatten())
would find the first
Comment
whose author’s name is ‘SilverElf’ and return it. If no entry is found, thenNone
is returned.This is different from filter due to the fact it stops the moment it finds a valid entry.
get_date¶
- moddb.utils.get_date(d)[source]¶
A helper function that takes a ModDB string representation of time and returns an equivalent datetime.datetime object. This can range from a datetime with the full year to second to just a year and a month.
- Parameters:
d (str) – String representation of a datetime
- Returns:
The datetime object for the given string
- Return type:
soup¶
get_page¶
get_views¶
join¶
normalize¶
get_media_type¶
get_page_type¶
get_list_stats¶
- moddb.utils.get_list_stats(result_box, per_page=30)[source]¶
Get the current page, total pages and total results from a result list
- Parameters:
result_box (bs4.BeautifulSoup) – The HTML of the result box from a list of results page
per_page (Optional[int]) – The number of results per page, important for calculations. Defaults to 30, doesn’t usually need to be touched
- Returns:
The stats in order of: number of current page (starting from 1), total number of pages (between 1 and X) and the total results.
- Return type: