Commit 82f95375 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved two examples from sharing_declarations.rst to the examples directory.

parent 084a25f5
cdef extern from "lunch.h":
void eject_tomato(float)
cimport shrubbing
import shrubbing
def main():
cdef shrubbing.Shrubbery sh
sh = shrubbing.standard_shrubbery()
print("Shrubbery size is", sh.width, 'x', sh.length)
cimport c_lunch
def eject_tomato(float speed):
c_lunch.eject_tomato(speed)
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize(["landscaping.pyx", "shrubbing.pyx"]))
cdef class Shrubbery:
cdef int width
cdef int length
cdef class Shrubbery:
def __cinit__(self, int w, int l):
self.width = w
self.length = l
def standard_shrubbery():
return Shrubbery(3, 7)
......@@ -147,17 +147,13 @@ for an imaginary module, and :keyword:`cimport` that module. You can then
refer to the C functions by qualifying them with the name of the module.
Here's an example:
:file:`c_lunch.pxd`::
:file:`c_lunch.pxd`:
cdef extern from "lunch.h":
void eject_tomato(float)
.. literalinclude:: ../../examples/userguide/sharing_declarations/c_lunch.pxd
:file:`lunch.pyx`::
:file:`lunch.pyx`:
cimport c_lunch
def eject_tomato(float speed):
c_lunch.eject_tomato(speed)
.. literalinclude:: ../../examples/userguide/sharing_declarations/lunch.pyx
You don't need any :file:`c_lunch.pyx` file, because the only things defined
in :file:`c_lunch.pxd` are extern C entities. There won't be any actual
......@@ -222,38 +218,23 @@ Python methods.
Here is an example of a module which defines and exports an extension type,
and another module which uses it:
:file:`Shrubbing.pxd`::
cdef class Shrubbery:
cdef int width
cdef int length
:file:`Shrubbing.pyx`::
:file:`shrubbing.pxd`:
cdef class Shrubbery:
def __cinit__(self, int w, int l):
self.width = w
self.length = l
.. literalinclude:: ../../examples/userguide/sharing_declarations/shrubbing.pxd
def standard_shrubbery():
return Shrubbery(3, 7)
:file:`shrubbing.pyx`:
:file:`Landscaping.pyx`::
.. literalinclude:: ../../examples/userguide/sharing_declarations/shrubbing.pyx
cimport Shrubbing
import Shrubbing
:file:`landscaping.pyx`:
cdef Shrubbing.Shrubbery sh
sh = Shrubbing.standard_shrubbery()
print("Shrubbery size is %d x %d" % (sh.width, sh.length))
.. literalinclude:: ../../examples/userguide/sharing_declarations/landscaping.pyx
One would then need to compile both of these modules, e.g. using
:file:`setup.py`::
:file:`setup.py`:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["Landscaping.pyx", "Shrubbing.pyx"]))
.. literalinclude:: ../../examples/userguide/sharing_declarations/setup.py
Some things to note about this example:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment