| 68 | }; |
| 69 | |
| 70 | bp::object variable_init(bp::tuple args, bp::dict kwargs) { |
| 71 | bp::object self = args[0]; |
| 72 | |
| 73 | if (bp::len(args) < 2) { |
| 74 | PyErr_SetString(PyExc_TypeError, "require at least two arguments"); |
| 75 | bp::throw_error_already_set(); |
| 76 | } |
| 77 | |
| 78 | std::vector<double> v; |
| 79 | for (int i = 1, n = bp::len(args); i < n; ++i) { |
| 80 | v.push_back(bp::extract<double>(args[i])); |
| 81 | } |
| 82 | |
| 83 | boost::string_view label; |
| 84 | auto uo = bha::uoflow_type::on; |
| 85 | while (bp::len(kwargs) > 0) { |
| 86 | bp::tuple kv = kwargs.popitem(); |
| 87 | const char* key_cstr = bp::extract<const char*>(kv[0]); |
| 88 | boost::string_view k(key_cstr, bp::len(kv[0])); |
| 89 | bp::object v = kv[1]; |
| 90 | if (k == "label") |
| 91 | label = boost::string_view(bp::extract<const char*>(v), bp::len(v)); |
| 92 | else if (k == "uoflow") { |
| 93 | if (!bp::extract<bool>(v)) uo = bha::uoflow_type::off; |
| 94 | } else { |
| 95 | std::stringstream s; |
| 96 | s << "keyword " << k << " not recognized"; |
| 97 | PyErr_SetString(PyExc_KeyError, s.str().c_str()); |
| 98 | bp::throw_error_already_set(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return self.attr("__init__")(bha::variable<>(v.begin(), v.end(), label, uo)); |
| 103 | } |
| 104 | |
| 105 | bp::object category_init(bp::tuple args, bp::dict kwargs) { |
| 106 | bp::object self = args[0]; |