Usage

class_resolver Package

The class_resolver package helps you look up related classes and functions to parametrize your code.

Getting Started

An example might be when you have several implementations of the same algorithm (e.g., fast, greedy one and a slow but correct one) and want to write a function run_algorithm that can be easily switched between them with a string corresponding to the name of the implementation

from class_resolver import ClassResolver, Hint


class Algorithm:
    def run(self, data): ...


class GreedyAlgorithm(Algorithm): ...


class CorrectAlgorithm(Algorithm): ...


algorithm_resolver = ClassResolver.from_subclasses(Algorithm)


def run_algorithm(data, *, algorithm: Hint[Algorithm] = "greedy"):
    algorithm = algorithm_resolver.make(algorithm)
    return algorithm.run(data)

Note that the string keys in the class resolver are "greedy" for GreedyAlgorithm and "correct" for CorrectAlgorithm. That’s because it knows the name of the base class is Algorithm and it can infer what you mean.

Pass a string, class, or instance

The Hint[Algorithm] signifies that the algorithm_resolver.make(...) function is very powerful. You can pass it one of the following:

  1. A string, like "GreedyAlgorithm" or "Greedy" or "greedy" and it deals with casing and the suffix

  2. A class like GreedyAlgorithm, CorrectAlgorithm, or any potential subclass of Algorithm

  3. An instance of an Algorithm, in case you have one pre-defined.

  4. None, if you defined the default=...` when you called ``ClassResolver.from_subclasses like in ClassResolver.from_subclasses(Algorithm, default=GreedyAlgorithm.

Functions

get_cls(query, base, lookup_dict[, ...])

Get a class by string, default, or implementation.

get_subclasses(cls[, exclude_private, ...])

Get all subclasses.

normalize_string(s, *[, suffix])

Normalize a string for lookup.

update_docstring_with_resolver_keys(...)

Build a decorator to add information about resolved parameter pairs.

Classes

BaseResolver([elements, default, synonyms, ...])

A resolver for arbitrary elements.

ClassResolver([classes, default, suffix, ...])

Resolve from a list of classes.

FunctionResolver([elements, default, ...])

A resolver for functions.

KeywordArgumentError(cls, s)

Thrown when missing a keyword-only argument.

RegistrationError(resolver, key, proposed, label)

Raised when trying to add a new element to a resolver with a pre-existing lookup key.

RegistrationNameConflict(resolver, key, ...)

Raised on a conflict with the lookup dict.

RegistrationSynonymConflict(resolver, key, ...)

Raised on a conflict with the synonym dict.

Resolver

alias of ClassResolver

ResolverKey(name, resolver[, key])

An object storing information about how a resolver is used in a signature.

UnexpectedKeywordError(cls)

Thrown when no arguments were expected.

Class Inheritance Diagram

digraph inheritanceb8471bf304 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "ABC" [URL="https://docs.python.org/3/library/abc.html#abc.ABC",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Helper class that provides a standard way to create an ABC using"]; "BaseResolver" [URL="api/class_resolver.BaseResolver.html#class_resolver.BaseResolver",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A resolver for arbitrary elements."]; "ABC" -> "BaseResolver" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Generic" -> "BaseResolver" [arrowsize=0.5,style="setlinewidth(0.5)"]; "ClassResolver" [URL="api/class_resolver.ClassResolver.html#class_resolver.ClassResolver",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Resolve from a list of classes."]; "BaseResolver" -> "ClassResolver" [arrowsize=0.5,style="setlinewidth(0.5)"]; "FunctionResolver" [URL="api/class_resolver.FunctionResolver.html#class_resolver.FunctionResolver",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A resolver for functions."]; "BaseResolver" -> "FunctionResolver" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Generic" [URL="https://docs.python.org/3/library/typing.html#typing.Generic",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Abstract base class for generic types."]; "KeywordArgumentError" [URL="api/class_resolver.KeywordArgumentError.html#class_resolver.KeywordArgumentError",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Thrown when missing a keyword-only argument."]; "RegistrationError" [URL="api/class_resolver.RegistrationError.html#class_resolver.RegistrationError",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Raised when trying to add a new element to a resolver with a pre-existing lookup key."]; "Generic" -> "RegistrationError" [arrowsize=0.5,style="setlinewidth(0.5)"]; "ABC" -> "RegistrationError" [arrowsize=0.5,style="setlinewidth(0.5)"]; "RegistrationNameConflict" [URL="api/class_resolver.RegistrationNameConflict.html#class_resolver.RegistrationNameConflict",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Raised on a conflict with the lookup dict."]; "RegistrationError" -> "RegistrationNameConflict" [arrowsize=0.5,style="setlinewidth(0.5)"]; "RegistrationSynonymConflict" [URL="api/class_resolver.RegistrationSynonymConflict.html#class_resolver.RegistrationSynonymConflict",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Raised on a conflict with the synonym dict."]; "RegistrationError" -> "RegistrationSynonymConflict" [arrowsize=0.5,style="setlinewidth(0.5)"]; "ResolverKey" [URL="api/class_resolver.ResolverKey.html#class_resolver.ResolverKey",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="An object storing information about how a resolver is used in a signature."]; "UnexpectedKeywordError" [URL="api/class_resolver.UnexpectedKeywordError.html#class_resolver.UnexpectedKeywordError",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Thrown when no arguments were expected."]; }