mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
cython: hello world and primes examples
This commit is contained in:
@@ -698,6 +698,7 @@ path_properties_tuples = (
|
|||||||
'libs': (
|
'libs': (
|
||||||
{'requires_dynamic_library': True},
|
{'requires_dynamic_library': True},
|
||||||
{
|
{
|
||||||
|
'cython': {'no_build': True},
|
||||||
'libdrm': {'requires_sudo': True},
|
'libdrm': {'requires_sudo': True},
|
||||||
'hdf5': (
|
'hdf5': (
|
||||||
{},
|
{},
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
|
Cython==0.29.15
|
||||||
pexpect==4.6.0
|
pexpect==4.6.0
|
||||||
|
|||||||
1
userland/libs/cython/helloworld.pyx
Normal file
1
userland/libs/cython/helloworld.pyx
Normal file
@@ -0,0 +1 @@
|
|||||||
|
print("Hello World")
|
||||||
6
userland/libs/cython/main.py
Executable file
6
userland/libs/cython/main.py
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import helloworld
|
||||||
|
import primes
|
||||||
|
|
||||||
|
print(primes.primes(4))
|
||||||
23
userland/libs/cython/primes.pyx
Normal file
23
userland/libs/cython/primes.pyx
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
def primes(int nb_primes):
|
||||||
|
cdef int n, i, len_p
|
||||||
|
cdef int p[1000]
|
||||||
|
if nb_primes > 1000:
|
||||||
|
nb_primes = 1000
|
||||||
|
|
||||||
|
len_p = 0 # The current number of elements in p.
|
||||||
|
n = 2
|
||||||
|
while len_p < nb_primes:
|
||||||
|
# Is n prime?
|
||||||
|
for i in p[:len_p]:
|
||||||
|
if n % i == 0:
|
||||||
|
break
|
||||||
|
|
||||||
|
# If no break occurred in the loop, we have a prime.
|
||||||
|
else:
|
||||||
|
p[len_p] = n
|
||||||
|
len_p += 1
|
||||||
|
n += 1
|
||||||
|
|
||||||
|
# Let's return the result in a python list:
|
||||||
|
result_as_list = [prime for prime in p[:len_p]]
|
||||||
|
return result_as_list
|
||||||
6
userland/libs/cython/setup.py
Normal file
6
userland/libs/cython/setup.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from distutils.core import setup
|
||||||
|
from Cython.Build import cythonize
|
||||||
|
|
||||||
|
setup(
|
||||||
|
ext_modules = cythonize(['*.pyx']),
|
||||||
|
)
|
||||||
4
userland/libs/cython/test
Executable file
4
userland/libs/cython/test
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -eux
|
||||||
|
python3 setup.py build_ext --inplace
|
||||||
|
./main.py
|
||||||
Reference in New Issue
Block a user