New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional Arguments #86
Changes from all commits
c2b92ff
61a2b11
3a2d7aa
092ef48
df7e902
9b40350
1c23c42
dbcda0e
6013dea
fd4f375
350b531
c3adca7
844ff92
5af8268
9549572
5e8323c
3628519
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,14 @@ def _py_args_names(self, args_list): | |
"""Set the argument names in Pybind11 format.""" | ||
names = args_list.args_names() | ||
if names: | ||
py_args = ['py::arg("{}")'.format(name) for name in names] | ||
py_args = [] | ||
for arg in args_list.args_list: | ||
if arg.default and isinstance(arg.default, str): | ||
arg.default = "\"{arg.default}\"".format(arg=arg) | ||
argument = 'py::arg("{name}"){default}'.format( | ||
name=arg.name, | ||
default=' = {0}'.format(arg.default) if arg.default else '') | ||
py_args.append(argument) | ||
return ", " + ", ".join(py_args) | ||
else: | ||
return '' | ||
|
@@ -124,35 +131,29 @@ def _wrap_method(self, method, cpp_class, prefix, suffix, method_suffix=""): | |
suffix=suffix, | ||
)) | ||
|
||
# Create __repr__ override | ||
# We allow all arguments to .print() and let the compiler handle type mismatches. | ||
if method.name == 'print': | ||
# Redirect stdout - see pybind docs for why this is a good idea: | ||
# https://pybind11.readthedocs.io/en/stable/advanced/pycpp/utilities.html#capturing-standard-output-from-ostream | ||
ret = ret.replace('self->', 'py::scoped_ostream_redirect output; self->') | ||
ret = ret.replace('self->print', 'py::scoped_ostream_redirect output; self->print') | ||
|
||
# __repr__() uses print's implementation: | ||
type_list = method.args.to_cpp(self.use_boost) | ||
if len(type_list) > 0 and type_list[0].strip() == 'string': | ||
ret += '''{prefix}.def("__repr__", | ||
[](const {cpp_class} &a) {{ | ||
# Make __repr__() call print() internally | ||
ret += '''{prefix}.def("__repr__", | ||
[](const {cpp_class}& self{opt_comma}{args_signature_with_names}){{ | ||
gtsam::RedirectCout redirect; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then why do we still want this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I mean if we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should tackle this in another PR so that we don't inadvertently break something. 😕 |
||
a.print(""); | ||
self.{method_name}({method_args}); | ||
return redirect.str(); | ||
}}){suffix}'''.format( | ||
prefix=prefix, | ||
cpp_class=cpp_class, | ||
suffix=suffix, | ||
) | ||
else: | ||
ret += '''{prefix}.def("__repr__", | ||
[](const {cpp_class} &a) {{ | ||
gtsam::RedirectCout redirect; | ||
a.print(); | ||
return redirect.str(); | ||
}}){suffix}'''.format( | ||
prefix=prefix, | ||
cpp_class=cpp_class, | ||
suffix=suffix, | ||
) | ||
}}{py_args_names}){suffix}'''.format( | ||
prefix=prefix, | ||
cpp_class=cpp_class, | ||
opt_comma=', ' if args_names else '', | ||
args_signature_with_names=args_signature_with_names, | ||
method_name=method.name, | ||
method_args=", ".join(args_names) if args_names else '', | ||
py_args_names=py_args_names, | ||
suffix=suffix) | ||
|
||
return ret | ||
|
||
def wrap_methods(self, methods, cpp_class, prefix='\n' + ' ' * 8, suffix=''): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
%-------Constructors------- | ||
%MyFactorPosePoint2(size_t key1, size_t key2, double measured, Base noiseModel) | ||
% | ||
%-------Methods------- | ||
%print(string s, KeyFormatter keyFormatter) : returns void | ||
% | ||
classdef MyFactorPosePoint2 < handle | ||
properties | ||
ptr_MyFactorPosePoint2 = 0 | ||
|
@@ -29,6 +32,16 @@ function delete(obj) | |
%DISPLAY Calls print on the object | ||
function disp(obj), obj.display; end | ||
%DISP Calls print on the object | ||
function varargout = print(this, varargin) | ||
% PRINT usage: print(string s, KeyFormatter keyFormatter) : returns void | ||
% Doxygen can be found at https://gtsam.org/doxygen/ | ||
if length(varargin) == 2 && isa(varargin{1},'char') && isa(varargin{2},'gtsam.KeyFormatter') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's interesting that the print method is here. I think by default it should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not even touching the Matlab code right now because I am not the right person to do that. |
||
class_wrapper(55, this, varargin{:}); | ||
return | ||
end | ||
error('Arguments do not match any overload of function MyFactorPosePoint2.print'); | ||
end | ||
|
||
end | ||
|
||
methods(Static = true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
function varargout = TemplatedFunctionRot3(varargin) | ||
if length(varargin) == 1 && isa(varargin{1},'gtsam.Rot3') | ||
functions_wrapper(8, varargin{:}); | ||
functions_wrapper(11, varargin{:}); | ||
else | ||
error('Arguments do not match any overload of function TemplatedFunctionRot3'); | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you have this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand your comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm saw the next comment