summaryrefslogtreecommitdiff
path: root/docs/examples/tutorial/pure
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/tutorial/pure')
-rw-r--r--docs/examples/tutorial/pure/A.py28
-rw-r--r--docs/examples/tutorial/pure/A_equivalent.pyx30
-rw-r--r--docs/examples/tutorial/pure/annotations.py10
-rw-r--r--docs/examples/tutorial/pure/c_arrays.py30
-rw-r--r--docs/examples/tutorial/pure/cclass.py32
-rw-r--r--docs/examples/tutorial/pure/compiled_switch.py12
-rw-r--r--docs/examples/tutorial/pure/cython_declare.py8
-rw-r--r--docs/examples/tutorial/pure/cython_declare2.py6
-rw-r--r--docs/examples/tutorial/pure/disabled_annotations.py33
-rw-r--r--docs/examples/tutorial/pure/dostuff.py10
-rw-r--r--docs/examples/tutorial/pure/exceptval.py14
-rw-r--r--docs/examples/tutorial/pure/locals.py12
-rw-r--r--docs/examples/tutorial/pure/mymodule.py20
-rw-r--r--docs/examples/tutorial/pure/pep_526.py44
-rw-r--r--docs/examples/tutorial/pure/py_cimport.py5
15 files changed, 166 insertions, 128 deletions
diff --git a/docs/examples/tutorial/pure/A.py b/docs/examples/tutorial/pure/A.py
index 1e0cea950..98d5530c8 100644
--- a/docs/examples/tutorial/pure/A.py
+++ b/docs/examples/tutorial/pure/A.py
@@ -1,14 +1,14 @@
-def myfunction(x, y=2):
- a = x - y
- return a + x * y
-
-def _helper(a):
- return a + 1
-
-class A:
- def __init__(self, b=0):
- self.a = 3
- self.b = b
-
- def foo(self, x):
- print(x + _helper(1.0))
+def myfunction(x, y=2):
+ a = x - y
+ return a + x * y
+
+def _helper(a):
+ return a + 1
+
+class A:
+ def __init__(self, b=0):
+ self.a = 3
+ self.b = b
+
+ def foo(self, x):
+ print(x + _helper(1.0))
diff --git a/docs/examples/tutorial/pure/A_equivalent.pyx b/docs/examples/tutorial/pure/A_equivalent.pyx
index 1b256a010..ab9e0081c 100644
--- a/docs/examples/tutorial/pure/A_equivalent.pyx
+++ b/docs/examples/tutorial/pure/A_equivalent.pyx
@@ -1,15 +1,15 @@
-cpdef int myfunction(int x, int y=2):
- a = x - y
- return a + x * y
-
-cdef double _helper(double a):
- return a + 1
-
-cdef class A:
- cdef public int a, b
- def __init__(self, b=0):
- self.a = 3
- self.b = b
-
- cpdef foo(self, double x):
- print(x + _helper(1.0))
+cpdef int myfunction(int x, int y=2):
+ a = x - y
+ return a + x * y
+
+cdef double _helper(double a):
+ return a + 1
+
+cdef class A:
+ cdef public int a, b
+ def __init__(self, b=0):
+ self.a = 3
+ self.b = b
+
+ cpdef foo(self, double x):
+ print(x + _helper(1.0))
diff --git a/docs/examples/tutorial/pure/annotations.py b/docs/examples/tutorial/pure/annotations.py
index 2b8487c0b..09682c352 100644
--- a/docs/examples/tutorial/pure/annotations.py
+++ b/docs/examples/tutorial/pure/annotations.py
@@ -1,5 +1,5 @@
-import cython
-
-def func(foo: dict, bar: cython.int) -> tuple:
- foo["hello world"] = 3 + bar
- return foo, 5
+import cython
+
+def func(foo: dict, bar: cython.int) -> tuple:
+ foo["hello world"] = 3 + bar
+ return foo, 5
diff --git a/docs/examples/tutorial/pure/c_arrays.py b/docs/examples/tutorial/pure/c_arrays.py
index 33067da68..f221b7ae8 100644
--- a/docs/examples/tutorial/pure/c_arrays.py
+++ b/docs/examples/tutorial/pure/c_arrays.py
@@ -1,15 +1,15 @@
-import cython
-
-
-@cython.locals(counts=cython.int[10], digit=cython.int)
-def count_digits(digits):
- """
- >>> digits = '01112222333334445667788899'
- >>> count_digits(map(int, digits))
- [1, 3, 4, 5, 3, 1, 2, 2, 3, 2]
- """
- counts = [0] * 10
- for digit in digits:
- assert 0 <= digit <= 9
- counts[digit] += 1
- return counts
+import cython
+
+
+@cython.locals(counts=cython.int[10], digit=cython.int)
+def count_digits(digits):
+ """
+ >>> digits = '01112222333334445667788899'
+ >>> count_digits(map(int, digits))
+ [1, 3, 4, 5, 3, 1, 2, 2, 3, 2]
+ """
+ counts = [0] * 10
+ for digit in digits:
+ assert 0 <= digit <= 9
+ counts[digit] += 1
+ return counts
diff --git a/docs/examples/tutorial/pure/cclass.py b/docs/examples/tutorial/pure/cclass.py
index 61c65183d..7f9cb1a04 100644
--- a/docs/examples/tutorial/pure/cclass.py
+++ b/docs/examples/tutorial/pure/cclass.py
@@ -1,16 +1,16 @@
-import cython
-
-
-@cython.cclass
-class A:
- cython.declare(a=cython.int, b=cython.int)
- c = cython.declare(cython.int, visibility='public')
- d = cython.declare(cython.int) # private by default.
- e = cython.declare(cython.int, visibility='readonly')
-
- def __init__(self, a, b, c, d=5, e=3):
- self.a = a
- self.b = b
- self.c = c
- self.d = d
- self.e = e
+import cython
+
+
+@cython.cclass
+class A:
+ cython.declare(a=cython.int, b=cython.int)
+ c = cython.declare(cython.int, visibility='public')
+ d = cython.declare(cython.int) # private by default.
+ e = cython.declare(cython.int, visibility='readonly')
+
+ def __init__(self, a, b, c, d=5, e=3):
+ self.a = a
+ self.b = b
+ self.c = c
+ self.d = d
+ self.e = e
diff --git a/docs/examples/tutorial/pure/compiled_switch.py b/docs/examples/tutorial/pure/compiled_switch.py
index 47d62a3e6..a35cac2c6 100644
--- a/docs/examples/tutorial/pure/compiled_switch.py
+++ b/docs/examples/tutorial/pure/compiled_switch.py
@@ -1,6 +1,6 @@
-import cython
-
-if cython.compiled:
- print("Yep, I'm compiled.")
-else:
- print("Just a lowly interpreted script.")
+import cython
+
+if cython.compiled:
+ print("Yep, I'm compiled.")
+else:
+ print("Just a lowly interpreted script.")
diff --git a/docs/examples/tutorial/pure/cython_declare.py b/docs/examples/tutorial/pure/cython_declare.py
index cf6d58bba..50a4ab8aa 100644
--- a/docs/examples/tutorial/pure/cython_declare.py
+++ b/docs/examples/tutorial/pure/cython_declare.py
@@ -1,4 +1,4 @@
-import cython
-
-x = cython.declare(cython.int) # cdef int x
-y = cython.declare(cython.double, 0.57721) # cdef double y = 0.57721
+import cython
+
+x = cython.declare(cython.int) # cdef int x
+y = cython.declare(cython.double, 0.57721) # cdef double y = 0.57721
diff --git a/docs/examples/tutorial/pure/cython_declare2.py b/docs/examples/tutorial/pure/cython_declare2.py
index 35fae7d7b..ee491d62b 100644
--- a/docs/examples/tutorial/pure/cython_declare2.py
+++ b/docs/examples/tutorial/pure/cython_declare2.py
@@ -1,3 +1,3 @@
-import cython
-
-cython.declare(x=cython.int, y=cython.double) # cdef int x; cdef double y
+import cython
+
+cython.declare(x=cython.int, y=cython.double) # cdef int x; cdef double y
diff --git a/docs/examples/tutorial/pure/disabled_annotations.py b/docs/examples/tutorial/pure/disabled_annotations.py
new file mode 100644
index 000000000..c92b4cf8e
--- /dev/null
+++ b/docs/examples/tutorial/pure/disabled_annotations.py
@@ -0,0 +1,33 @@
+import cython
+
+@cython.annotation_typing(False)
+def function_without_typing(a: int, b: int) -> int:
+ """Cython is ignoring annotations in this function"""
+ c: int = a + b
+ return c * a
+
+
+@cython.annotation_typing(False)
+@cython.cclass
+class NotAnnotatedClass:
+ """Cython is ignoring annotatons in this class except annotated_method"""
+ d: dict
+
+ def __init__(self, dictionary: dict):
+ self.d = dictionary
+
+ @cython.annotation_typing(True)
+ def annotated_method(self, key: str, a: cython.int, b: cython.int):
+ prefixed_key: str = 'prefix_' + key
+ self.d[prefixed_key] = a + b
+
+
+def annotated_function(a: cython.int, b: cython.int):
+ s: cython.int = a + b
+ with cython.annotation_typing(False):
+ # Cython is ignoring annotations within this code block
+ c: list = []
+ c.append(a)
+ c.append(b)
+ c.append(s)
+ return c
diff --git a/docs/examples/tutorial/pure/dostuff.py b/docs/examples/tutorial/pure/dostuff.py
index 748c31b10..7a88533c5 100644
--- a/docs/examples/tutorial/pure/dostuff.py
+++ b/docs/examples/tutorial/pure/dostuff.py
@@ -1,5 +1,5 @@
-def dostuff(n):
- t = 0
- for i in range(n):
- t += i
- return t
+def dostuff(n):
+ t = 0
+ for i in range(n):
+ t += i
+ return t
diff --git a/docs/examples/tutorial/pure/exceptval.py b/docs/examples/tutorial/pure/exceptval.py
index 8bf564040..3d991f7c1 100644
--- a/docs/examples/tutorial/pure/exceptval.py
+++ b/docs/examples/tutorial/pure/exceptval.py
@@ -1,7 +1,7 @@
-import cython
-
-@cython.exceptval(-1)
-def func(x: cython.int) -> cython.int:
- if x < 0:
- raise ValueError("need integer >= 0")
- return x + 1
+import cython
+
+@cython.exceptval(-1)
+def func(x: cython.int) -> cython.int:
+ if x < 0:
+ raise ValueError("need integer >= 0")
+ return x + 1
diff --git a/docs/examples/tutorial/pure/locals.py b/docs/examples/tutorial/pure/locals.py
index 8eda7114a..b273a9ebe 100644
--- a/docs/examples/tutorial/pure/locals.py
+++ b/docs/examples/tutorial/pure/locals.py
@@ -1,6 +1,6 @@
-import cython
-
-@cython.locals(a=cython.long, b=cython.long, n=cython.longlong)
-def foo(a, b, x, y):
- n = a * b
- # ...
+import cython
+
+@cython.locals(a=cython.long, b=cython.long, n=cython.longlong)
+def foo(a, b, x, y):
+ n = a * b
+ # ...
diff --git a/docs/examples/tutorial/pure/mymodule.py b/docs/examples/tutorial/pure/mymodule.py
index 62d4c76ac..83f5cdc28 100644
--- a/docs/examples/tutorial/pure/mymodule.py
+++ b/docs/examples/tutorial/pure/mymodule.py
@@ -1,10 +1,10 @@
-# mymodule.py
-
-import cython
-
-# override with Python import if not in compiled code
-if not cython.compiled:
- from math import sin
-
-# calls sin() from math.h when compiled with Cython and math.sin() in Python
-print(sin(0))
+# mymodule.py
+
+import cython
+
+# override with Python import if not in compiled code
+if not cython.compiled:
+ from math import sin
+
+# calls sin() from math.h when compiled with Cython and math.sin() in Python
+print(sin(0))
diff --git a/docs/examples/tutorial/pure/pep_526.py b/docs/examples/tutorial/pure/pep_526.py
index 163d97859..ecb3bfcdf 100644
--- a/docs/examples/tutorial/pure/pep_526.py
+++ b/docs/examples/tutorial/pure/pep_526.py
@@ -1,22 +1,22 @@
-import cython
-
-def func():
- # Cython types are evaluated as for cdef declarations
- x: cython.int # cdef int x
- y: cython.double = 0.57721 # cdef double y = 0.57721
- z: cython.float = 0.57721 # cdef float z = 0.57721
-
- # Python types shadow Cython types for compatibility reasons
- a: float = 0.54321 # cdef double a = 0.54321
- b: int = 5 # cdef object b = 5
- c: long = 6 # cdef object c = 6
- pass
-
-@cython.cclass
-class A:
- a: cython.int
- b: cython.int
-
- def __init__(self, b=0):
- self.a = 3
- self.b = b
+import cython
+
+def func():
+ # Cython types are evaluated as for cdef declarations
+ x: cython.int # cdef int x
+ y: cython.double = 0.57721 # cdef double y = 0.57721
+ z: cython.float = 0.57721 # cdef float z = 0.57721
+
+ # Python types shadow Cython types for compatibility reasons
+ a: float = 0.54321 # cdef double a = 0.54321
+ b: int = 5 # cdef object b = 5
+ c: long = 6 # cdef object c = 6
+ pass
+
+@cython.cclass
+class A:
+ a: cython.int
+ b: cython.int
+
+ def __init__(self, b=0):
+ self.a = 3
+ self.b = b
diff --git a/docs/examples/tutorial/pure/py_cimport.py b/docs/examples/tutorial/pure/py_cimport.py
new file mode 100644
index 000000000..233ddde7e
--- /dev/null
+++ b/docs/examples/tutorial/pure/py_cimport.py
@@ -0,0 +1,5 @@
+
+from cython.cimports.libc import math
+
+def use_libc_math():
+ return math.ceil(5.5)