Skip to content

Commit 9172fc9

Browse files
committed
Add illustration of Cython classes
1 parent 91d6982 commit 9172fc9

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

source-code/cython/Classes/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ Illustration of using Cython extension types (aka cdef classes).
1212
1. `driver.py`: Python script that uses both classes.
1313
1. `setup.py`: Python installation file to build the Cython
1414
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..
1522
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()

0 commit comments

Comments
 (0)