MOE icon indicating copy to clipboard operation
MOE copied to clipboard

Use boost::python::arg to set up kwargs

Open suntzu86 opened this issue 10 years ago • 1 comments

Right now, all the boost python wrapped C++ functions take positional arguments only. Not only that but the arguments aren't named so error messages and autogen'd docs can be pretty unhelpful. This also lets us set default values!

boost::python::arg (in the args.hpp header) can instruct boost set up kwargs for us. Then our wrapped functions will behave exactly like Python functions with named arguments.

Let's convert our functions.

For example, in autogen docs instead of:

__init__( (object)arg1, (int)arg2, (int)arg3, (float)arg4, (float)arg5, (float)arg6), (float)arg7) -> None :

we could have:

__init__( (object)arg1, (int)num_multistarts, (int)max_num_steps, (float)gamma, (float)time_factor, (float)max_relative_change, (float)tolerance) -> None :

note (object)arg1 is self since these are class member functions.

See: http://stackoverflow.com/questions/16660049/extracting-arguments-from-kwargs-in-boostpython http://www.boost.org/doc/libs/1_57_0/libs/python/doc/v2/args.html

Note, in the stream boost::python::arg("a"), "b", "c", "d" creates arglist (a, b, c, d) the call to arg is implied due to overloading of the comma operator. If you want to set default values, arg must be re-called, e.g.,: boost::python::arg("a")=1, "b", "c", boost::python::arg("d")=2 creates a function with arglist a=1, b, c, d=2

suntzu86 avatar Nov 22 '14 03:11 suntzu86

This is already done for the constructors of NewtonParameters and GradientDescentParameters through #138.

suntzu86 avatar Nov 22 '14 03:11 suntzu86