Skip to content

Commit a6b88da

Browse files
authored
Merge pull request #15 from gjbex/development
Update for Cython 3.x
2 parents 85d5d3d + 9172fc9 commit a6b88da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+6689
-970
lines changed

python_for_hpc.pptx

-1.15 KB
Binary file not shown.

source-code/cython/Classes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
points.c

source-code/cython/Classes/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
VERSION = cpython-311-x86_64-linux-gnu
2+
POINTS_LIB = points.$(VERSION).so
3+
POINTS_PURE_LIB = points_pure.$(VERSION).so
4+
5+
all: $(POINTS_LIB)
6+
7+
$(POINTS_LIB): points.pyx points_pure.py
8+
python setup.py build_ext --inplace
9+
10+
clean:
11+
python setup.py clean
12+
$(RM) points.c points_pure.c $(POINTS_LIB) $(POINTS_PURE_LIB)

source-code/cython/Classes/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Classes
2+
3+
Illustration of using Cython extension types (aka cdef classes).
4+
5+
## What is it?
6+
7+
1. `points.pyx`: implementation of a cdef class, and a Python
8+
child class thereof.
9+
1. `points_pure.py`: implementation in Cython's pure Python
10+
syntax.
11+
1. `points_python.py`: Python implementation of the class.
12+
1. `driver.py`: Python script that uses both classes.
13+
1. `setup.py`: Python installation file to build the Cython
14+
extension.
15+
1. `distances_cython.py`: Python script to compute distances between
16+
points using Cython class.
17+
1. `distances_python.py`: Python script to compute distances between
18+
points using Python class.
19+
1. `distances_internal.py`: Python script to compute distances between
20+
points using Cython class, computing the distances using a static
21+
class method..
22+
1. `Makefile`: make file to build the extension.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import itertools
5+
import pyximport
6+
pyximport.install(pyimport=True, language_level='3str')
7+
from points_pure import Point
8+
import random
9+
10+
11+
def main():
12+
arg_parser = argparse.ArgumentParser(description='compute distances')
13+
arg_parser.add_argument('--n', type=int, default=10,
14+
help='number of points')
15+
options = arg_parser.parse_args()
16+
points = [Point(random.random(), random.random()) for _ in range(options.n)]
17+
min_distance = 2.0
18+
max_distance = 0.0
19+
for i, p1 in enumerate(points):
20+
for p2 in points[i+1:]:
21+
d = p1.distance(p2)
22+
min_distance = min(d, min_distance)
23+
max_distance = max(d, max_distance)
24+
print(f'min. distance: {min_distance}')
25+
print(f'max. distance: {max_distance}')
26+
27+
if __name__ == '__main__':
28+
main()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import itertools
5+
import pyximport
6+
pyximport.install(pyimport=True, language_level='3str')
7+
from points_pure import Point
8+
import random
9+
10+
11+
def main():
12+
arg_parser = argparse.ArgumentParser(description='compute distances')
13+
arg_parser.add_argument('--n', type=int, default=10,
14+
help='number of points')
15+
options = arg_parser.parse_args()
16+
points = [Point(random.random(), random.random()) for _ in range(options.n)]
17+
min_distance, max_distance = Point.min_max_distance(points)
18+
print(f'min. distance: {min_distance}')
19+
print(f'max. distance: {max_distance}')
20+
21+
if __name__ == '__main__':
22+
main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import itertools
5+
from points_python import Point
6+
import random
7+
8+
9+
def main():
10+
arg_parser = argparse.ArgumentParser(description='compute distances')
11+
arg_parser.add_argument('--n', type=int, default=10,
12+
help='number of points')
13+
options = arg_parser.parse_args()
14+
points = [Point(random.random(), random.random()) for _ in range(options.n)]
15+
min_distance = 2.0
16+
max_distance = 0.0
17+
for i, p1 in enumerate(points):
18+
for p2 in points[i+1:]:
19+
d = p1.distance(p2)
20+
min_distance = min(d, min_distance)
21+
max_distance = max(d, max_distance)
22+
print(f'min. distance: {min_distance}')
23+
print(f'max. distance: {max_distance}')
24+
25+
if __name__ == '__main__':
26+
main()

source-code/cython/Classes/driver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
3+
from points import Point, ColoredPoint
4+
5+
p = Point(1.0, -2.0)
6+
print(p)
7+
print(f'point = {p.x}, {p.y}')
8+
print(p.distance())
9+
10+
p1 = ColoredPoint(1.0, -2.0, 'blue')
11+
print(p1.color)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
3+
import pyximport
4+
pyximport.install(pyimport=True, language_level='3str')
5+
6+
from points_pure import Point, ColoredPoint
7+
8+
p = Point(1.0, -2.0)
9+
print(p)
10+
print(f'point = {p.x}, {p.y}')
11+
print(p.distance())
12+
13+
p1 = ColoredPoint(1.0, -2.0, 'blue')
14+
print(p1.color)

source-code/cython/Classes/points.pyx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from libc.math cimport sqrt
2+
3+
cdef class Point:
4+
5+
cdef double _x, _y
6+
7+
def __init__(self, x, y):
8+
self._x = x
9+
self._y = y
10+
11+
cpdef distance(self, other):
12+
return sqrt((self._x - other._x)**2 + (self._y - other._y)**2)
13+
14+
property x:
15+
def __get__(self):
16+
return self._x
17+
def __set__(self, value):
18+
self._x = float(value)
19+
20+
property y:
21+
def __get__(self):
22+
return self._y
23+
def __set__(self, value):
24+
self._y = float(value)
25+
26+
27+
class ColoredPoint(Point):
28+
29+
def __init__(self, x, y, color):
30+
super().__init__(x, y)
31+
self._color = color
32+
33+
@property
34+
def color(self):
35+
return self._color
36+
37+
@color.setter
38+
def color(self, value):
39+
self._color = str(value)

0 commit comments

Comments
 (0)