Commit 8ae84a55 authored by ggellner@basestar's avatar ggellner@basestar

Applied patches to fix typos. Also added an hgignore, and the .static files...

Applied patches to fix typos. Also added an hgignore, and the .static files needed to generate output.
parent a8312bd9
syntax: glob
*.pyc
*~
.*.swp
syntax: regexp
^build/
...@@ -209,25 +209,26 @@ Here's a complete example. It defines a property which adds to a list each ...@@ -209,25 +209,26 @@ Here's a complete example. It defines a property which adds to a list each
time it is written to, returns the list when it is read, and empties the list time it is written to, returns the list when it is read, and empties the list
when it is deleted.:: when it is deleted.::
#cheesy.pyx Test input # cheesy.pyx
cdef class CheeseShop: cdef class CheeseShop:
cdef object cheeses cdef object cheeses
def __cinit__(self): def __cinit__(self):
self.cheeses = [] self.cheeses = []
property cheese: property cheese:
def __get__(self): def __get__(self):
return "We don't have: %s" % self.cheeses return "We don't have: %s" % self.cheeses
def __set__(self, value): def __set__(self, value):
self.cheeses.append(value) self.cheeses.append(value)
def __del__(self): def __del__(self):
del self.cheeses[:] del self.cheeses[:]
# Test input
from cheesy import CheeseShop from cheesy import CheeseShop
shop = CheeseShop() shop = CheeseShop()
...@@ -242,7 +243,7 @@ when it is deleted.:: ...@@ -242,7 +243,7 @@ when it is deleted.::
del shop.cheese del shop.cheese
print shop.cheese print shop.cheese
#Test output # Test output
We don't have: [] We don't have: []
We don't have: ['camembert'] We don't have: ['camembert']
We don't have: ['camembert', 'cheddar'] We don't have: ['camembert', 'cheddar']
...@@ -280,18 +281,17 @@ functions, C methods are declared using :keyword:`cdef` instead of ...@@ -280,18 +281,17 @@ functions, C methods are declared using :keyword:`cdef` instead of
:keyword:`def`. C methods are "virtual", and may be overridden in derived :keyword:`def`. C methods are "virtual", and may be overridden in derived
extension types.:: extension types.::
pets.pyx # pets.pyx
Output
cdef class Parrot: cdef class Parrot:
cdef void describe(self): cdef void describe(self):
print "This parrot is resting." print "This parrot is resting."
cdef class Norwegian(Parrot): cdef class Norwegian(Parrot):
cdef void describe(self): cdef void describe(self):
Parrot.describe(self) Parrot.describe(self)
print "Lovely plumage!" print "Lovely plumage!"
cdef Parrot p1, p2 cdef Parrot p1, p2
...@@ -301,7 +301,9 @@ extension types.:: ...@@ -301,7 +301,9 @@ extension types.::
p1.describe() p1.describe()
print "p2:" print "p2:"
p2.describe() p2.describe()
p1:
# Output
p1:
This parrot is resting. This parrot is resting.
p2: p2:
This parrot is resting. This parrot is resting.
......
...@@ -293,18 +293,16 @@ can then be called and the extension types used as usual. ...@@ -293,18 +293,16 @@ can then be called and the extension types used as usual.
Any public C type or extension type declarations in the Cython module are also Any public C type or extension type declarations in the Cython module are also
made available when you include :file:`modulename_api.h`.:: made available when you include :file:`modulename_api.h`.::
delorean.pyx # delorean.pyx
marty.c
cdef public struct Vehicle: cdef public struct Vehicle:
int speed int speed
float power float power
cdef api void activate(Vehicle *v): cdef api void activate(Vehicle *v):
if v.speed >= 88 and v.power >= 1.21: if v.speed >= 88 and v.power >= 1.21:
print "Time travel achieved" print "Time travel achieved"
# marty.c
#include "delorean_api.h" #include "delorean_api.h"
Vehicle car; Vehicle car;
......
...@@ -327,7 +327,7 @@ body, and the loop may have an else clause. ...@@ -327,7 +327,7 @@ body, and the loop may have an else clause.
Error return values Error return values
------------------- -------------------
If you don't do anything special, a function declared with :keyword`cdef` that If you don't do anything special, a function declared with :keyword:`cdef` that
does not return a Python object has no way of reporting Python exceptions to does not return a Python object has no way of reporting Python exceptions to
its caller. If an exception is detected in such a function, a warning message its caller. If an exception is detected in such a function, a warning message
is printed and the exception is ignored. is printed and the exception is ignored.
...@@ -520,7 +520,7 @@ Such expressions are made up of literal values and names defined using ``DEF`` ...@@ -520,7 +520,7 @@ Such expressions are made up of literal values and names defined using ``DEF``
statements, combined using any of the Python expression syntax. statements, combined using any of the Python expression syntax.
The following compile-time names are predefined, corresponding to the values The following compile-time names are predefined, corresponding to the values
returned by :func:``os.uname``. returned by :func:`os.uname`.
UNAME_SYSNAME, UNAME_NODENAME, UNAME_RELEASE, UNAME_SYSNAME, UNAME_NODENAME, UNAME_RELEASE,
UNAME_VERSION, UNAME_MACHINE UNAME_VERSION, UNAME_MACHINE
......
...@@ -108,7 +108,7 @@ python. One can declare variables and return values for functions to be of the ...@@ -108,7 +108,7 @@ python. One can declare variables and return values for functions to be of the
:ctype:`bint` type. For example:: :ctype:`bint` type. For example::
cdef int i = x cdef int i = x
Cdef bint b = x cdef bint b = x
The first conversion would happen via ``x.__int__()`` whereas the second would The first conversion would happen via ``x.__int__()`` whereas the second would
happen via ``x.__nonzero__()``. (Actually, if ``x`` is the python object happen via ``x.__nonzero__()``. (Actually, if ``x`` is the python object
...@@ -156,7 +156,7 @@ method on the class directly, e.g.:: ...@@ -156,7 +156,7 @@ method on the class directly, e.g.::
cdef class A: cdef class A:
cpdef foo(self): cpdef foo(self):
pass pass
x = A() x = A()
x.foo() # will check to see if overridden x.foo() # will check to see if overridden
...@@ -197,10 +197,10 @@ In Cython ``<type>x`` will try and do a coercion (as would happen on assignment ...@@ -197,10 +197,10 @@ In Cython ``<type>x`` will try and do a coercion (as would happen on assignment
It does not stop one from casting where there is no conversion (though it will It does not stop one from casting where there is no conversion (though it will
emit a warning). If one really wants the address, cast to a ``void *`` first. emit a warning). If one really wants the address, cast to a ``void *`` first.
As in Pyrex ``<MyExtensionType>x`` will cast ``x`` to type <ctype>`MyExtensionType` without any As in Pyrex ``<MyExtensionType>x`` will cast ``x`` to type :ctype:`MyExtensionType` without any
type checking. Cython supports the syntax ``<MyExtensionType?>`` to do the cast type checking. Cython supports the syntax ``<MyExtensionType?>`` to do the cast
with type checking (i.e. it will throw an error if ``x`` is not a (subclass of) with type checking (i.e. it will throw an error if ``x`` is not a (subclass of)
<ctype>`MyExtensionType`. :ctype:`MyExtensionType`.
Optional arguments in cdef/cpdef functions Optional arguments in cdef/cpdef functions
------------------------------------------ ------------------------------------------
......
...@@ -16,7 +16,12 @@ suffix, containing C declarations that are to be available to other Cython ...@@ -16,7 +16,12 @@ suffix, containing C declarations that are to be available to other Cython
modules, and an implementation file with a ``.pyx`` suffix, containing modules, and an implementation file with a ``.pyx`` suffix, containing
everything else. When a module wants to use something declared in another everything else. When a module wants to use something declared in another
module's definition file, it imports it using the :keyword:`cimport` module's definition file, it imports it using the :keyword:`cimport`
statement. What a Definition File contains A definition file can contain: statement.
What a Definition File contains
-------------------------------
A definition file can contain:
* Any kind of C type declaration. * Any kind of C type declaration.
* extern C function or variable declarations. * extern C function or variable declarations.
...@@ -77,9 +82,9 @@ uses it. ...@@ -77,9 +82,9 @@ uses it.
d.filler = dishes.sausage d.filler = dishes.sausage
def serve(): def serve():
spamdish d cdef spamdish d
prepare(&d) prepare(&d)
print "%d oz spam, filler no. %d" % (d->oz_of_spam, d->otherstuff) print "%d oz spam, filler no. %d" % (d.oz_of_spam, d.otherstuff)
It is important to understand that the :keyword:`cimport` statement can only It is important to understand that the :keyword:`cimport` statement can only
be used to import C data types, C functions and variables, and extension be used to import C data types, C functions and variables, and extension
...@@ -139,12 +144,12 @@ C functions defined at the top level of a module can be made available via ...@@ -139,12 +144,12 @@ C functions defined at the top level of a module can be made available via
:keyword:`cimport` by putting headers for them in the ``.pxd`` file, for :keyword:`cimport` by putting headers for them in the ``.pxd`` file, for
example,: example,:
:file:`volume.pxd`: :file:`volume.pxd`::
:file:`spammery.pyx`::
cdef float cube(float) cdef float cube(float)
:file:`spammery.pyx`::
from volume cimport cube from volume cimport cube
def menu(description, size): def menu(description, size):
...@@ -185,17 +190,22 @@ Python methods. ...@@ -185,17 +190,22 @@ Python methods.
Here is an example of a module which defines and exports an extension type, Here is an example of a module which defines and exports an extension type,
and another module which uses it.:: and another module which uses it.::
Shrubbing.pxd Shrubbing.pyx # Shrubbing.pxd
cdef class Shrubbery: cdef class Shrubbery:
cdef int width cdef int width
cdef int length cdef class Shrubbery: cdef int length
# Shrubbing.pyx
cdef class Shrubbery:
def __new__(self, int w, int l): def __new__(self, int w, int l):
self.width = w self.width = w
self.length = l self.length = l
def standard_shrubbery(): def standard_shrubbery():
return Shrubbery(3, 7) return Shrubbery(3, 7)
Landscaping.pyx
# Landscaping.pyx
cimport Shrubbing cimport Shrubbing
import Shrubbing import Shrubbing
......
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