Skip to content

Commit 290b167

Browse files
committed
small fixes
1 parent ad64032 commit 290b167

12 files changed

Lines changed: 57 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [v0.1.3]
2+
- fix terminology (subpackage, not submodule)
3+
- provide pruned-down version [setup-purepython.py](setup-purepython.py) for pure Python projects. This is mainly for comparison, as many templates already exist for this.
4+
15
## [v0.1.2]
26
- fix license also in [README.md](README.md)
37

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Setuptools[[1]][setuptools] has become the tool of choice[[2]][packaging] for pa
88

99
For packages to be distributed (especially through [PyPI](https://pypi.python.org/pypi)), setuptools is preferable, since the documentation on distributing packages[[3]][distributing] assumes that is what the developer uses. Also, [setuptools adds dependency resolution](http://stackoverflow.com/questions/25337706/setuptools-vs-distutils-why-is-distutils-still-a-thing) (over distutils), which is an essential feature of `pip`.
1010

11-
This very minimal project documents the author's best guess at current best practices for the packaging and distribution of Cython projects, by piecing together information from various sources (mainly documentation and StackOverflow). Possible corrections, if any, are welcome.
11+
This very minimal project documents the author's best guess at current best practices for the packaging and distribution of Cython projects, by piecing together information from various sources (mainly documentation and StackOverflow). A pruned-down version for pure Python projects is also provided for comparison. Possible corrections, if any, are welcome.
1212

13-
This is intended as a template for new Cython projects. For completeness, a minimal Cython module is included. This project is placed in the public domain so as to allow its use anywhere.
13+
This is intended as a template [`setup.py`](setup.py) for new Cython projects. For completeness, a minimal Cython-based example library is included, containing examples of things such as absolute cimports, subpackages, [NumPyDoc](https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt) style docstrings, and using [memoryviews](http://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html) for passing arrays (for the last two, see [compute.pyx](mylibrary/compute.pyx)). The example in the [test/](test/) subdirectory demonstrates its usage.
14+
15+
This project is placed under The Unlicense so as to allow its use anywhere. Code snippets based on StackOverflow answers are credited by providing the original links.
1416

1517
This is similar to [simple-cython-example](https://github.com/thearn/simple-cython-example), but different. Here the focus is on numerical scientific projects, where a custom Cython extension (containing all-new code) can bring a large speedup.
1618

@@ -57,7 +59,7 @@ For `install`, the switch `--user` may be useful. As can, alternatively, running
5759

5860
#### Binary distributions
5961

60-
As noted in the [Python packaging guide](https://packaging.python.org/distributing/#platform-wheels), PyPI accepts platform wheels (platform-specific binary distributions) for Linux only if they conform to [the `manylinux1` ABI](https://www.python.org/dev/peps/pep-0513/), so running `python setup.py bdist_egg` on an arbitrary development machine is generally not very useful for the purposes of distribution.
62+
As noted in the [Python packaging guide](https://packaging.python.org/distributing/#platform-wheels), PyPI accepts platform wheels (platform-specific binary distributions) for Linux only if they conform to [the `manylinux1` ABI](https://www.python.org/dev/peps/pep-0513/), so running `python setup.py bdist_wheel` on an arbitrary development machine is generally not very useful for the purposes of distribution.
6163

6264
For the adventurous, PyPA provides [instructions](https://github.com/pypa/manylinux) along with a Docker image.
6365

mylibrary/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import absolute_import
66

77
# This is extracted automatically by the top-level setup.py.
8-
__version__ = '0.1.2'
8+
__version__ = '0.1.3'
99

10-
# add any imports here, if you wish to bring things into the library's top-level namespace.
10+
# add any imports here, if you wish to bring things into the library's top-level namespace when the library is imported.
1111

mylibrary/compute.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# cython: wraparound = False
44
# cython: boundscheck = False
55
# cython: cdivision = True
6-
"""Example Cython module."""
6+
"""Example Cython module for numerical computation mixing libc.math and NumPy."""
77

88
from __future__ import division, print_function, absolute_import
99

@@ -42,7 +42,7 @@ Return value:
4242
cdef int n = x.shape[0]
4343

4444
# np.empty() is a pretty good mechanism for dynamic allocation of arrays,
45-
# as long as memory allocation can be done on the Python side,
45+
# as long as memory allocation can be done in Python parts of the code (no "nogil").
4646
#
4747
# If you absolutely need to dynamically allocate memory in nogil code,
4848
# then "from libc.stdlib cimport malloc, free", and be ready for pain.
@@ -57,7 +57,7 @@ Return value:
5757
# can proceed while this one is computing.
5858
#
5959
# We could also "cimport cython.parallel" and "for j in cython.parallel.prange(n):"
60-
# if we wanted to link this with OpenMP.
60+
# if we wanted (and then link this module with OpenMP in setup.py).
6161
#
6262
cdef int j
6363
with nogil:

mylibrary/dostuff.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
# cython: wraparound = False
44
# cython: boundscheck = False
55
# cython: cdivision = True
6-
"""Example Cython module."""
6+
"""Example Cython module that calls a function from a subpackage of mylibrary."""
77

88
from __future__ import division, print_function, absolute_import
99

1010

1111
# Use absolute module names, even from this library itself.
1212
#
13-
cimport mylibrary.submodule.helloworld as helloworld
13+
cimport mylibrary.subpackage.helloworld as helloworld
1414

1515

1616
def hello(s):
17-
"""Python interface to mylibrary.submodule.helloworld.
17+
"""Python interface to mylibrary.subpackage.helloworld.
1818
1919
This is mainly an example of absolute imports in Cython modules.
2020

mylibrary/submodule/helloworld.pxd

Lines changed: 0 additions & 8 deletions
This file was deleted.

mylibrary/submodule/helloworld.pyx

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Cython-level declarations for mylibrary.subpackage.helloworld, available for cimport from other Cython modules.
4+
5+
from __future__ import absolute_import
6+
7+
cdef void hello(str s)
8+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# cython: wraparound = False
4+
# cython: boundscheck = False
5+
# cython: cdivision = True
6+
"""Example Cython module.""" # this is the Python-level docstring
7+
8+
# Note that this is a pure Cython-level module; it has no "def" functions or Python-accessible objects.
9+
#
10+
# If this is imported to Python, the user will just see the module docstring.
11+
12+
from __future__ import division, print_function, absolute_import
13+
14+
# Echo the string s.
15+
#
16+
cdef void hello(str s):
17+
print(s) # this is really, really silly (providing a Cython-level cdef function that just calls Python print())
18+

0 commit comments

Comments
 (0)