forked from EveryTimeIWill18/Cython_Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCython_Tutorial_One.pyx
95 lines (77 loc) · 2.33 KB
/
Cython_Tutorial_One.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from pprint import pprint
# creating cython extension types
cdef class Noddy:
pass
noddy = Noddy()
dir(noddy)
# cython class extension with one method
cdef class MyType:
def my_method(self, name):
"""prints name"""
print("Hello, ", name)
t = MyType()
t.my_method('William')
class PyBook:
def __init__(self, title):
self.title = title
cdef class CyBook:
# fields only accessable in cython code
# must provide an access level
# python can read but cannot change the value
# readonly must be defined so native
# python can print out the value
cdef readonly str title # must define title
def __init__(self, title):
self.title = title
# CyBook will compile but attribute error occurs
# extension types are c structs. must be declaired at
# compile time
py_book = PyBook("Learning Python")
book = CyBook("Learning Cython")
print(book.title)
# cython extension types
# must define data fields at the class level
cdef class Swallow:
pass
cdef class Parrot:
cdef Swallow friend
cdef public bint alive
cdef readonly str status
def __init__(self, Swallow friend):
self.alive = False
self.status = "busy"
self.friend = friend
class pyEvent:
def from_dict(self, d):
"""self loading from an event dict"""
self.host = d['host']
self.year = int(d['year'])
self.attendance = int(d['average attendance'])
def find_max_py1(events: list):
return max(events, key=lambda e: e.attendance)
def find_max_py2(events: list):
for e in events:
if e.attendance > largest.attendance:
largest = e
return largest
cdef class cyEvent:
cdef public:
str host
int attendance, matches, teams, goals, year
def from_dict(self, d):
self.host = d['host']
self.year = int(d['year'])
self.attendance = int(d['average attendance'])
cdef int keyfunc(cyEvent e):
return e.attendance
def find_max_cy1(list events):
# python max function slows things down
# *NOTE:*
# manual loop in cython will be the fastest option
return max(events, key=keyfunc)
def find_max_cy2(list events):
cdef cyEvent e, largest = events[0]
for e in events:
if e.attendance > largest.attendance:
largest = e
return largest