diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-04-17 00:49:33 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-04-17 00:49:33 +0300 |
commit | 2183f3f860af34e45058ef078045322062b51f42 (patch) | |
tree | 1f61c51be301db314720fe4a945d6ddaaffa8249 /algorithms/impl/__init__.py | |
parent | README update (diff) | |
download | sorting-algorithms-2183f3f860af34e45058ef078045322062b51f42.tar.gz sorting-algorithms-2183f3f860af34e45058ef078045322062b51f42.zip |
rearrange source files
* Add a useful `algorithms` package to provide convinient access to the
implemented algorithms.
* This allows to e.g. dynamically list available algorithms, which
greatly simplifies a lot of things.
Diffstat (limited to 'algorithms/impl/__init__.py')
-rw-r--r-- | algorithms/impl/__init__.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/algorithms/impl/__init__.py b/algorithms/impl/__init__.py new file mode 100644 index 0000000..29cd51c --- /dev/null +++ b/algorithms/impl/__init__.py @@ -0,0 +1,29 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +_ALL_ALGORITHMS = {} + +def _refresh_algorithms(): + _ALGORITHMS_NAME = '_ALGORITHMS' + global _ALL_ALGORITHMS + _ALL_ALGORITHMS = {} + + from algorithms.algorithm import Algorithm + + from importlib import import_module + import os.path + from pkgutil import iter_modules + + for _, module_name, is_pkg in iter_modules([os.path.dirname(__file__)]): + if is_pkg: + continue + module = import_module('.' + module_name, __package__) + if hasattr(module, _ALGORITHMS_NAME): + module_algorithms = getattr(module, _ALGORITHMS_NAME) + for algorithm in module_algorithms: + assert isinstance(algorithm, Algorithm) + assert algorithm.get_codename() not in _ALL_ALGORITHMS + _ALL_ALGORITHMS[algorithm.get_codename()] = algorithm + +_refresh_algorithms() |