diff options
Diffstat (limited to 'docs/examples/tutorial/array')
-rw-r--r-- | docs/examples/tutorial/array/clone.py | 8 | ||||
-rw-r--r-- | docs/examples/tutorial/array/clone.pyx | 16 | ||||
-rw-r--r-- | docs/examples/tutorial/array/overhead.py | 17 | ||||
-rw-r--r-- | docs/examples/tutorial/array/overhead.pyx | 32 | ||||
-rw-r--r-- | docs/examples/tutorial/array/resize.py | 10 | ||||
-rw-r--r-- | docs/examples/tutorial/array/resize.pyx | 20 | ||||
-rw-r--r-- | docs/examples/tutorial/array/safe_usage.py | 6 | ||||
-rw-r--r-- | docs/examples/tutorial/array/safe_usage.pyx | 12 | ||||
-rw-r--r-- | docs/examples/tutorial/array/unsafe_usage.py | 11 | ||||
-rw-r--r-- | docs/examples/tutorial/array/unsafe_usage.pyx | 22 |
10 files changed, 104 insertions, 50 deletions
diff --git a/docs/examples/tutorial/array/clone.py b/docs/examples/tutorial/array/clone.py new file mode 100644 index 000000000..6736c2f67 --- /dev/null +++ b/docs/examples/tutorial/array/clone.py @@ -0,0 +1,8 @@ +from cython.cimports.cpython import array +import array + +int_array_template = cython.declare(array.array, array.array('i', [])) +cython.declare(newarray=array.array) + +# create an array with 3 elements with same type as template +newarray = array.clone(int_array_template, 3, zero=False) diff --git a/docs/examples/tutorial/array/clone.pyx b/docs/examples/tutorial/array/clone.pyx index e2bac0e4a..2eb803499 100644 --- a/docs/examples/tutorial/array/clone.pyx +++ b/docs/examples/tutorial/array/clone.pyx @@ -1,8 +1,8 @@ -from cpython cimport array
-import array
-
-cdef array.array int_array_template = array.array('i', [])
-cdef array.array newarray
-
-# create an array with 3 elements with same type as template
-newarray = array.clone(int_array_template, 3, zero=False)
+from cpython cimport array +import array + +cdef array.array int_array_template = array.array('i', []) +cdef array.array newarray + +# create an array with 3 elements with same type as template +newarray = array.clone(int_array_template, 3, zero=False) diff --git a/docs/examples/tutorial/array/overhead.py b/docs/examples/tutorial/array/overhead.py new file mode 100644 index 000000000..f60c019ce --- /dev/null +++ b/docs/examples/tutorial/array/overhead.py @@ -0,0 +1,17 @@ +from cython.cimports.cpython import array +import array + +a = cython.declare(array.array, array.array('i', [1, 2, 3])) +ca = cython.declare(cython.int[:], a) + +@cython.cfunc +def overhead(a: cython.object) -> cython.int: + ca: cython.int[:] = a + return ca[0] + +@cython.cfunc +def no_overhead(ca: cython.int[:]) -> cython.int: + return ca[0] + +print(overhead(a)) # new memory view will be constructed, overhead +print(no_overhead(ca)) # ca is already a memory view, so no overhead diff --git a/docs/examples/tutorial/array/overhead.pyx b/docs/examples/tutorial/array/overhead.pyx index e385bff3f..a113e8dc9 100644 --- a/docs/examples/tutorial/array/overhead.pyx +++ b/docs/examples/tutorial/array/overhead.pyx @@ -1,15 +1,17 @@ -from cpython cimport array
-import array
-
-cdef array.array a = array.array('i', [1, 2, 3])
-cdef int[:] ca = a
-
-cdef int overhead(object a):
- cdef int[:] ca = a
- return ca[0]
-
-cdef int no_overhead(int[:] ca):
- return ca[0]
-
-print(overhead(a)) # new memory view will be constructed, overhead
-print(no_overhead(ca)) # ca is already a memory view, so no overhead
+from cpython cimport array +import array + +cdef array.array a = array.array('i', [1, 2, 3]) +cdef int[:] ca = a + + +cdef int overhead(object a): + cdef int[:] ca = a + return ca[0] + + +cdef int no_overhead(int[:] ca): + return ca[0] + +print(overhead(a)) # new memory view will be constructed, overhead +print(no_overhead(ca)) # ca is already a memory view, so no overhead diff --git a/docs/examples/tutorial/array/resize.py b/docs/examples/tutorial/array/resize.py new file mode 100644 index 000000000..c2e50472f --- /dev/null +++ b/docs/examples/tutorial/array/resize.py @@ -0,0 +1,10 @@ +from cython.cimports.cpython import array +import array + +a = cython.declare(array.array, array.array('i', [1, 2, 3])) +b = cython.declare(array.array, array.array('i', [4, 5, 6])) + +# extend a with b, resize as needed +array.extend(a, b) +# resize a, leaving just original three elements +array.resize(a, len(a) - len(b)) diff --git a/docs/examples/tutorial/array/resize.pyx b/docs/examples/tutorial/array/resize.pyx index a11fbde7b..7b92958b4 100644 --- a/docs/examples/tutorial/array/resize.pyx +++ b/docs/examples/tutorial/array/resize.pyx @@ -1,10 +1,10 @@ -from cpython cimport array
-import array
-
-cdef array.array a = array.array('i', [1, 2, 3])
-cdef array.array b = array.array('i', [4, 5, 6])
-
-# extend a with b, resize as needed
-array.extend(a, b)
-# resize a, leaving just original three elements
-array.resize(a, len(a) - len(b))
+from cpython cimport array +import array + +cdef array.array a = array.array('i', [1, 2, 3]) +cdef array.array b = array.array('i', [4, 5, 6]) + +# extend a with b, resize as needed +array.extend(a, b) +# resize a, leaving just original three elements +array.resize(a, len(a) - len(b)) diff --git a/docs/examples/tutorial/array/safe_usage.py b/docs/examples/tutorial/array/safe_usage.py new file mode 100644 index 000000000..8b9ffd42c --- /dev/null +++ b/docs/examples/tutorial/array/safe_usage.py @@ -0,0 +1,6 @@ +from cython.cimports.cpython import array +import array +a = cython.declare(array.array, array.array('i', [1, 2, 3])) +ca = cython.declare(cython.int[:], a) + +print(ca[0]) diff --git a/docs/examples/tutorial/array/safe_usage.pyx b/docs/examples/tutorial/array/safe_usage.pyx index 61d6b39eb..15107ae92 100644 --- a/docs/examples/tutorial/array/safe_usage.pyx +++ b/docs/examples/tutorial/array/safe_usage.pyx @@ -1,6 +1,6 @@ -from cpython cimport array
-import array
-cdef array.array a = array.array('i', [1, 2, 3])
-cdef int[:] ca = a
-
-print(ca[0])
+from cpython cimport array +import array +cdef array.array a = array.array('i', [1, 2, 3]) +cdef int[:] ca = a + +print(ca[0]) diff --git a/docs/examples/tutorial/array/unsafe_usage.py b/docs/examples/tutorial/array/unsafe_usage.py new file mode 100644 index 000000000..99b2b1690 --- /dev/null +++ b/docs/examples/tutorial/array/unsafe_usage.py @@ -0,0 +1,11 @@ +from cython.cimports.cpython import array +import array + +a = cython.declare(array.array, array.array('i', [1, 2, 3])) + +# access underlying pointer: +print(a.data.as_ints[0]) + +from cython.cimports.libc.string import memset + +memset(a.data.as_voidptr, 0, len(a) * cython.sizeof(cython.int)) diff --git a/docs/examples/tutorial/array/unsafe_usage.pyx b/docs/examples/tutorial/array/unsafe_usage.pyx index 2aefeb102..d1f498c68 100644 --- a/docs/examples/tutorial/array/unsafe_usage.pyx +++ b/docs/examples/tutorial/array/unsafe_usage.pyx @@ -1,11 +1,11 @@ -from cpython cimport array
-import array
-
-cdef array.array a = array.array('i', [1, 2, 3])
-
-# access underlying pointer:
-print(a.data.as_ints[0])
-
-from libc.string cimport memset
-
-memset(a.data.as_voidptr, 0, len(a) * sizeof(int))
+from cpython cimport array +import array + +cdef array.array a = array.array('i', [1, 2, 3]) + +# access underlying pointer: +print(a.data.as_ints[0]) + +from libc.string cimport memset + +memset(a.data.as_voidptr, 0, len(a) * sizeof(int)) |