Yelp / MOE

A global, black box optimization engine for real world metric optimization.
Other
1.31k stars 139 forks source link

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

Open suntzu86 opened 9 years ago

suntzu86 commented 9 years ago

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 commented 9 years ago

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