📁
randomstuff.py
  • randomstuff.py
  • Clients
    • Client
    • AsyncClient
  • Data Classes
    • AIResponse
    • Joke
    • JokeFlags
    • Waifu
    • Weather
    • WeatherLocation
    • CurrentWeather
    • WeatherForecast
  • Sub modules
    • utils
  • Errors tree
    • Forbidden
      • BadAPIKey
      • PlanNotAllowed
    • ArgumentError
      • InvalidPlanError
      • InvalidVersionError
      • InvalidServerError
      • InvalidType
      • InvalidCityError
    • HTTPError
  • Brief
    • Warnings
    • Lists
    • Unique ID
    • Joke 101
    • FAQ
    • Changelogs
Powered by GitBook
On this page
  1. Sub modules

utils

PreviousWeatherForecastNextForbidden

Last updated 4 years ago

Was this helpful?

CtrlK
  • Functions
  • generate_uid
  • format_joke
  • get_safe_joke

Was this helpful?

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:

: The generated key.

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:

Type:

: The new joke with joke attribute as the formatted joke.

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 or instance.

Type: Union[, ]

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.)

type (Optional)

Equivalent to Joke.type

Type: Union[Client, AsyncClient]

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