PyUtils

Python Utils

This package contains dependency-free Python utility functions used throughout the codebase.

Each utility should belong in its own file and be the default export.

These functions are not part of the module interface and are subject to change.

graphql.pyutils.camel_to_snake(s: str) str

Convert from CamelCase to snake_case

graphql.pyutils.snake_to_camel(s: str, upper: bool = True) str

Convert from snake_case to CamelCase

If upper is set, then convert to upper CamelCase, otherwise the first character keeps its case.

graphql.pyutils.cached_property(func: Callable) None

A cached property.

A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property.

graphql.pyutils.did_you_mean(suggestions: Sequence[str], sub_message: Optional[str] = None) str

Given [ A, B, C ] return ‘ Did you mean A, B, or C?’

graphql.pyutils.register_description(base: type) None

Register a class that shall be accepted as a description.

graphql.pyutils.unregister_description(base: type) None

Unregister a class that shall no more be accepted as a description.

class graphql.pyutils.SimplePubSub

Bases: object

A very simple publish-subscript system.

Creates an AsyncIterator from an EventEmitter.

Useful for mocking a PubSub system for tests.

__init__() None
emit(event: Any) bool

Emit an event.

get_subscriber(transform: Optional[Callable] = None) graphql.pyutils.simple_pub_sub.SimplePubSubIterator
subscribers: Set[Callable]
class graphql.pyutils.SimplePubSubIterator(pubsub: graphql.pyutils.simple_pub_sub.SimplePubSub, transform: Optional[Callable])

Bases: AsyncIterator

__init__(pubsub: graphql.pyutils.simple_pub_sub.SimplePubSub, transform: Optional[Callable]) None
async aclose() None
async empty_queue() None
async push_value(event: Any) None
graphql.pyutils.identity_func(x: graphql.pyutils.identity_func.T = Undefined, *_args: Any) graphql.pyutils.identity_func.T

Return the first received argument.

graphql.pyutils.inspect(value: Any) str

Inspect value and a return string representation for error messages.

Used to print values in error messages. We do not use repr() in order to not leak too much of the inner Python representation of unknown objects, and we do not use json.dumps() because not all objects can be serialized as JSON and we want to output strings with single quotes like Python repr() does it.

We also restrict the size of the representation by truncating strings and collections and allowing only a maximum recursion depth.

graphql.pyutils.is_finite(value: Any) bool

Return true if a value is a finite number.

graphql.pyutils.is_integer(value: Any) bool

Return true if a value is an integer number.

graphql.pyutils.AwaitableOrValue

alias of Union[Awaitable[graphql.pyutils.awaitable_or_value.T], graphql.pyutils.awaitable_or_value.T]

graphql.pyutils.suggestion_list(input_: str, options: Collection[str]) List[str]

Get list with suggestions for a given input.

Given an invalid input string and list of valid options, returns a filtered list of valid options sorted based on their similarity with the input.

class graphql.pyutils.FrozenError

Bases: TypeError

Error when trying to change a frozen (read only) collection.

class graphql.pyutils.FrozenList(iterable=(), /)

Bases: List[graphql.pyutils.frozen_list.T]

List that can only be read, but not changed.

class graphql.pyutils.FrozenDict

Bases: Dict[graphql.pyutils.frozen_dict.KT, graphql.pyutils.frozen_dict.VT]

Dictionary that can only be read, but not changed.

class graphql.pyutils.Path(prev: Any, key: Union[str, int], typename: Optional[str])

Bases: tuple

A generic path of string or integer indices

__init__()
add_key(key: Union[str, int], typename: Optional[str] = None) graphql.pyutils.path.Path

Return a new Path containing the given key.

as_list() List[Union[str, int]]

Return a list of the path keys.

count(value, /)

Return number of occurrences of value.

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

property key

current index in the path (string or integer)

property prev

path with the previous indices

property typename

name of the parent type to avoid path ambiguity

graphql.pyutils.print_path_list(path: Collection[Union[str, int]]) str

Build a string describing the path.

graphql.pyutils.Undefined = Undefined

Symbol for undefined values

This singleton object is used to describe undefined or invalid values. It can be used in places where you would use undefined in GraphQL.js.