utils

randomstuff.utils is a sub module that contains some useful functions to aid with the API interactions.

All functions are library specific and are not provided or suggested by API.

In order to use this module, You have to import it explicitly like this:

from randomstuff import utils
# or
import randomstuff.utils

Functions

generate_uid

Generates a complex and safe to use unique ID. This is very useful when you need a unique key for AI response endpoint.

level(Optional)

This determines the number of chars in the key. Defaults to 30 by default.

Type: int

format_joke

A simple method to turn a two-part joke into a string. It simply formats two-part joke in provided format, By default is: '{setup}... {delivery}'

format_to (Optional)

This is the format. In that format, {setup} is replaced with setup of the joke and {delivery} with delivery .

For example:

joke = client.get_joke()
formatted_joke = utils.format_joke(
    joke, 
    format_to='{setup}... {delivery}'
    )
print(formatted_joke.joke) # The formated joke

Type: str

If the passed joke type is single, It silently ignores it and returns the passed joke as it is. (So, this technically raises no error so you can use it for general usage without having to handle any error.)

get_safe_joke

A method that filters or ignores any unsafe joke. This method is made to be used in replacement to get_joke method.

It ignores any joke whose safe attribute is False and fetches another joke. Here, A question is raised that won't it cause rate-limit because of calling API so many times. The answer is, No. Usually jokes are returned with safe attribute as True there is a small chance of getting an unsafe joke. This method handles that chance. So, it has hardly noticeable affect.

For example:

client = randomstuff.Client(api_key="key") # Or AsyncClient 
joke = utils.get_safe_joke(client, type="any")
print(joke.safe) # Always prints `True`
print(joke.joke)

client

Your Client or AsyncClient instance.

Type: Union[Client, AsyncClient]

type (Optional)

Equivalent to Joke.type

Type: Union[Client, AsyncClient]

Last updated