Calling python function repr from C++ using boost.python (possible?)
Is it possible to call repr from C++ on a C++ object that can be converted to python? Let me explain. I have an enum that I converted to python using boost::python::enum_(). That class already provides a good repr() to python (thanks to the boost::python developers for that). However that enum is used as a type on a member in another C++ class that I have where boost::python does not provide a good repr(). So I'm trying to provide one and thought, it sure would be nice if I could just call repr() on the member. I assume I have to convert the value on that member to python first, some kind of way then call repr? Is that possible? Or is there a better way? Example: Typedef enum { V1 = 0, V2 = 5, V3 = 6 } values; Class ValueExample { Public: ValueExample(const values& v) {m_value = v}; Values getValue() const { return m_value }; Private: Values m_value; } Std::string ValueExampleRepr(const ValueExample& object) { Std::stringstream ss; // What do I put here? // Ss << repr(object.getValue()); Return ss::str(); } Boost::python::enum_<values> values("values"); Values.value("V1", V1); Values.value("V2", V2); Values.value("V3", V3); Boost::python::class_<ValueExample> ValueExample("ValueExample"); ValueExample("__repr__", ValueExampleRepr);
On Sat, Mar 2, 2019 at 6:12 PM Jones, Torrin A (US) <torrin.jones@baesystems.com> wrote:
Is it possible to call repr from C++ on a C++ object that can be converted to python? Let me explain.
I have an enum that I converted to python using boost::python::enum_(). That class already provides a good repr() to python (thanks to the boost::python developers for that). However that enum is used as a type on a member in another C++ class that I have where boost::python does not provide a good repr(). So I’m trying to provide one and thought, it sure would be nice if I could just call repr() on the member. I assume I have to convert the value on that member to python first, some kind of way then call repr? Is that possible? Or is there a better way?
Example:
Typedef enum
{
V1 = 0,
V2 = 5,
V3 = 6
} values;
Class ValueExample
{
Public:
ValueExample(const values& v) {m_value = v};
Values getValue() const { return m_value };
Private:
Values m_value;
}
Std::string
ValueExampleRepr(const ValueExample& object)
{
Std::stringstream ss;
// What do I put here?
// Ss << repr(object.getValue());
Return ss::str();
}
Boost::python::enum_<values> values(“values”);
Values.value(“V1”, V1);
Values.value(“V2”, V2);
Values.value(“V3”, V3);
Boost::python::class_<ValueExample> ValueExample(“ValueExample”);
ValueExample(“__repr__”, ValueExampleRepr);
I can only come up with this: Ss << extract<std::string>(boost::python::object(object.getValue()).attr("__repr__")())();
This worked very well thanks. -----Original Message----- From: Stefan Ring [mailto:stefanrin@gmail.com] Sent: Tuesday, March 05, 2019 12:04 PM To: Development of Python/C++ integration <cplusplus-sig@python.org> Cc: Jones, Torrin A (US) <torrin.jones@baesystems.com> Subject: Re: [C++-sig] Calling python function repr from C++ using boost.python (possible?) *** WARNING *** EXTERNAL EMAIL -- This message originates from outside our organization. On Sat, Mar 2, 2019 at 6:12 PM Jones, Torrin A (US) <torrin.jones@baesystems.com> wrote:
Is it possible to call repr from C++ on a C++ object that can be converted to python? Let me explain.
I have an enum that I converted to python using boost::python::enum_(). That class already provides a good repr() to python (thanks to the boost::python developers for that). However that enum is used as a type on a member in another C++ class that I have where boost::python does not provide a good repr(). So I’m trying to provide one and thought, it sure would be nice if I could just call repr() on the member. I assume I have to convert the value on that member to python first, some kind of way then call repr? Is that possible? Or is there a better way?
Example:
Typedef enum
{
V1 = 0,
V2 = 5,
V3 = 6
} values;
Class ValueExample
{
Public:
ValueExample(const values& v) {m_value = v};
Values getValue() const { return m_value };
Private:
Values m_value;
}
Std::string
ValueExampleRepr(const ValueExample& object)
{
Std::stringstream ss;
// What do I put here?
// Ss << repr(object.getValue());
Return ss::str();
}
Boost::python::enum_<values> values(“values”);
Values.value(“V1”, V1);
Values.value(“V2”, V2);
Values.value(“V3”, V3);
Boost::python::class_<ValueExample> ValueExample(“ValueExample”);
ValueExample(“__repr__”, ValueExampleRepr);
I can only come up with this: Ss << extract<std::string>(boost::python::object(object.getValue()).attr("__repr__")())();
participants (2)
-
Jones, Torrin A (US) -
Stefan Ring