OS: CentOS 6 (64bit)
I have a dynamic library (.so) in C. And I want to create an abstraction layer of Python over it and then use it to implement my logic. I have decided to use CFFI for this since it doesn't deal with any kind of dsl (domain specific language).
Couple of things I wanted to know:
Is there some good starting point which I can refer for doing this (loading and using dynamic libraries using cffi)? The docs on the official site talk about this, but I was looking if there was some concrete reference somewhere with some examples. Or someone who might have tried it.
Can there a possible drawback to this approach?
Thanks
Two good starting points:
The CFFI documentation, and specifically the ABI out of line example: https://cffi.readthedocs.org/en/latest/overview.html#out-of-line-example-abi-level-out-of-line
My CFFI example repository: https://github.com/wolever/python-cffi-example
Between the two you shouldn't have too much trouble putting together your wrapper.
And to your second question: if the shared library you're wrapping is very simple (ex, a few function calls, simle data structures) you might find ctypes simpler (as it's part of the standard library).
Related
I would like to build my project in Scala and then use it in a script in Python for my data hacking (as a module or something like that). I have seen that there are ways to integrate python code into JVM languages with Jython (only Python 2 projects though). What I want to do is the other way around though. I found no information on the net how to do this, but it seems strange that this should not be possible.
General solution -- use some RPC/IPC (sockets, protobuf, whatever).
However, you might want to look at Spark's solution -- how they translate Python code in Scala's APIs (https://www.py4j.org/) .
Recently, scalapy was created to call Python libraries from Scala.
https://github.com/shadaj/scalapy
In writing some Python code, I came upon a need for a string-like data structure that offers fast insertion into, access to, and deletion from arbitrary positions. The first data structure that came to mind was a rope. Does Python have a rope data structure already implemented somewhere? I've looked through the standard library and PyPI, but I haven't seen one. (It doesn't help that there's a refactoring library for Python by the name of Rope as well as a company called Python Rope that sells wire rope.)
There isn't one in the standard library, but there are implementations out there, e.g. pyropes.
There's also this list of various non-built-in data structure implementations for Python.
Yes! there is one package available at PyPI.org for Rope data structure(named pyropes), written purely in Python 3. You can install it using
pip install pyropes
It also have full documentation on how to use it. Though it requires Python >=3.6(because it uses f-strings)
I am looking for a good Python binding to ImageMagick, but there seem a lot of bindings already. I am not sure that which of these is the right tool for my job. Can you guys recommend me one?
Here is the list of my requirements and preferences (in order of importance):
Must be available on PyPI (to simplify our deployment)
Prefer ctypes over C API extension — we will go PyPy soon
Pythonic API design and naming conventions
Good documentation (especially API references)
I found the package myself: magickwand is a ctypes-based ImageMagick binding for Python. Yet it has no documentation at all, it still satisfies most of my requirements.
Plus: I finally started my own project: Wand.
I am new at writing APIs in python, in any language for that matter. I was hoping to get pointers on how i can create an API that can be installed using setup.py method and used in other python projects. Something similar to the twitterapi.
I have already created and coded all the methods i want to include in the API. I just need to know how to implement the installation so other can use my code to leverage ideas they may have. Or if i need to format the code a certain way to facilitate installation.
I learn best with examples or tutorials.
Thanks so much.
It's worth noting that this part of python is undergoing some changes right now. It's all a bit messy. The most current overview I know of is the Hitchhiker's Guide to Packaging: http://guide.python-distribute.org/
The current state of packaging section is important: http://guide.python-distribute.org/introduction.html#current-state-of-packaging
The python packaging world is a mess (like poswald said). Here's a brief overview along with a bunch of pointers. Your basic problem (using setup.py etc.) is solved by reading the distutils guide which msw has mentioned in his comment.
Now for the dirt. The basic infrastructure of the distribution modules which is in the Python standard library is distutils referred to above. It's limited in some ways and so a series of extensions was written on top of it called setuptools. Setuptools along with actually increasing the functionality provided a command line "installer" called "easy_install".
Setuptools maintenance was not too great and so it was forked and a more active branch called "distribute" was setup and it is the preferred alternative right now. In addition to this, a replacement for easy_install named pip was created which was more modular and useful.
Now there's a huge project going which attempts to fold in all changes from distribute and stuff into a unified library that will go into the stdlib. It's tentatively called "distutils2".
I need a library to handle computational geometry in a project, especially boolean operations, but just about every feature is useful. The best library I can find for this is CGAL, but this is the sort of project I would hesitate to make without garbage collection.
What language/library pairs can you recommend? So far my best bet is importing CGAL into D. There is also a project for making Python bindings for CGAL, but it's very incomplete.
I would still recommend to proceed with Python and the existing Python binding. When you find it's incomplete, you'll also find that it is fairly easy to extend - Python's C API is designed so that integrating with external libraries is fairly easy (for experienced C programmers).
Perhaps you can look at Shapely for python
http://pypi.python.org/pypi/Shapely/
For Java I would use JTS
For .NET I would use SharpMap or .NETTopologySuite
The CGAL-bindings project provides bindings for CGAL using SWIG. The targeted languages, so far, are Java and Python. The CGAL-bindings project is open source, and supported/founded by two french companies.
JTS is also available in .NET via IKVM.
I've just found this and it seems very promising even if it seems a young project: https://pyrr.readthedocs.org/en/latest/index.html#
Pyrr is a Python mathematical library.
and it is based on numpy!