summaryrefslogtreecommitdiff
path: root/docs/examples/tutorial/cdef_classes
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/tutorial/cdef_classes')
-rw-r--r--docs/examples/tutorial/cdef_classes/integrate.py17
-rw-r--r--docs/examples/tutorial/cdef_classes/integrate.pyx31
-rw-r--r--docs/examples/tutorial/cdef_classes/math_function.py14
-rw-r--r--docs/examples/tutorial/cdef_classes/math_function_2.py5
-rw-r--r--docs/examples/tutorial/cdef_classes/math_function_2.pyx8
-rw-r--r--docs/examples/tutorial/cdef_classes/nonecheck.py20
-rw-r--r--docs/examples/tutorial/cdef_classes/nonecheck.pyx39
-rw-r--r--docs/examples/tutorial/cdef_classes/sin_of_square.py13
-rw-r--r--docs/examples/tutorial/cdef_classes/sin_of_square.pyx22
-rw-r--r--docs/examples/tutorial/cdef_classes/wave_function.py22
-rw-r--r--docs/examples/tutorial/cdef_classes/wave_function.pyx43
11 files changed, 161 insertions, 73 deletions
diff --git a/docs/examples/tutorial/cdef_classes/integrate.py b/docs/examples/tutorial/cdef_classes/integrate.py
new file mode 100644
index 000000000..cd02554e5
--- /dev/null
+++ b/docs/examples/tutorial/cdef_classes/integrate.py
@@ -0,0 +1,17 @@
+from cython.cimports.sin_of_square import Function, SinOfSquareFunction
+
+def integrate(f: Function, a: float, b: float, N: cython.int):
+ i: cython.int
+
+ if f is None:
+ raise ValueError("f cannot be None")
+
+ s: float = 0
+ dx: float = (b - a) / N
+
+ for i in range(N):
+ s += f.evaluate(a + i * dx)
+
+ return s * dx
+
+print(integrate(SinOfSquareFunction(), 0, 1, 10000))
diff --git a/docs/examples/tutorial/cdef_classes/integrate.pyx b/docs/examples/tutorial/cdef_classes/integrate.pyx
index a3bbcbfec..ad4c8540b 100644
--- a/docs/examples/tutorial/cdef_classes/integrate.pyx
+++ b/docs/examples/tutorial/cdef_classes/integrate.pyx
@@ -1,14 +1,17 @@
-from sin_of_square cimport Function, SinOfSquareFunction
-
-def integrate(Function f, double a, double b, int N):
- cdef int i
- cdef double s, dx
- if f is None:
- raise ValueError("f cannot be None")
- s = 0
- dx = (b - a) / N
- for i in range(N):
- s += f.evaluate(a + i * dx)
- return s * dx
-
-print(integrate(SinOfSquareFunction(), 0, 1, 10000))
+from sin_of_square cimport Function, SinOfSquareFunction
+
+def integrate(Function f, double a, double b, int N):
+ cdef int i
+ cdef double s, dx
+ if f is None:
+ raise ValueError("f cannot be None")
+
+ s = 0
+ dx = (b - a) / N
+
+ for i in range(N):
+ s += f.evaluate(a + i * dx)
+
+ return s * dx
+
+print(integrate(SinOfSquareFunction(), 0, 1, 10000))
diff --git a/docs/examples/tutorial/cdef_classes/math_function.py b/docs/examples/tutorial/cdef_classes/math_function.py
index 21281cc9b..1a6ee896c 100644
--- a/docs/examples/tutorial/cdef_classes/math_function.py
+++ b/docs/examples/tutorial/cdef_classes/math_function.py
@@ -1,7 +1,7 @@
-class MathFunction(object):
- def __init__(self, name, operator):
- self.name = name
- self.operator = operator
-
- def __call__(self, *operands):
- return self.operator(*operands)
+class MathFunction(object):
+ def __init__(self, name, operator):
+ self.name = name
+ self.operator = operator
+
+ def __call__(self, *operands):
+ return self.operator(*operands)
diff --git a/docs/examples/tutorial/cdef_classes/math_function_2.py b/docs/examples/tutorial/cdef_classes/math_function_2.py
new file mode 100644
index 000000000..ba5917639
--- /dev/null
+++ b/docs/examples/tutorial/cdef_classes/math_function_2.py
@@ -0,0 +1,5 @@
+@cython.cclass
+class Function:
+ @cython.ccall
+ def evaluate(self, x: float) -> float:
+ return 0
diff --git a/docs/examples/tutorial/cdef_classes/math_function_2.pyx b/docs/examples/tutorial/cdef_classes/math_function_2.pyx
index 1793ef689..a4bdb7aa2 100644
--- a/docs/examples/tutorial/cdef_classes/math_function_2.pyx
+++ b/docs/examples/tutorial/cdef_classes/math_function_2.pyx
@@ -1,3 +1,5 @@
-cdef class Function:
- cpdef double evaluate(self, double x) except *:
- return 0
+
+cdef class Function:
+
+ cpdef double evaluate(self, double x) except *:
+ return 0
diff --git a/docs/examples/tutorial/cdef_classes/nonecheck.py b/docs/examples/tutorial/cdef_classes/nonecheck.py
new file mode 100644
index 000000000..dccb97435
--- /dev/null
+++ b/docs/examples/tutorial/cdef_classes/nonecheck.py
@@ -0,0 +1,20 @@
+# cython: nonecheck=True
+# ^^^ Turns on nonecheck globally
+
+import cython
+
+@cython.cclass
+class MyClass:
+ pass
+
+# Turn off nonecheck locally for the function
+@cython.nonecheck(False)
+def func():
+ obj: MyClass = None
+ try:
+ # Turn nonecheck on again for a block
+ with cython.nonecheck(True):
+ print(obj.myfunc()) # Raises exception
+ except AttributeError:
+ pass
+ print(obj.myfunc()) # Hope for a crash!
diff --git a/docs/examples/tutorial/cdef_classes/nonecheck.pyx b/docs/examples/tutorial/cdef_classes/nonecheck.pyx
index b9e12c8d5..92c8fa42b 100644
--- a/docs/examples/tutorial/cdef_classes/nonecheck.pyx
+++ b/docs/examples/tutorial/cdef_classes/nonecheck.pyx
@@ -1,19 +1,20 @@
-# cython: nonecheck=True
-# ^^^ Turns on nonecheck globally
-
-import cython
-
-cdef class MyClass:
- pass
-
-# Turn off nonecheck locally for the function
-@cython.nonecheck(False)
-def func():
- cdef MyClass obj = None
- try:
- # Turn nonecheck on again for a block
- with cython.nonecheck(True):
- print(obj.myfunc()) # Raises exception
- except AttributeError:
- pass
- print(obj.myfunc()) # Hope for a crash!
+# cython: nonecheck=True
+# ^^^ Turns on nonecheck globally
+
+import cython
+
+
+cdef class MyClass:
+ pass
+
+# Turn off nonecheck locally for the function
+@cython.nonecheck(False)
+def func():
+ cdef MyClass obj = None
+ try:
+ # Turn nonecheck on again for a block
+ with cython.nonecheck(True):
+ print(obj.myfunc()) # Raises exception
+ except AttributeError:
+ pass
+ print(obj.myfunc()) # Hope for a crash!
diff --git a/docs/examples/tutorial/cdef_classes/sin_of_square.py b/docs/examples/tutorial/cdef_classes/sin_of_square.py
new file mode 100644
index 000000000..1904ea934
--- /dev/null
+++ b/docs/examples/tutorial/cdef_classes/sin_of_square.py
@@ -0,0 +1,13 @@
+from cython.cimports.libc.math import sin
+
+@cython.cclass
+class Function:
+ @cython.ccall
+ def evaluate(self, x: float) -> float:
+ return 0
+
+@cython.cclass
+class SinOfSquareFunction(Function):
+ @cython.ccall
+ def evaluate(self, x: float) -> float:
+ return sin(x ** 2)
diff --git a/docs/examples/tutorial/cdef_classes/sin_of_square.pyx b/docs/examples/tutorial/cdef_classes/sin_of_square.pyx
index 7aab96056..67af294b5 100644
--- a/docs/examples/tutorial/cdef_classes/sin_of_square.pyx
+++ b/docs/examples/tutorial/cdef_classes/sin_of_square.pyx
@@ -1,9 +1,13 @@
-from libc.math cimport sin
-
-cdef class Function:
- cpdef double evaluate(self, double x) except *:
- return 0
-
-cdef class SinOfSquareFunction(Function):
- cpdef double evaluate(self, double x) except *:
- return sin(x ** 2)
+from libc.math cimport sin
+
+
+cdef class Function:
+
+ cpdef double evaluate(self, double x) except *:
+ return 0
+
+
+cdef class SinOfSquareFunction(Function):
+
+ cpdef double evaluate(self, double x) except *:
+ return sin(x ** 2)
diff --git a/docs/examples/tutorial/cdef_classes/wave_function.py b/docs/examples/tutorial/cdef_classes/wave_function.py
new file mode 100644
index 000000000..7ff59a762
--- /dev/null
+++ b/docs/examples/tutorial/cdef_classes/wave_function.py
@@ -0,0 +1,22 @@
+from cython.cimports.sin_of_square import Function
+
+@cython.cclass
+class WaveFunction(Function):
+
+ # Not available in Python-space:
+ offset: float
+
+ # Available in Python-space:
+ freq = cython.declare(cython.double, visibility='public')
+
+ # Available in Python-space, but only for reading:
+ scale = cython.declare(cython.double, visibility='readonly')
+
+ # Available in Python-space:
+ @property
+ def period(self):
+ return 1.0 / self.freq
+
+ @period.setter
+ def period(self, value):
+ self.freq = 1.0 / value
diff --git a/docs/examples/tutorial/cdef_classes/wave_function.pyx b/docs/examples/tutorial/cdef_classes/wave_function.pyx
index aa35d954e..34b144667 100644
--- a/docs/examples/tutorial/cdef_classes/wave_function.pyx
+++ b/docs/examples/tutorial/cdef_classes/wave_function.pyx
@@ -1,21 +1,22 @@
-from sin_of_square cimport Function
-
-cdef class WaveFunction(Function):
-
- # Not available in Python-space:
- cdef double offset
-
- # Available in Python-space:
- cdef public double freq
-
- # Available in Python-space, but only for reading:
- cdef readonly double scale
-
- # Available in Python-space:
- @property
- def period(self):
- return 1.0 / self.freq
-
- @period.setter
- def period(self, value):
- self.freq = 1.0 / value
+from sin_of_square cimport Function
+
+
+cdef class WaveFunction(Function):
+
+ # Not available in Python-space:
+ cdef double offset
+
+ # Available in Python-space:
+ cdef public double freq
+
+ # Available in Python-space, but only for reading:
+ cdef readonly double scale
+
+ # Available in Python-space:
+ @property
+ def period(self):
+ return 1.0 / self.freq
+
+ @period.setter
+ def period(self, value):
+ self.freq = 1.0 / value