Class Resolver 0.4.2 Documentation
class_resolver package helps you look up related classes and functions to parametrize your code.
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
CorrectAlgorithm. That’s because it knows the name of the base
Algorithm and it can infer what you mean.
Pass a string, class, or instance
Hint[Algorithm] signifies that the
algorithm_resolver.make(...) function is very powerful. You can pass
it one of the following:
A string, like
"greedy"and it deals with casing and the suffix
A class like
CorrectAlgorithm, or any potential subclass of
An instance of an
Algorithm, in case you have one pre-defined.
None, if you defined the
default=...` when you called ``ClassResolver.from_subclasseslike in