Merge branch 'pr/659'

Fixes #659
This commit is contained in:
Ian Cordasco
2015-11-19 17:25:36 -06:00
9 changed files with 139 additions and 24 deletions
-10
View File
@@ -8,16 +8,6 @@
<p><a href="http://tinyletter.com/kennethreitz">Subscribe to Newsletter</a></p>
<h3>Donate</h3>
<p>
If you enjoy this guide, consider supporting the author <a href="https://www.gittip.com/kennethreitz/">on Gittip</a>:
</p>
<p>
<iframe style="border: 0; margin: 0; padding: 0;"
src="https://www.gittip.com/kennethreitz/widget.html"
width="60pt" height="20pt"></iframe>
</p>
<h3>Contributors</h3>
<p>
This guide is the result of the collaboration of
-10
View File
@@ -7,13 +7,3 @@
<p>Receive updates on new releases and upcoming projects.</p>
<p><a href="http://tinyletter.com/kennethreitz">Subscribe to Newsletter</a></p>
<h3>Donate</h3>
<p>
If you enjoy this guide, consider supporting the author <a href="https://www.gittip.com/kennethreitz/">on Gittip</a>:
</p>
<p>
<iframe style="border: 0; margin: 0; padding: 0;"
src="https://www.gittip.com/kennethreitz/widget.html"
width="60pt" height="20pt"></iframe>
</p>
+1
View File
@@ -63,6 +63,7 @@ different scenarios.
scenarios/xml
scenarios/json
scenarios/crypto
scenarios/clibs
Shipping Great Code
+2
View File
@@ -1,3 +1,5 @@
.. _virtualenvironments-ref:
Virtual Environments
====================
+129
View File
@@ -0,0 +1,129 @@
Interfacing with C/C++ Libraries
================================
C Foreign Function Interface
----------------------------
`CFFI <https://cffi.readthedocs.org/en/latest/>`_ provides a simple to use
mechanism for interfacing with C from both CPython and PyPy. It supports two
modes: an inline ABI compatibility mode (example provided below), which allows
you to dynamically load and run functions from executable modules (essentially
exposing the same functionality as LoadLibrary or dlopen), and an API mode,
which allows you to build C extension modules.
ABI Interaction
~~~~~~~~~~~~~~~
.. code-block:: python
:linenos:
from cffi import FFI
ffi = FFI()
ffi.cdef("size_t strlen(const char*);")
clib = ffi.dlopen(None)
length = clib.strlen("String to be evaluated.")
# prints: 23
print("{}".format(length))
ctypes
------
`ctypes <https://docs.python.org/3/library/ctypes.html>`_ is the de facto
library for interfacing with C/C++ from CPython, and it provides not only
full access to the native C interface of most major operating systems (e.g.,
kernel32 on Windows, or libc on \*nix), but also provides support for loading
and interfacing with dynamic libraries, such as DLLs or shared objects at
runtime. It does bring along with it a whole host of types for interacting
with system APIs, and allows you to rather easily define your own complex
types, such as structs and unions, and allows you to modify things such as
padding and alignment, if needed. It can be a bit crufty to use, but in
conjunction with the `struct <https://docs.python.org/3.5/library/struct.html>`_
module, you are essentially provided full control over how your data types get
translated into something something usable by a pure C(++) method.
Struct Equivalents
~~~~~~~~~~~~~~~~~~
:file:`MyStruct.h`
.. code-block:: c
:linenos:
struct my_struct {
int a;
int b;
};
:file:`MyStruct.py`
.. code-block:: python
:linenos:
import ctypes
class my_struct(ctypes.Structure):
_fields_ = [("a", c_int),
("b", c_int)]
SWIG
----
`SWIG <http://www.swig.org>`_, though not strictly Python focused (it supports a
large number of scripting languages), is a tool for generating bindings for
interpreted languages from C/C++ header files. It is extremely simple to use:
the consumer simply needs to define an interface file (detailed in the
tutorial and documentations), include the requisite C/C++ headers, and run
the build tool against them. While it does have some limits, (it currently
seems to have issues with a small subset of newer C++ features, and getting
template-heavy code to work can be a bit verbose), it provides a great deal
of power and exposes lots of features to Python with little effort.
Additionally, you can easily extend the bindings SWIG creates (in the
interface file) to overload operators and built-in methods, effectively re-
cast C++ exceptions to be catchable by Python, etc.
Example: Overloading __repr__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:file:`MyClass.h`
.. code-block:: c++
:linenos:
#include <string>
class MyClass {
private:
std::string name;
public:
std::string getName();
};
:file:`myclass.i`
.. code-block:: c++
:linenos:
%include "string.i"
%module myclass
%{
#include <string>
#include "MyClass.h"
%}
%extend MyClass {
std::string __repr__()
{
return $self->getName();
}
}
%include "MyClass.h"
Boost.Python
------------
`Boost.Python <http://www.boost.org/doc/libs/1_59_0/libs/python/doc/>`_
requires a bit more manual work to expose C++ object functionality, but
it is capable of providing all the same features SWIG does and then some,
to include providing wrappers to access PyObjects in C++, extracting SWIG-
wrapper objects, and even embedding bits of Python into your C++ code.
+4 -1
View File
@@ -38,7 +38,10 @@ parse it using the ``html`` module and save the results in ``tree``:
.. code-block:: python
page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
tree = html.fromstring(page.text)
tree = html.fromstring(page.content)
(We need to use ``page.content`` rather than ``page.text`` because
``html.fromstring`` implicitly expects ``bytes`` as input.)
``tree`` now contains the whole HTML file in a nice tree structure which
we can go over two different ways: XPath and CSSSelect. In this example, we
+1 -1
View File
@@ -58,7 +58,7 @@ your global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.3 while also
maintaining a project which requires Django 1.0.
To start using and see more information: `Virtual Environments <http://github.com/kennethreitz/python-guide/blob/master/docs/dev/virtualenvs.rst>`_ docs.
To start using this and see more information: :ref:`Virtual Environments <virtualenvironments-ref>` docs.
You can also use :ref:`virtualenvwrapper <virtualenvwrapper-ref>` to make it easier to
manage your virtual environments.
+1 -1
View File
@@ -92,7 +92,7 @@ your global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.3 while also
maintaining a project which requires Django 1.0.
To start using and see more information: `Virtual Environments <http://github.com/kennethreitz/python-guide/blob/master/docs/dev/virtualenvs.rst>`_ docs.
To start using this and see more information: :ref:`Virtual Environments <virtualenvironments-ref>` docs.
--------------------------------
+1 -1
View File
@@ -77,7 +77,7 @@ your global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.3 while also
maintaining a project which requires Django 1.0.
To start using and see more information: `Virtual Environments <http://github.com/kennethreitz/python-guide/blob/master/docs/dev/virtualenvs.rst>`_ docs.
To start using this and see more information: :ref:`Virtual Environments <virtualenvironments-ref>` docs.
--------------------------------