Skip to content

Commit 57b69ed

Browse files
committed
Add sample code
1 parent 1411d15 commit 57b69ed

File tree

230 files changed

+44964
-0
lines changed

Some content is hidden

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

230 files changed

+44964
-0
lines changed

source-code/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Source code
2+
3+
This is source code that is either used in the presentation, or was developed
4+
to create it. There is some material not covered in the presentation as well.
5+
6+
## Requirements
7+
8+
* Python version: at least 3.6
9+
* Packages (names listed taht can be used with `pip` or `conda` to install):
10+
* cython
11+
* dask
12+
* numpy
13+
* numexpr
14+
* scipy
15+
* matplotlib
16+
* mpi4py
17+
* pytables
18+
* jupyter
19+
* ipywidgets
20+
21+
22+
## What is it?
23+
24+
1. `cython`: illustrations of how to use Cython to speed up Python.
25+
1. `dask`: examples of how to use dask for distributed computing.
26+
1. `interfaciing-c-c++-fortran`: illustrations of ctypes, SWIG and
27+
f2py to interface with C, C++ and Fortran code.
28+
1. `ising`: example of speeding up a Python simulation by wrapping
29+
C++ code using SWIG.
30+
1. `mpi4py`: illustrations of distributed programming using MPI.
31+
1. `multiprocessing`: illustrations of multithreaded programming
32+
using multiprocessing.
33+
1. `numba`: illustration of using the numba library.
34+
1. `profiling`: some illustrations and how-to on profiling a Python
35+
application.
36+
1. `pyspark`: illustrations of using PySpark.

source-code/cython/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
*.so
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
VERSION = cpython-34m
2+
AVERAGE_LIB = average.$(VERSION).so
3+
4+
all: $(AVERAGE_LIB)
5+
6+
$(AVERAGE_LIB): average.pyx
7+
python setup.py build_ext --inplace
8+
9+
clean:
10+
python setup.py clean
11+
rm -f average.c $(AVERAGE_LIB)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Exceptions
2+
Error handling in Cython code.
3+
4+
## What is it?
5+
1. `average.pyx`: code to be compiled using Cython, implements two
6+
functions to compute the average of an array slice, one with, the
7+
other without error handling.
8+
1. `setup.py`: Python build script.
9+
1. `Makefile`: make file to build the extension.
10+
1. `compute_average.py`: script to load the compiled module and call the
11+
implemented function with and without error handling.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def average(data, m=0, n=None):
2+
if n is None:
3+
n = len(data)
4+
return _average(memoryview(data), m, n)
5+
6+
def average_no_except(data, m=0, n=None):
7+
if n is None:
8+
n = len(data)
9+
return _average_no_except(memoryview(data), m, n)
10+
11+
cdef double _average(data, int m=0, int n=-1) except? -1.0:
12+
cdef int i
13+
cdef double mean = 0.0
14+
for i in range(m, n):
15+
mean += data[i]
16+
return mean/(n - m + 1)
17+
18+
cdef double _average_no_except(data, int m=0, int n=-1):
19+
cdef int i
20+
cdef double mean = 0.0
21+
for i in range(m, n):
22+
mean += data[i]
23+
return mean/(n - m + 1)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
from argparse import ArgumentParser
4+
import array
5+
import average
6+
7+
8+
size = 10
9+
10+
arg_parser = ArgumentParser(description='test Cython errors')
11+
arg_parser.add_argument('--m', type=int, default=0, help='lower bound')
12+
arg_parser.add_argument('--n', type=int, default=size, help='upper bound')
13+
options = arg_parser.parse_args()
14+
data = array.array('d', list(range(size)))
15+
print('with except:')
16+
try:
17+
print(average.average(data, options.m, options.n))
18+
except Exception as e:
19+
print('caught exception {0}: {1}'.format(str(e.__class__), str(e)))
20+
print('without except:')
21+
try:
22+
print(average.average_no_except(data, options.m, options.n))
23+
print('no exception caught')
24+
except Exception as e:
25+
print('caught exception {0}: {1}'.format(e.__class__, str(e)))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
3+
from distutils.core import setup
4+
from Cython.Build import cythonize
5+
6+
setup(
7+
ext_modules=cythonize('average.pyx')
8+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
VERSION = cpython-34m
2+
HELLO_WORLD_LIB = hello_world.$(VERSION).so
3+
4+
all: $(HELLO_WORLD_LIB)
5+
6+
$(HELLO_WORLD_LIB): hello_world.pyx
7+
python setup.py build_ext --inplace
8+
9+
clean:
10+
python setup.py clean
11+
rm -f hello_world.c $(HELLO_WORLD_LIB)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# HelloWorld
2+
Most trivial example of Cython code.
3+
4+
## What is it?
5+
1. `hello_world.pyx`: code to be compiled using Cython.
6+
1. `setup.py`: Python build script.
7+
1. `Makefile`: make file to build the extension.
8+
1. `say_hello.py`: script to load the compiled module. Note that the code
9+
is executed upon import since no guard is in place.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('hello world!')

0 commit comments

Comments
 (0)