From: Barry S. <ba...@ba...> - 2019-11-10 13:08:27
|
> On 9 Nov 2019, at 20:18, Anton Milev <am...@ab...> wrote: > > Hello, Bary. > > Thanks! I will risk to ask for some improvements even if I do not understand completely PyCXX. I will happy if you answer any of it > > 1. Can you add a VS2017/19 Step-by-Step demo? I actually started using PyCXX with VS2017 so if you like I can help with this. Sorry I'm not a Visual Studio GUI user. I use the VS C++ from the command line. > 2. With VS 2017 I had to search for and add all .cxx files from "C:\PyCXX\Src" (cxx_extensions.cxx,. ...) to the VS project. This is not convenient. > Is it possible to create with the build scripts a single lib file and I to include in my project only the libs path (from Linker > General->Additional Library Directories) ? It is not practical to provide as a lib. This is because you have to compile the code with the C++ options that you have chosen for the rest of your project. For example some projects want RTTI and others do not. You might use Python limiter API or not. > It is similar to how Python libs are included in VS, you only include <Python.h> and set the link path. > 3. I am confused with the demos, simple.cxx is not simple at all and is hard for me to start using it. Simple is relative... What I mean by simple is that its shows the simplest way to create a module, class and function, etc. > Please advice me in the following case - > > I have a C++ class A. It is quite complicated and contains a lot of functions and code. I want to export this class A to Python. > > Is this the right approach: > > class OldClass: public Py::PythonExtension< OldClass > > > Look at Demo/Python3/simple.cxx and follow the pattern of the new_style_class. You are using the older style of classes that is not recommended for new code. > { > > public: > > OldClass() > > { > > } > > > virtual ~OldClass() > > { > > } > > > static void init_type(void) > > { > > behaviors().name("simple.OldClass"); > > behaviors().doc("documentation for OldClass class"); > > behaviors().supportGetattr(); > > > add_noargs_method("func1",&OldClass::func1); > > add_varargs_method("func2",&OldClass::func2); > > add_keyword_method("func3",&OldClass::func3); > > > behaviors().readyType(); > > } > > > // override functions from PythonExtension > > virtual Py::Object getattr(const char *name) > > { > > return getattr_methods(name); > > } > > > Py::Object func1(void) > > { > > std::cout << "func1 Called." << std::endl; > > return Py::None(); > > } > > > Py::Object func2(const Py::Tuple &args) > > { > > std::cout << "func2 Called with " << args.length() << " normal arguments." << std::endl; > > return Py::None(); > > } > > > > private: > > CppClass m_cpp ; > > }; > > 4. I have in my class A many overload operators and I want to use them in Python too. > > For example: > > >>> import simple > >>> a = simple.OldClass() > >>> a.func1() > func1 Called. > >>> a.func2(5) > func2 Called with 1 normal arguments. > >>> b = a > > So far Ok but how to do with PyCXX support the following operators: > > > >>> b += a > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: unsupported operand type(s) for +=: 'simple.OldClass' and 'simple.OldClass' You have to implement the number_inplace_add. See Demo/Python3/simple.cxx (I just added support for inplace add today. Please get check out the source: svn co https://svn.code.sf.net/p/cxx/code/trunk/CXX <https://svn.code.sf.net/p/cxx/code/trunk/CXX> > > >>> c = simple.OldClass() > >>> a = b + c > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: unsupported operand type(s) for +: 'simple.OldClass' and 'simple.OldClass' You have to implement the number_add method. See Demo/Python3/simple.cxx > > > My purpose is to expose and use in Python my C++ class as a new type. I need support for different constructors, like: > > >>> a = simple..OldClass("a") > >>> a = simple..OldClass(5) > No problem look at simple.cxx and the c'tor for new_style_class. new_style_class( Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds ) You can handle args and kwds as you need to implement your API. > > I hope my questions are not too confusing Barry |