Re: [C++-sig] Conversion of python files to C++ ostreams

2010-04-01 Thread Christopher Bruns
On Tue, Mar 30, 2010 at 12:02 AM, Michele De Stefano
 wrote:
> there is a much easier way to treat a FILE* as a C++ stream. The easy
> way is to use my open source library (mds-utils,
> http://code.google.com/p/mds-utils/).

If I have a C++ api like:
  void foo(ostream& os);

using mds-utils, would I be able to wrap in python:

  foo(sys.stdout) # 1

or would the python side need to be something more like:

  foo(oFileObj(sys.stdout)) # 2
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Conversion of python files to C++ ostreams

2010-04-01 Thread Michele De Stefano
I think the second one, but I suggest to follow the example provided
with the doxygen documentation.

May be you will have to make a wrapper that takes a Python object as
argument, create an oFileObj object within it, and then, call your
function.

I don't have the time now to investigate the problem ... I'll return
on it tomorrow.

Bye,
Michele

2010/4/1 Christopher Bruns :
> On Tue, Mar 30, 2010 at 12:02 AM, Michele De Stefano
>  wrote:
>> there is a much easier way to treat a FILE* as a C++ stream. The easy
>> way is to use my open source library (mds-utils,
>> http://code.google.com/p/mds-utils/).
>
> If I have a C++ api like:
>  void foo(ostream& os);
>
> using mds-utils, would I be able to wrap in python:
>
>  foo(sys.stdout) # 1
>
> or would the python side need to be something more like:
>
>  foo(oFileObj(sys.stdout)) # 2
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://code.google.com/p/mds-utils
http://xoomer.virgilio.it/michele_de_stefano
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Conversion of python files to C++ ostreams

2010-04-01 Thread Christopher Bruns
On Tue, Mar 30, 2010 at 11:33 AM, Ralf W. Grosse-Kunstleve
 wrote:
> http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/boost_adaptbx/python_streambuf.h?view=markup

I have not seen this cctbx product before.  It especially interesting
to me, since I am performing molecular computations, and also because
I was a protein crystallographer for ten years.

I like that the streambuf class appears to work with gzip files and
other file objects that presumably do not have a FILE* internally.

Is it necessary to explicitly invoke the streambuf object from python?
 In other words, given C++ api:

  void foo(ostream&); // C++ API

Is it possible for the python api to be used like:

  foo(sys.stdout) # python invocation 1

or must it be

  foo(streambuf(sys.stdout))
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Conversion of python files to C++ ostreams

2010-04-01 Thread Michele De Stefano
Christopher,

I've figured out how you can use mds-utils.

The example is present into the documentation page of the
mds_utils::python::FileObj class.

So you have foo(ostream& os) and you would like to call it passing a
Python file object.

You cannot call

 foo(oFileObj(...))

directly, simply because there is no converter for converting from a
Python object into an oFileObj object.

So, as shown into the doxygen example, you have to program a wrapper
like this one:

foo_wrap(boost::python::object pyfile) {

 mds_utils::python::oFileObj  fobj(py_file);

 foo(fobj);
}

That's all, and it is quite simple.

I hope it helps.

Best regards.
Michele


2010/4/1 Christopher Bruns :
> On Tue, Mar 30, 2010 at 12:02 AM, Michele De Stefano
>  wrote:
>> there is a much easier way to treat a FILE* as a C++ stream. The easy
>> way is to use my open source library (mds-utils,
>> http://code.google.com/p/mds-utils/).
>
> If I have a C++ api like:
>  void foo(ostream& os);
>
> using mds-utils, would I be able to wrap in python:
>
>  foo(sys.stdout) # 1
>
> or would the python side need to be something more like:
>
>  foo(oFileObj(sys.stdout)) # 2
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Michele De Stefano
http://www.linkedin.com/in/micdestefano
http://code.google.com/p/mds-utils
http://xoomer.virgilio.it/michele_de_stefano
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Conversion of python files to C++ ostreams

2010-04-01 Thread Ralf W. Grosse-Kunstleve
> I like that the streambuf class appears to work with gzip files and

> other file objects that presumably do not have a FILE* internally.

Yes, because it simply uses the Python methods .read(), .write() etc.

> Is it necessary to explicitly invoke the streambuf object from python?

Yes. I could be different and in fact was different in the initial
implementation (if you look back in the svn history). But there were
a few subtle problems (buffer size, exception handling) and in the end
I decided the most obvious and robust approach is to require
streambuf(sys.stdout) or ostream(sys.stdout).
I think it is safe to re-use the same streambuf, e.g.

def foo():
  out = ostream(sys.stdout)
  func1(out)
  func2(out)

which is almost what you want.
But I'd avoid "out" as a global because output may get stuck in
the buffer and only appear surprisingly in unrelated contexts.
Note that buffer_size=1 introduces a noticeable performance
penalty. See time_it() in boost_adaptbx/tests/tst_python_streambuf.py .

Ralf
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] custom r-value converters

2010-04-01 Thread Nathan Stewart
On Mon, Mar 29, 2010 at 4:31 PM, Roman Yakovenko
wrote:

> May be I am missing something, but (py++) variable_t class doesn't
> have return_by_value property.
>
> Indeed it doesn't. I was having trouble figuring out how to create a
return_by_value property to pass to the make functions.

>I am not sure. What py++ version do you use
> Can you post small and complete example, of what you are trying to do?

I didn't post an example because it's essentially 'How to Wrap a Custom
String' from the boost.python FAQ in py++.
Here's an example - I'm using py++ 1.0 and trying to wrap the following:
class CustomString
{
   operator=(...)
   string& val;
};

struct TypeOne
{
CustomString name;
};

struct TypeTwo
{
CustomString label;
}

In my py++ script, I create the CustomString_to_pystring and
CustomString_from_pystring structs as in the boost.python docs, and register
the converter.
Where I'm getting a bit confused, is do I need to let py++ wrap the
CustomString class, or does the rvalue converter replace that?  I got around
the original property question by having my py++ script iterate through my
TypeN structs and if the type matches CustomString then I do a
property.exlude() and add the code using struct.add_registration_code() for
that property.  At least that generates registration code like this:

bp::class_("TypeOne")
.add_property("name",
 make_getter(&TypeOne::name,
bp::return_by_value_policy()),
 make_setter(&TypeOne::name,
bp::return_by_value_policy()))

Back to the question of wrapping CustomString though, if I exclude it
(thinking that exposing it was somehow messing with the rvalue conversion
stuff), it just segfaults. If I assign to my CustomString, it behaves as a
python string after that point. But until it has been assigned, it shows up
as .

My converters look like this:

struct CustomString_to_python_str
{
static PyObject* convert(CustomString const& s)
{
return
boost::python::incref(boost::python::object(const_cast(s)).ptr());
}
};

struct CustomString_from_python_str
{
CustomString_from_python_str()
{
boost::python::converter::registry::push_back(
&convertible,
&construct,
boost::python::type_id());
}

   static void* convertible(PyObject* obj_ptr)
{
if (!PyString_Check(obj_ptr)) return 0;
return obj_ptr;
}

static void construct(PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
const char* value = PyString_AsString(obj_ptr);
if (value == 0) boost::python::throw_error_already_set();
void* storage =
((boost::python::converter::rvalue_from_python_storage*)
data)->storage.bytes;
new (storage) CustomStringvalue);
data->convertible = storage;
}
}; // struct CustomString_from_python_str

And of course in the module init code, they're installed like so:

boost::python::to_python_converter();
CustomString_from_python_str();

The property getters & setters don't seem to be helpful or necessary - I
think now this might just be a default ctor initialization issue, but
providing initialization of the C++ class doesn't seem to work - it looks
like my converter is suspect.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig