diff --git a/src/python_bindings/opt_to_str.cpp b/src/python_bindings/opt_to_str.cpp new file mode 100644 index 0000000000..16226ada9c --- /dev/null +++ b/src/python_bindings/opt_to_str.cpp @@ -0,0 +1,69 @@ +#pragma once +#include "opt_to_str.h" +namespace { +using ConvFunction = std::function; + +template +std::pair normal_conv_pair{ + std::type_index(typeid(T)), + [](boost::any value) { return std::to_string(boost::any_cast(value)); } +}; + +template <> +std::pair normal_conv_pair{ + std::type_index(typeid(bool)), + [](boost::any value) { return boost::any_cast(value) ? "True" : "False"; } +}; + +template <> +std::pair normal_conv_pair{ + std::type_index(typeid(config::IndicesType)), + [](boost::any value) { + auto opt_value = (boost::any_cast(value)); + if (opt_value.size() == 0) { + return std::string("[]"); + } + std::string str_value = "["; + for (int i = 0; i < opt_value.size() - 1; i++) { + str_value += std::to_string(opt_value.at(i)) + ", "; + } + str_value += std::to_string(opt_value.back()) + "]"; + return str_value; + } +}; +template <> +std::pair normal_conv_pair{ + std::type_index(typeid(algos::metric::MetricAlgo)), + [](boost::any value){ + auto opt_value = boost::any_cast(value); + return opt_value._to_string(); + } +}; +template <> +std::pair normal_conv_pair{ + std::type_index(typeid(algos::metric::Metric)), + [](boost::any value){ + auto opt_value = boost::any_cast(value); + return opt_value._to_string(); + } +}; +const std::unordered_map converters{ + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair, + normal_conv_pair +}; +} // namespace + +namespace python_bindings{ + std::string opt_to_str(std::type_index type, boost::any val) { + return converters.at(type)(val); + } +} // namespace python_bindings \ No newline at end of file diff --git a/src/python_bindings/opt_to_str.h b/src/python_bindings/opt_to_str.h new file mode 100644 index 0000000000..886c264ee6 --- /dev/null +++ b/src/python_bindings/opt_to_str.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +#include "algorithms/algorithm.h" +#include "algorithms/metric/enums.h" +#include "config/equal_nulls/type.h" +#include "config/error/type.h" +#include "config/indices/type.h" +#include "config/max_lhs/type.h" +#include "config/tabular_data/input_table_type.h" +#include "config/thread_number/type.h" +#include "config/names.h" +#include "model/types/types.h" + +namespace python_bindings{ + std::string opt_to_str(std::type_index type, boost::any val); +} // namespace python_bindings \ No newline at end of file diff --git a/src/python_bindings/py_algorithm.h b/src/python_bindings/py_algorithm.h index d2afc6e628..4f12d819d2 100644 --- a/src/python_bindings/py_algorithm.h +++ b/src/python_bindings/py_algorithm.h @@ -16,72 +16,10 @@ #include "config/max_lhs/type.h" #include "config/tabular_data/input_table_type.h" #include "config/thread_number/type.h" +#include "config/names.h" #include "model/types/types.h" -namespace { -using ConvFunction = std::function; +#include "opt_to_str.h" -template -std::pair normal_conv_pair{ - std::type_index(typeid(T)), - [](boost::any value) { return std::to_string(boost::any_cast(value)); } -}; - -template <> -std::pair normal_conv_pair{ - std::type_index(typeid(bool)), - [](boost::any value) { return boost::any_cast(value) ? "True" : "False"; } -}; - -template <> -std::pair normal_conv_pair{ - std::type_index(typeid(config::IndicesType)), - [](boost::any value) { - auto opt_value = (boost::any_cast(value)); - if (opt_value.size() == 0) { - return std::string("[]"); - } - std::string str_value = "["; - for (int i = 0; i < opt_value.size() - 1; i++) { - str_value += std::to_string(opt_value.at(i)) + ", "; - } - str_value += std::to_string(opt_value.back()) + "]"; - return str_value; - } -}; -template <> -std::pair normal_conv_pair{ - std::type_index(typeid(algos::metric::MetricAlgo)), - [](boost::any value){ - auto opt_value = boost::any_cast(value); - return opt_value._to_string(); - } -}; -template <> -std::pair normal_conv_pair{ - std::type_index(typeid(algos::metric::Metric)), - [](boost::any value){ - auto opt_value = boost::any_cast(value); - return opt_value._to_string(); - } -}; -const std::unordered_map converters{ - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair, - normal_conv_pair -}; - -std::string opt_to_str(std::type_index type, boost::any val) { - return converters.at(type)(val); -} -} // namespace namespace python_bindings { class PyAlgorithmBase { @@ -123,7 +61,7 @@ class PyAlgorithmBase { auto opt_ = algorithm_->GetOptValues(); std::unordered_map res; for (const auto& [name,value] : opt_) { - if (name == "table" || name == "input_format") { + if (name == config::names::kTable || name == config::names::kInputFormat) { continue; } res[std::string(name)] = opt_to_str(value->type, value->value);