forked from shlomif/primesieve-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a149f77
commit ecd0f2a
Showing
17 changed files
with
998 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
test.py | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Generated by Cython | ||
primesieve/*.cpp | ||
primesieve/array/*.cpp | ||
primesieve/numpy/*.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "lib/primesieve"] | ||
path = lib/primesieve | ||
url = https://github.com/kimwalisch/primesieve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Our .travis.yml is a slightly modified version of: | ||
# https://github.com/joerick/cibuildwheel#example-setup | ||
|
||
language: python | ||
jobs: | ||
include: | ||
# perform a linux build | ||
- services: docker | ||
env: | ||
- BUILD_SOURCE_DISTRIBUTION="true" | ||
- services: docker | ||
# and a mac build | ||
- os: osx | ||
# PyPy 7.3.2 needs macOS >= 10.14 | ||
osx_image: xcode10.2 | ||
language: shell | ||
# and a windows build | ||
- os: windows | ||
language: shell | ||
before_install: | ||
- choco install python --version 3.8.0 | ||
- export PATH="/c/Python38:/c/Python38/Scripts:$PATH" | ||
# make sure it's on PATH as 'python3' | ||
- ln -s /c/Python38/python.exe /c/Python38/python3.exe | ||
|
||
env: | ||
global: | ||
# Skip building Python 2.7 and PyPy (jit) | ||
- CIBW_SKIP="cp27-* pp*" | ||
# TWINE_PASSWORD is set to a PyPI API token in Travis settings | ||
- TWINE_USERNAME=__token__ | ||
|
||
install: | ||
- python3 -m pip install --upgrade setuptools cython numpy pytest cibuildwheel==1.6.4 | ||
|
||
script: | ||
- | | ||
if [ "$BUILD_SOURCE_DISTRIBUTION" = "true" ] | ||
then | ||
python3 setup.py install | ||
pytest | ||
pushd .. | ||
python3 -c "import primesieve;print(primesieve.n_primes(10))" | ||
popd | ||
python3 setup.py sdist | ||
export UPLOAD_FILES=dist/*.tar.gz | ||
else | ||
# For the CIBW environment variables we need to use python instead of python3. | ||
# This way we use the CIBW python version. | ||
export CIBW_BEFORE_BUILD="python -m pip install --upgrade cython numpy" | ||
export CIBW_TEST_REQUIRES="pytest numpy" | ||
export CIBW_TEST_COMMAND='pytest {project} && python -c "import primesieve;print(primesieve.n_primes(10))"' | ||
python3 -m cibuildwheel --output-dir wheelhouse | ||
export UPLOAD_FILES=wheelhouse/*.whl | ||
fi | ||
# Unfortunately failures in after_success currently do not cause the | ||
# build to fail, see https://github.com/travis-ci/travis-ci/issues/758. | ||
# I think we could switch to "deploy:" to get the desired behavior. | ||
after_success: | ||
# Because of a bug in Travis we have to pin keyring here, | ||
# see: https://github.com/kimwalisch/primesieve-python/issues/44. | ||
# Try removing this workaround in 2022 (remove "keyring==21.4.0"). | ||
- python3 -m pip install twine keyring==21.4.0 | ||
|
||
# All commits are uploaded to https://test.pypi.org in order to | ||
# ensure that releases will be uploaded to PyPI without any issues. | ||
- python3 -m twine upload -p $TEST_TOKEN --repository-url https://test.pypi.org/legacy/ --skip-existing --verbose $UPLOAD_FILES; | ||
|
||
# If the release was tagged, upload to PyPI. | ||
- if [[ $TRAVIS_TAG ]]; then python3 -m twine upload -p $PYPI_TOKEN --skip-existing --verbose $UPLOAD_FILES; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 - 2017 Mirth Hickford | ||
Copyright (c) 2015 - 2019 Kim Walisch | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include README.md | ||
include LICENSE | ||
graft lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# primesieve-python | ||
|
||
[](https://travis-ci.org/kimwalisch/primesieve-python) [](https://pypi.python.org/pypi/primesieve) | ||
|
||
# Summary | ||
|
||
Python bindings for the [primesieve](https://github.com/kimwalisch/primesieve) | ||
C++ library. | ||
|
||
Generates primes orders of magnitude faster than any pure Python code! | ||
|
||
**Features:** | ||
|
||
* Get an array of primes | ||
* Iterate over primes using little memory | ||
* Find the nth prime | ||
* Count/print primes and [prime k-tuplets](https://en.wikipedia.org/wiki/Prime_k-tuple) | ||
* Multi-threaded for counting primes and finding the nth prime | ||
* NumPy support | ||
|
||
# Prerequisites | ||
|
||
We provide primesieve wheels (distribution packages) for Windows, macOS | ||
and Linux for x86 and x64 CPUs. For other operating systems and/or CPUs | ||
you need to have installed a C++ compiler. | ||
|
||
```bash | ||
# Ubuntu/Debian | ||
sudo apt install g++ python-dev | ||
|
||
# Fedora | ||
sudo dnf install gcc-c++ python-devel | ||
|
||
# macOS | ||
xcode-select --install | ||
``` | ||
|
||
# Installation | ||
|
||
```bash | ||
# Python 3.5 or later | ||
pip install primesieve | ||
|
||
# For Python 2.7 use: | ||
pip install "primesieve<=1.4.4" | ||
``` | ||
|
||
# Conda Installation | ||
|
||
[](https://travis-ci.org/conda-forge/python-primesieve-feedstock) | ||
[](https://ci.appveyor.com/project/conda-forge/python-primesieve-feedstock/branch/master) | ||
[](https://circleci.com/gh/conda-forge/python-primesieve-feedstock) | ||
[](https://anaconda.org/conda-forge/python-primesieve) | ||
|
||
You don't need to install a C++ compiler when installing python-primesieve using Conda. | ||
|
||
``` | ||
conda install -c conda-forge python-primesieve | ||
``` | ||
|
||
# Usage examples | ||
|
||
```Python | ||
>>> from primesieve import * | ||
|
||
# Get an array of the primes <= 40 | ||
>>> primes(40) | ||
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] | ||
|
||
# Get an array of the primes between 100 and 120 | ||
>>> primes(100, 120) | ||
[101, 103, 107, 109, 113] | ||
|
||
# Get an array of the first 10 primes | ||
>>> n_primes(10) | ||
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29] | ||
|
||
# Get an array of the first 10 primes >= 1000 | ||
>>> n_primes(10, 1000) | ||
[1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061] | ||
|
||
# Get the 10th prime | ||
>>> nth_prime(10) | ||
29 | ||
|
||
# Count the primes below 10**9 | ||
>>> count_primes(10**9) | ||
50847534 | ||
``` | ||
|
||
Here is a [list of all available functions](primesieve/_primesieve.pyx). | ||
|
||
# Iterating over primes | ||
|
||
Instead of generating a large array of primes and then do something | ||
with the primes it is also possible to simply iterate over the primes | ||
which uses less memory. | ||
|
||
```Python | ||
""">>> import primesieve | ||
it = primesieve.Iterator() | ||
prime = it.next_prime() | ||
# Iterate over the primes below 10000 | ||
while prime < 10000: | ||
print prime | ||
prime = it.next_prime() | ||
# Set iterator start number to 100 | ||
it.skipto(100) | ||
prime = it.prev_prime() | ||
# Iterate backwards over the primes below 100 | ||
while prime > 0: | ||
print prime | ||
prime = it.prev_prime()""" | ||
|
||
from primesieve import primes_range | ||
|
||
# iterate over the primes in range between 100 and 10^7 | ||
# 100 < prime <= 10^7 | ||
for prime in primes_range(100,10e7): | ||
|
||
# do something with the prime | ||
... | ||
|
||
``` | ||
|
||
# NumPy support | ||
|
||
Using the ```primesieve.numpy``` module you can generate an array of | ||
primes using native C++ performance! | ||
|
||
In comparison the ```primesieve``` module generates an array of primes | ||
about 3 times slower mostly because the conversion of the C primes | ||
array into a python array is quite slow. | ||
|
||
```Python | ||
>>> from primesieve.numpy import * | ||
|
||
# Generate a numpy array with the primes below 100 | ||
>>> primes(100) | ||
array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, | ||
61, 67, 71, 73, 79, 83, 89, 97]) | ||
|
||
# Generate a numpy array with the first 100 primes | ||
>>> n_primes(100) | ||
array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, | ||
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, | ||
103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, | ||
173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, | ||
241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, | ||
317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, | ||
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, | ||
479, 487, 491, 499, 503, 509, 521, 523, 541]) | ||
``` | ||
|
||
# Development | ||
|
||
You need to have installed a C++ compiler, see [Prerequisites](#prerequisites). | ||
|
||
```bash | ||
# Install prerequisites | ||
pip install cython pytest numpy | ||
|
||
# Clone repository | ||
git clone --recursive https://github.com/kimwalisch/primesieve-python | ||
|
||
cd primesieve-python | ||
|
||
# Build and install primesieve-python | ||
pip install . --upgrade | ||
|
||
# Run tests | ||
pytest | ||
``` | ||
|
||
# How to do a new release | ||
|
||
* You need to be a maintainer of the [primesieve-python](https://github.com/kimwalisch/primesieve-python) repo. | ||
* You need to be a maintainer of the [primesieve pypi](https://pypi.org/project/primesieve) project. | ||
* Compare ```.travis.yml``` with [cibuildwheel#example-setup](https://github.com/joerick/cibuildwheel#example-setup) and update ```.travis.yml``` if needed. | ||
* Update the supported Python versions in ```setup.py``` (we support the same versions as [cibuildwheel](https://pypi.org/project/cibuildwheel)). | ||
* Increment the primesieve-python version in ```setup.py```. Ideally this should be the last commit before the release as this uploads the new primesieve wheels to [https://test.pypi.org](https://test.pypi.org/project/primesieve/#files). | ||
* Check if all primesieve wheels (Windows, macOS, Linux) have been uploaded to [https://test.pypi.org](https://test.pypi.org/project/primesieve/#files). | ||
* If not, read the [Travis CI logs](https://travis-ci.org/github/kimwalisch/primesieve-python) and fix the bugs. | ||
* Finally, do a new release on GitHub. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from primesieve._primesieve import * |
Oops, something went wrong.