Skip to content
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

Better python printing #81

Closed

Conversation

gchenfc
Copy link
Member

@gchenfc gchenfc commented Apr 7, 2021

Since gtsam::KeyFormatter is a boost::function and pybind11 does not do boost::functions, some modification is needed in order to be able to use the print_(string s, KeyFormatter) function. Specifically, the print_ function is altered so that the python interface accepts a std::function and then calls the gtsam print with the boost version. So for example:

class ClassWithPrint {
  void print(const string &s,
             const gtsam::KeyFormatter &keyFormatter);
};
...
        .def("print_",
                    [](gtsam::ClassWithPrint* self, const string& s, const std::function<std::string(gtsam::Key)>& keyFormatter) {
                        py::scoped_ostream_redirect stream;
                        self->print(s, [&keyFormatter](gtsam::Key key){return keyFormatter(key);});
                    }, py::arg("s"), py::arg("keyFormatter"))

Note: py::scoped_ostream_redirect stream; is so that printing occurs to python sys.stdout which just generally plays nicer with python. See pybind docs

Also, this allows functionals for the keyformatter:

def myKeyFormatter(key):
    return 'this is my key formatter {}'.format(key)
factor.print_('factor: ', myKeyFormatter)

outputs:

factor: min torque factor
  keys = { this is my key formatter 23924273508777984 }
  noise model: unit (1) 

Copy link
Collaborator

@varunagrawal varunagrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review. I am confused why not just add support for functionals?
https://pybind11.readthedocs.io/en/stable/advanced/cast/functional.html

Also, you've mixed up two PRs. Please separate, or wait till I add functional support, or even better, discuss?

# ~~~
# destination: The concatenated master interface header file will be placed here.
# inputs (optional): All the input interface header files
function(combine_interface_headers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with this? Not part of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad... temporarily i've just changed the base of this PR, but if this goes through before the other PR then I'll make a new branch/PR to merge

@@ -126,6 +126,23 @@ def _wrap_method(self, method, cpp_class, prefix, suffix, method_suffix=""):

if method.name == 'print':
type_list = method.args.to_cpp(self.use_boost)
if len(type_list) == 2 \
and 'string' in type_list[0] \
and 'gtsam::KeyFormatter' in type_list[1]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this gtsam::KeyFormatter and not a functional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand your question - gtsam::KeyFormatter is typedef'd to boost::function, so it is a functional? Unless that's not what a functional is...

if len(type_list) == 2 \
and 'string' in type_list[0] \
and 'gtsam::KeyFormatter' in type_list[1]:
ret = '''{prefix}.def("print_",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a good idea. If a class defines a print() method, this might override that (or the other way around).

@gchenfc gchenfc changed the base branch from master to feature/multiple_interface_files April 7, 2021 18:44
@gchenfc
Copy link
Member Author

gchenfc commented Apr 7, 2021

Once borglab/gtsam#735 merges, this will no longer be necessary so I'm closing this PR.

@gchenfc gchenfc closed this Apr 7, 2021
@varunagrawal varunagrawal deleted the feature/print_keyformatter branch August 31, 2023 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants