Skip to content

Commit 9884a21

Browse files
committed
Add pure Python version
1 parent a0a7b8a commit 9884a21

File tree

7 files changed

+69
-7
lines changed

7 files changed

+69
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
point_cython.c
2+
point_pure.c
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
VERSION = cpython-34m
2-
POINT_LIB = point.$(VERSION).so
1+
VERSION = cpython-311-x86_64-linux-gnu
2+
CYTHON_LIB = point_cython.$(VERSION).so
3+
PURE_LIB = point_pure.$(VERSION).so
34

4-
all: $(POINT_LIB)
5+
all: $(CYTHON_LIB) $(PURE_LIB)
56

6-
$(POINT_LIB): point.pyx
7+
$(CYTHON_LIB): point_cython.pyx
8+
python setup.py build_ext --inplace
9+
10+
$(PURE_LIB): point_pure.py
711
python setup.py build_ext --inplace
812

913
clean:
1014
python setup.py clean
11-
rm -f point.c $(POINT_LIB)
15+
$(RM) point_cython.c point_pure.c $(CYTHON_LIB) $(PURE_LIB)
16+
$(RM) -r build
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Structues
2+
3+
It is easy to define structures in Cython and pure Python
4+
syntax.
5+
6+
7+
## What is it?
8+
9+
1. `point_cython.pyx`: Cython file that defines structure and
10+
some functions that use this structure.
11+
1. `point_pure.py`: pure Python file that defines structure and
12+
some functions that use this structure.
13+
1. `setup.py`: Python setup file to build the Cython extensions.
14+
1. `Makefile`: make file to build the Cython extensions.
15+
1. `driver.py`: Python script to illustrate the use of the
16+
structure type, either the Cython or the pure Python version
17+
can be selected.

source-code/cython/Structures/driver.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#!/usr/bin/env python
22

3-
import point
3+
import argparse
4+
5+
arg_parser = argparse.ArgumentParser(description='Driver for point modules')
6+
arg_parser.add_argument('module', choices=['cython', 'pure_python'],
7+
help='module to use')
8+
options = arg_parser.parse_args()
9+
10+
if options.module == 'cython':
11+
import point_cython as point
12+
elif options.module == 'pure_python':
13+
import point_pure as point
414

515
p = point.create(1.0, -2.0, 1)
616
print(p)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import cython
2+
import math
3+
4+
Point = cython.struct(
5+
x=cython.double,
6+
y=cython.double,
7+
id=cython.int
8+
)
9+
10+
def create(x: cython.double, y: cython.double, id: cython.int) -> Point:
11+
point: Point
12+
point.x = x
13+
point.y = y
14+
point.id = id
15+
return point
16+
17+
def radius(point: Point) -> cython.double:
18+
return math.sqrt(point.x**2 + point.y**2)
19+
20+
def azimuth(point: Point) -> cython.double:
21+
return math.atan2(point.y, point.x)
22+
23+
def project(point: Point) -> None:
24+
r: cython.double = radius(point)
25+
point.x /= r
26+
point.y /= r
27+
print(point)

source-code/cython/Structures/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
from Cython.Build import cythonize
55

66
setup(
7-
ext_modules=cythonize("point.pyx")
7+
ext_modules=cythonize(['point_cython.pyx', 'point_pure.py'],
8+
language_level='3str')
89
)

0 commit comments

Comments
 (0)