Skip to content

Commit

Permalink
Use core's exception in Python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
BUYT-1 authored and polyntsov committed Oct 27, 2023
1 parent 6a6c804 commit c886b07
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/python_bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "algorithms/algorithms.h"
#include "algorithms/association_rules/ar.h"
#include "config/exceptions.h"
#include "config/tabular_data/input_table_type.h"
#include "py_ac_algorithm.h"
#include "py_ar_algorithm.h"
Expand Down Expand Up @@ -91,6 +92,9 @@ PYBIND11_MODULE(desbordante, module) {

module.doc() = "A data profiling library";

py::register_exception<config::ConfigurationError>(module, "ConfigurationError",
PyExc_ValueError);

py::class_<config::InputTable>(module, "Table");

py::class_<ARStrings>(module, "AssociativeRule")
Expand Down
4 changes: 3 additions & 1 deletion src/python_bindings/create_dataframe_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "create_dataframe_reader.h"

#include "config/exceptions.h"
#include "dataframe_reader.h"

namespace python_bindings {
Expand All @@ -24,7 +25,8 @@ static bool AllColumnsAreStrings(py::handle dataframe) {
}

config::InputTable CreateDataFrameReader(py::handle dataframe, std::string name) {
if (!IsDataFrame(dataframe)) throw std::invalid_argument("Passed object is not a dataframe");
if (!IsDataFrame(dataframe))
throw config::ConfigurationError("Passed object is not a dataframe");
if (AllColumnsAreStrings(dataframe)) {
return std::make_shared<StringDataframeReader>(dataframe, std::move(name));
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/python_bindings/py_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <boost/any.hpp>

#include "algorithms/algo_factory.h"
#include "config/exceptions.h"
#include "config/names.h"
#include "config/tabular_data/input_table_type.h"
#include "create_dataframe_reader.h"
Expand Down Expand Up @@ -62,8 +63,8 @@ std::unordered_set<std::string_view> PyAlgorithmBase::GetPossibleOptions() const
py::tuple PyAlgorithmBase::GetOptionType(std::string_view option_name) const {
auto type_index = algorithm_->GetTypeIndex(option_name);
if (type_index == void_index)
throw std::invalid_argument{std::string{"Option named \""} + option_name.data() +
"\" doesn't exist!"};
throw config::ConfigurationError{std::string{"Option named \""} + option_name.data() +
"\" doesn't exist!"};
return GetPyType(type_index);
}

Expand Down
9 changes: 5 additions & 4 deletions src/python_bindings/py_to_any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "algorithms/algebraic_constraints/bin_operation_enum.h"
#include "algorithms/metric/enums.h"
#include "association_rules/ar_algorithm_enums.h"
#include "config/exceptions.h"
#include "config/tabular_data/input_table_type.h"
#include "create_dataframe_reader.h"
#include "parser/csv_parser/csv_parser.h"
Expand All @@ -24,15 +25,15 @@ T CastAndReplaceCastError(std::string_view option_name, py::handle value) {
try {
return py::cast<T>(value);
} catch (py::cast_error& e) {
throw std::invalid_argument(
throw config::ConfigurationError(
std::string("Unable to cast Python object to C++ type for option \"") +
option_name.data() + '"');
}
}

config::InputTable CreateCsvParser(std::string_view option_name, py::tuple const& arguments) {
if (py::len(arguments) != 3) {
throw std::invalid_argument("Cannot create a csv parser from passed tuple.");
throw config::ConfigurationError("Cannot create a CSV parser from passed tuple.");
}

return std::make_shared<CSVParser>(
Expand All @@ -58,7 +59,7 @@ std::pair<std::type_index, ConvFunc> const EnumConvPair{
std::stringstream error_message;
error_message << "Incorrect value for option \"" << option_name
<< "\". Possible values: " << util::EnumToAvailableValues<EnumType>();
throw std::invalid_argument(error_message.str());
throw config::ConfigurationError(error_message.str());
}};

template <typename EnumType>
Expand All @@ -83,7 +84,7 @@ std::pair<std::type_index, ConvFunc> const CharEnumConvPair{
error_message.seekp(-1, std::stringstream::cur);
error_message << ']';

throw std::invalid_argument(error_message.str());
throw config::ConfigurationError(error_message.str());
}};

boost::any InputTableToAny(std::string_view option_name, py::handle obj) {
Expand Down

0 comments on commit c886b07

Please sign in to comment.