List Info

Thread: Boost.Python and std::string subclassing




Boost.Python and std::string subclassing
country flaguser name
Finland
2008-05-05 07:56:09
The library I am wrapping creates a subclass TCEString of
std::string.
Py++ generates me bindings for the subclass, but in Python
the
subclass does not quite work like std::string. For example,
the Python expression

"%s.%s.%s" % (term.functionUnit().name(),
term.port().name(), 
term.operation().name())

returns the string

"universal_fu.OCSetting32.<TCE.tools.TCEString
object at 0x81308ec>"

because term.operation().name() returns a TCEString instead
of std::string.

Is there some convenient way to make the subclass work like
std::string?
I know how to do it by defining a __str__ method in the
Python class,
but I am hoping that there might be a simpler way to do it
using
Boost.Python or Py++.
-- 
Pertti
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: Boost.Python and std::string subclassing
user name
2008-05-05 13:12:34
On Mon, May 5, 2008 at 3:56 PM, Pertti Kellomäki
<pertti.kellomakitut.fi> wrote:
> The library I am wrapping creates a subclass TCEString
of std::string.
>  Py++ generates me bindings for the subclass, but in
Python the
>  subclass does not quite work like std::string. For
example,
>  the Python expression
>
>  "%s.%s.%s" % (term.functionUnit().name(),
term.port().name(),
> term.operation().name())
>
>  returns the string
>
>  "universal_fu.OCSetting32.<TCE.tools.TCEString
object at 0x81308ec>"
>
>  because term.operation().name() returns a TCEString
instead of std::string.
>
>  Is there some convenient way to make the subclass work
like std::string?
>  I know how to do it by defining a __str__ method in
the Python class,
>  but I am hoping that there might be a simpler way to
do it using
>  Boost.Python or Py++.

I am not sure I fully understand you.

Do you want to add new method "__str__" to
TCEString ? If so take a
look on this document:
http://language-binding.net/pyplusplus/d
ocumentation/inserting_code.html

If you want to "hide" from user class TCEString
and to define your
interface in terms of Python string than:
1. create custom converter (
http://www.boost.org/doc/libs/1_35
_0/libs/python/doc/v2/faq.html#custom_string
)
2. In Py++ script mark TCEString classs as "already
exposed", so you
will not get warnings. See
http://languag
e-binding.net/pyplusplus/documentation/multi_module_developm
ent.html#already-exposed
for an explanation.

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-
binding.net/
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: Boost.Python and std::string subclassing
user name
2008-05-05 14:18:59
Roman Yakovenko wrote:
> I am not sure I fully understand you.
> 
> Do you want to add new method "__str__" to
TCEString ? If so take a
> look on this document:
> http://language-binding.net/pyplusplus/d
ocumentation/inserting_code.html

Sorry for being obtuse. Since Boost.Python converts
std::string
to Python strings, I was hoping that I could easily tell
either
Boost.Python or Py++ to do the same thing for TCEString, by
the virtue of it being a subclass of std::string.
-- 
Pertti
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: Boost.Python and std::string subclassing
user name
2008-05-05 15:22:56
On Mon, May 5, 2008 at 10:18 PM, Pertti Kellomäki
<pertti.kellomakitut.fi> wrote:
> Roman Yakovenko wrote:
>
> > I am not sure I fully understand you.
> >
> > Do you want to add new method "__str__"
to TCEString ? If so take a
> > look on this document:
> > http://language-binding.net/pyplusplus/d
ocumentation/inserting_code.html
> >
>
>  Sorry for being obtuse.

You don't have, really 

> Since Boost.Python converts std::string
>  to Python strings, I was hoping that I could easily
tell either
>  Boost.Python or Py++ to do the same thing for
TCEString, by
>  the virtue of it being a subclass of std::string.

In this case, my second receipt will work for you.


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-
binding.net/
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )