Skip to content

Commit

Permalink
support pip 10 + make cysignals optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vrasneur committed May 6, 2018
1 parent 413ee1a commit 7dd28b6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# pyfasttext
Yet another Python binding for [fastText](https://github.com/facebookresearch/fastText).

The binding supports Python 2.6, 2.7 and Python 3. It requires [Cython](http://cython.org/) and [cysignals](http://cysignals.readthedocs.io/en/latest/).
[Numpy](http://www.numpy.org/) is also a dependency, but is optional.
The binding supports Python 2.6, 2.7 and Python 3. It requires [Cython](http://cython.org/).

[Numpy](http://www.numpy.org/) and [cysignals](http://cysignals.readthedocs.io/en/latest/) are also dependencies, but are optional.

`pyfasttext` has been tested successfully on Linux and Mac OS X.
*Warning*: `pyfasttext` does not currently compile on Windows because the `cysignals` module does not support this platform.
*Warning*: if you want to compile `pyfasttext` on Windows, do not compile with the `cysignals` module because it does not support this platform.

Table of Contents
=================
Expand All @@ -18,7 +19,7 @@ Table of Contents
* [Cloning](#cloning)
* [Requirements for Python 2.7](#requirements-for-python-27)
* [Building and installing manually](#building-and-installing-manually)
* [Building and installing without Numpy](#building-and-installing-without-numpy)
* [Building and installing without optional dependencies](#building-and-installing-without-optional-dependencies)
* [Usage](#usage)
* [How to load the library?](#how-to-load-the-library)
* [How to load an existing model?](#how-to-load-an-existing-model)
Expand Down Expand Up @@ -72,9 +73,10 @@ To compile `pyfasttext`, make sure you have the following compiler:

### Simplest way to install pyfasttext: use pip

Just type this line:
Just type these lines:

```bash
pip install cython
pip install pyfasttext
```

Expand Down Expand Up @@ -121,7 +123,7 @@ Then, build and install with `setup.py`:
python setup.py install
```

#### Building and installing without Numpy
#### Building and installing without optional dependencies

`pyfasttext` can export word vectors as `numpy` `ndarray`s, however this feature can be disabled at compile time.

Expand All @@ -131,6 +133,8 @@ To compile without `numpy`, pyfasttext has a `USE_NUMPY` environment variable. S
USE_NUMPY=0 python setup.py install
```

If you want to compile without `cysignals`, likewise, you can set the `USE_CYSIGNALS` environment variable to 0 (or empty).

## Usage

### How to load the library?
Expand Down
30 changes: 18 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ pyfasttext
Yet another Python binding for
`fastText <https://github.com/facebookresearch/fastText>`__.

| The binding supports Python 2.6, 2.7 and Python 3. It requires
`Cython <http://cython.org/>`__ and
`cysignals <http://cysignals.readthedocs.io/en/latest/>`__.
| `Numpy <http://www.numpy.org/>`__ is also a dependency, but is
optional.
The binding supports Python 2.6, 2.7 and Python 3. It requires
`Cython <http://cython.org/>`__.

`Numpy <http://www.numpy.org/>`__ and
`cysignals <http://cysignals.readthedocs.io/en/latest/>`__ are also
dependencies, but are optional.

| ``pyfasttext`` has been tested successfully on Linux and Mac OS X.
| *Warning*: ``pyfasttext`` does not currently compile on Windows
because the ``cysignals`` module does not support this platform.
| *Warning*: if you want to compile ``pyfasttext`` on Windows, do not
compile with the ``cysignals`` module because it does not support this
platform.
Table of Contents
=================
Expand All @@ -32,8 +34,8 @@ Table of Contents
- `Building and installing
manually <#building-and-installing-manually>`__

- `Building and installing without
Numpy <#building-and-installing-without-numpy>`__
- `Building and installing without optional
dependencies <#building-and-installing-without-optional-dependencies>`__

- `Usage <#usage>`__

Expand Down Expand Up @@ -117,10 +119,11 @@ partial C++17 support.
Simplest way to install pyfasttext: use pip
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Just type this line:
Just type these lines:

.. code:: bash
pip install cython
pip install pyfasttext
Possible compilation error
Expand Down Expand Up @@ -176,8 +179,8 @@ Then, build and install with ``setup.py``:
python setup.py install
Building and installing without Numpy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Building and installing without optional dependencies
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

``pyfasttext`` can export word vectors as ``numpy`` ``ndarray``\ s,
however this feature can be disabled at compile time.
Expand All @@ -189,6 +192,9 @@ variable. Set this variable to 0 (or empty), like this:
USE_NUMPY=0 python setup.py install
If you want to compile without ``cysignals``, likewise, you can set the
``USE_CYSIGNALS`` environment variable to 0 (or empty).

Usage
-----

Expand Down
52 changes: 33 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@
from Cython.Build import cythonize
from glob import glob
from os.path import join
from subprocess import call
import os
import sys

VERSION = '0.4.4'

# if the module is being installed from pip using bdist_wheel or egg_info
# make sure cysignals is installed before compiling
if 'bdist_wheel' in sys.argv or 'egg_info' in sys.argv:
try:
import cysignals
except ImportError:
import pip
ret = pip.main(['install', 'cysignals'])
if ret:
raise RuntimeError('cannot install cysignals with pip')
VERSION = '0.4.5'

def to_bool(val):
if not val:
Expand All @@ -29,22 +19,36 @@ def to_bool(val):
val = 1
return bool(val)

# numpy support is optional
USE_NUMPY = to_bool(os.environ.get('USE_NUMPY', '1'))
# cysignals support is optional too
USE_CYSIGNALS = to_bool(os.environ.get('USE_CYSIGNALS', '1'))
if sys.platform == 'win32':
USE_CYSIGNALS = False

# if the module is being installed from pip using bdist_wheel or egg_info
# make sure cysignals is installed before compiling
if USE_CYSIGNALS and 'bdist_wheel' in sys.argv or 'egg_info' in sys.argv:
try:
import cysignals
except ImportError:
ret = call([sys.executable, '-m', 'pip', 'install', 'cysignals'])
if ret:
raise RuntimeError('cannot install cysignals with pip')

def get_fasttext_commit_hash():
try:
with open('.git/modules/fastText/HEAD', 'r') as f:
return f.read().strip()
except:
return 'unknown'

# numpy support is optional
USE_NUMPY = to_bool(os.environ.get('USE_NUMPY', '1'))

include_dirs = ['.', 'src/variant/include']
install_requires = ['cysignals', 'future']
include_dirs = ['.', 'src/variant/include', 'src']
setup_requires = []
install_requires = ['future', 'cysignals']

if USE_NUMPY:
import numpy as np
include_dirs.append(np.get_include())
setup_requires.append('numpy')
install_requires.append('numpy')

cpp_dir = join('src', 'fastText', 'src')
Expand All @@ -65,6 +69,14 @@ def build_extensions(self):
extra_compile_args.append('-std=c++0x')
build_ext.build_extensions(self)

def finalize_options(self):
build_ext.finalize_options(self)
# prevent numpy from thinking it is still in its setup process
if USE_NUMPY:
__builtins__.__NUMPY_SETUP__ = False
import numpy as np
self.include_dirs.append(np.get_include())

extension = Extension(
'pyfasttext',
sources=sources,
Expand All @@ -84,8 +96,10 @@ def build_extensions(self):
license='GPLv3',
package_dir={'': 'src'},
ext_modules=cythonize([extension], compile_time_env={'USE_NUMPY': USE_NUMPY,
'USE_CYSIGNALS': USE_CYSIGNALS,
'VERSION': VERSION,
'FASTTEXT_VERSION': get_fasttext_commit_hash()}),
setup_requires=setup_requires,
install_requires=install_requires,
cmdclass={'build_ext': BuildExt},
classifiers=[
Expand Down
5 changes: 4 additions & 1 deletion src/pyfasttext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ from libcpp.set cimport set
from libcpp.utility cimport pair
from libcpp.queue cimport priority_queue

from cysignals.signals cimport sig_on, sig_off, sig_check
IF USE_CYSIGNALS:
from cysignals.signals cimport sig_on, sig_off, sig_check
ELSE:
sig_on = sig_off = sig_check = lambda: None

import array

Expand Down

0 comments on commit 7dd28b6

Please sign in to comment.