Class Resolver 0.4.3-dev Documentation

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)

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.

Indices and Tables