Module pyreddit.helpers

Miscellaneous helpers for the whole application.

Functions

def get_random_post_url(subreddit: str) ‑> str

Return the "random post" url relative to the Reddit API.

Parameters

subreddit : str
Subreddit for which to get the url.

Returns

str
A new string representing the url, including the base url of reddit.
def get_subreddit_names(text: str) ‑> List[str]

Return a list of the ("r/" prefixed) subreddit names present in the text.

Subreddits are searched using the official subreddit name validation.

Parameters

text : str
String of text in which to search for subreddit names.

Returns

array
Array of valid subreddit names present in the given text ("r/" prefixed).
def get_subreddit_name(text: str, reverse: bool = False) ‑> Union[str, NoneType]

Return the first (or last) ("r/" prefixed) subreddit name in the given text.

Parameters

text : str
String of text in which to search for the subreddit name.
reverse : Boolean

(Default value = False)

Whether to return the first or last match in the string.

Returns

str or None
The subreddit name if present in the text, None otherwise.
def escape_markdown(text: str, version=2, entity_type=None) ‑> str

Escape markup symbols.

Args:

text (:obj:<code>str</code>): The text.
version (:obj:<code>int</code> | :obj:<code>str</code>): Use to specify the version of telegrams Markdown.
    Either <code>1</code> or <code>2</code>. Defaults to <code>1</code>.
entity_type (:obj:<code>str</code>, optional): For the entity types <code>PRE</code>, <code>CODE</code> and the link
    part of <code>TEXT\_LINKS</code>, only certain characters need to be escaped in <code>MarkdownV2</code>.
    See the official API documentation for details. Only valid in combination with
    ``version=2``, will be ignored else.
def truncate_text(text: str, length: int = 200) ‑> str

Return the given text, truncated at length characters, plus ellipsis.

Parameters

text : str
String to truncate.
length : int

(Default value = MAX_TITLE_LENGTH)

Length to which to truncate the text, not including three characters of ellipsis.

Returns

str
New string containing the truncated text, plus ellipsis.
def polish_text(text: str) ‑> str

Return the given text without newline characters.

Parameters

text : str
Text to polish

Returns

str
New string containing the polished text.
def prefix_reddit_url(url: str) ‑> str

Return the url with reddit's base url as a prefix.

Parameters

url : str
Url to prefix

Returns

str
New string containing the prefixed url
def get_urls_from_text(text: str) ‑> List[str]

Return a list of the reddit urls present in the given text.

Parameters

text : str
Text to search for urls.

Returns

array
Array containing all the reddit links extracted from the text.
def get(obj: Any, attr: str, default: Any = None) ‑> Any

Return the value of attr if it exists and is not None, default otherwise.

Useful when you don't want to have a KeyError raised if the attribute is missing in the object.

Parameters

obj : object
The object for which to return the attribute.
attr : str
The key of the attribute to return.
default : any

(Default value = None)

What to return if obj doesn't have the attr attribute, or if it is None.

Returns

any
The attribute or default.
def chained_get(obj: object, attrs: List[str], default: Any = None) ‑> Any

Get for nested objects.

Travel the nested object based on attrs array and return the value of the last attr if not None, default otherwise.

Useful when you don't want to have a KeyError raised if an attribute of the chain is missing in the object.

Parameters

obj : object
The object for which to return the attribute.
attrs : array
Array of keys to search for recursively.
default : any

(Default value = None)

What to return if obj doesn't have any of the attrs attributes, or if they are None.

Returns

any
The attribute corresponding to the right-most key in attrs, if it exists and is not None, default otherwise.