curious.util

Misc utilities shared throughout the library.

Functions

base64ify(image_data) Base64-ifys an image to send to discord.
coerce_agen(gen) Coerces an async generator into a list.
deprecated(*, since, see_instead, removal) Marks a method as deprecated.
remove_from_multidict(d, key, item) Removes an item from a multidict key.
replace_quotes(item) Replaces the quotes in a string, but only if they are un-escaped.
safe_generator(cbl)
subclass_builtin(original) Subclasses an immutable builtin, providing method wrappers that return the subclass instead of the original.
to_datetime(timestamp) Converts a Discord-formatted timestamp to a datetime object.

Classes

AsyncIteratorWrapper(coro, …) Wraps a coroutine that returns a sequence of items into something that can iterated over asynchronously.
attrdict A dict that allows attribute access as well as item access for keys.

Exceptions

CuriousDeprecatedWarning Warned when a function is deprecated.
class curious.util.AsyncIteratorWrapper(coro: typing.Callable[typing.Union[typing.Awaitable[typing.List[typing.Any]], typing.Coroutine[NoneType, NoneType, typing.Any]]])[source]

Bases: collections.abc.AsyncIterator

Wraps a coroutine that returns a sequence of items into something that can iterated over asynchronously.

async def a():
    # ... some long op
    return [..., ..., ...]
 
it = AsyncIteratorWrapper(a)

async for item in it:
    print(item)
await all()[source]

Gets a flattened list of items from this iterator.

Return type:List[Any]
await next(default=<object object>)[source]

Gets the next item from this iterable.

Return type:Any
exception curious.util.CuriousDeprecatedWarning[source]

Bases: FutureWarning

Warned when a function is deprecated.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

curious.util._traverse_stack_for(t)[source]

Traverses the stack for an object of type t.

Parameters:t (type) – The type of the object.
Returns:The object, if found.
class curious.util.attrdict

Bases: dict

A dict that allows attribute access as well as item access for keys.

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

Returns a new dict with keys from iterable and values equal to value.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() → an object providing a view on D's values
curious.util.base64ify(image_data)[source]

Base64-ifys an image to send to discord.

Parameters:image_data (bytes) – The data of the image to use.
Returns:A string containing the encoded image.
await curious.util.coerce_agen(gen)[source]

Coerces an async generator into a list.

curious.util.deprecated(*, since, see_instead, removal)[source]

Marks a method as deprecated.

Parameters:
  • since (str) – The version since this is deprecated.
  • see_instead – What method to see instead.
  • removal (str) – The version this function will be removed at.
curious.util.remove_from_multidict(d, key, item)[source]

Removes an item from a multidict key.

curious.util.replace_quotes(item)[source]

Replaces the quotes in a string, but only if they are un-escaped.

some_weird_string = r'"this is quoted and removed" but " that was kept and this isn't \"'
replace_quotes(some_weird_string)  # 'this is quoted and removed but " that was kept but
this isnt \'
Parameters:item (str) – The string to scan.
Return type:str
Returns:The string, with quotes replaced.
curious.util.subclass_builtin(original)[source]

Subclasses an immutable builtin, providing method wrappers that return the subclass instead of the original.

curious.util.to_datetime(timestamp)[source]

Converts a Discord-formatted timestamp to a datetime object.

Parameters:timestamp (str) – The timestamp to convert.
Return type:datetime
Returns:The datetime.datetime object that corresponds to this datetime.