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.

Classes

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

A resolver for arbitrary elements.

Resolver

alias of ClassResolver

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

Resolve from a list of classes.

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

A resolver for functions.

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.

KeywordArgumentError(cls, s)

Thrown when missing a keyword-only argument.

UnexpectedKeywordError(cls)

Thrown when no arguments were expected.

Class Inheritance Diagram

Inheritance diagram of class_resolver.base.BaseResolver, class_resolver.api.ClassResolver, class_resolver.api.ClassResolver, class_resolver.func.FunctionResolver, class_resolver.base.RegistrationError, class_resolver.base.RegistrationNameConflict, class_resolver.base.RegistrationSynonymConflict, class_resolver.api.KeywordArgumentError, class_resolver.api.UnexpectedKeywordError