8 ms·
other projects in that space are: cython (need to manually do wrapping), xdress, SWIG, SIP, clif. Does anyone have experience with using these and able to comp
by ivan_ah 9y ago
other projects in that space are: cython (need to manually do wrapping), xdress, SWIG, SIP, clif.
Does anyone have experience with using these and able to compare/contrast?
I'm afraid of C++, but if I can wrap it and learn how to use from a python REPL, I think I can handle... any recommendations/tuotirals/howtos would be much appreciated. (The library I want to wrap is https://github.com/openzim/libzim https://github.com/openzim/libzim )
- tehsauce 9y agoI used pybind11 for my open source asteroid detection code. It's very slick, especially it's ability to seamlessly convert between stl and python lists- for example a vector of strings converts directly to a python list of strings. Also the interoperability with numpy. A class containing an stl of primitive types and the dimensions can easily be turned into a numpy array. Very neat
- tehsauce 9y agohttps://github.com/DiracInstitute/kbmod https://github.com/DiracInstitute/kbmod The project I was referring to :)
- first_amendment 9y agoI've used cython, swig, and boost.python. I prefer pybind11 to all of them. It requires the least extra tooling and allows you to seamlessly transition between C++ and Python using the idioms native to each language.
- ThePhysicist 9y agoI built bindings for various C++ libraries in SWIG (not only for Python but also Lua and Ruby) and I was always very impressed with the result. What's amazing about SWIG is its ability to easily port very complex and rich code. I successfully exported very complex class hierarchies and code relying heavily on pointers and STL to Python and/or Lua. The process consists of writing several header-like configuration files that SWIG ingests and uses to generate bindings, which is often pretty straightforward. Often you can simply import the normal C/C++ header files in those configs and add some glue code / extra hints to help SWIG in cases where it can't figure out what to do with a particular type. In my experience, the most difficult aspect of binding generation is not writing the glue code, but doing so in a scalable way. Manually writing bindings for any real-world C++ codebase would therefore be extremely tedious (IMHO), so having an automated system that does this work for you is a huge time saver. Some larger libraries / frameworks have their own wrapping generators Btw, PyQt (which provides extremely good bindings to QT) for example has SIP which is worth looking at (it's open source).
- piokuc 9y agoWhat's also important is to have a simple, neat API, ideally C API. If you do have it, then writing wrappers for any language, even manually, without SWIG (which is great, but does add complexity to your project's build process), is simple.
- ThePhysicist 9y agoYes that's a valid point, manually writing bindings for C++ is very hard though (IMHO), as there are many intricacies that you have to keep in mind, and SWIG is really good at automating all of these away. For C-code the story is a bit different because usually the code is much less complex.
- brational 9y agoHave you used pybind? I've used SWIG extensively so I'm familiar with that - you talked about SWIG but not how it compares to this new one. Reading through some docs, it looks like pybind suits better for a python codebase that occasionally needs C++ pieces added in as you have to setup the C++ code to handle the py needs. SWIG I was always impressed with exactly your point. You could sometimes just make 1 big header file in a facade pattern to expose some "start" buttons needed to run a huge c++ application and nothing else needed to be setup. No going back and forth. Example usage here was a single heavy duty image processing application but using python has the distributor and work manager. C++ code was a "worker" and python and zeroMQ did everything else. So the only "connection" we needed was a start button and some metadata to the c++. Maybe 1 header file with 100-200 lines of code to make the bridge for SWIG. (to anyone else reading) does that sound trivially simple to the do the same with pybind?
- ThePhysicist 9y agoI haven't used pybind yet, so I can't compare the two unfortunately. From how I understand the docs it seems you would have to write the interface definitions in C++ and the compiler would generate the bindings for you, which sounds like a nice approach that could be a bit more cumbersome though as (again, in my understanding) forces you to write and adapt all bindings by hand.
- unkown-unknowns 9y agoAlso, Boost.Python. http://www.boost.org/doc/libs/1_64_0/libs/python/doc/html/index.html http://www.boost.org/doc/libs/1_64_0/libs/python/doc/html/in... A proprietary piece of software we use at work has a Python API for which they are using Boost.Python. I've written a piece of software to get some data from the API and to analyze the retrieved data. The Python API has a bit of a "C++ smell" to it and doesn't feel completely pythonic but it's good enough. The bigger problem is that the vendor's idea of documentation is a single PDF generated from the class definitions and everything is either very sparesly or not at all commented, heh.
- aldanor 9y agoIn fact, pybind11 was heavily influenced by Boost.Python, to the point where some of the syntax may look quite similar. It has one obvious advantage though, in that it doesn't depend on Boost :)
- jcelerier 9y agoHere's how the pybind11 bindings of a c++ lib I work on look: https://github.com/OSSIA/libossia/blob/master/OSSIA/ossia-python/ossia_python.cpp#L173 https://github.com/OSSIA/libossia/blob/master/OSSIA/ossia-py... as you can see, it's just a matter of declaring the classes and methods you want to wrap. It was very straightforward and works extremely well.
- frumiousirc 9y agoROOT https://root.cern.ch/ https://root.cern.ch/ provides Python bindings through its PyROOT functionality. It builds the bindings (ROOT calls them "dictionaries") by parsing your C++ using their "cling" interpreter based on LLVM. As far as usability, ROOT is a big package but once a given it's rather easy and non-intrusive to create Python bindings for your C++.
- autopoiesis 9y agoAlso as teajunky notes below, cppyy (which was born out of PyROOT) is quite nice: http://cppyy.readthedocs.io/en/latest/ http://cppyy.readthedocs.io/en/latest/
- xioxox 9y agoI've used SIP, which wasn't too painful. The code you write to do the interfacing looks quite like the C++ definitions. I mostly used it as was an application which used PyQt. It probably is harder to use as it's not the most popular option.