Skip to content

Commit ec2cd1d

Browse files
committed
BLD: Attempt to build using spin
This commit is the first commit to try and move from poetry to spin. It is not working at the moment, namely the test runner cannot find the given tests.
1 parent 9080caf commit ec2cd1d

File tree

7 files changed

+126
-43
lines changed

7 files changed

+126
-43
lines changed

environment.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# To use:
2+
# $ conda env create -f environment.yml # `mamba` works too for this command
3+
# $ conda activate numpy-financial-dev
4+
#
5+
name: numpy-financial-dev
6+
channels:
7+
- conda-forge
8+
dependencies:
9+
- python
10+
- cython>=3.0.9
11+
- compilers
12+
- meson
13+
- meson-python
14+
- ninja
15+
- numpy
16+
- pytest
17+
- pytest-xdist
18+
- asv>=0.6.0
19+
- hypothesis
20+
- myst-parser
21+
- spin

meson.build

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
project(
2+
'numpy-financial',
3+
'cython',
4+
version: '2.0.0.dev',
5+
license: 'BSD-3',
6+
meson_version: '>=1.4.0',
7+
)
8+
9+
cy = meson.get_compiler('cython')
10+
11+
if not cy.version().version_compare('>=3.0.9')
12+
error('NumPy-Financial requires Cython >= 3.0.9')
13+
endif
14+
15+
py = import('python').find_installation(pure: false)
16+
py_dep = py.dependency()
17+
18+
subdir('numpy_financial')
19+
subdir('tests')

numpy_financial/_cfinancial.pyx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from libc.math cimport NAN
2+
3+
4+
def _cnpv(const double[::1] rates, const double[:, ::1] values, double[:, ::1] out):
5+
cdef:
6+
Py_ssize_t i, j, t
7+
double acc
8+
9+
for i in range(rates.shape[0]):
10+
for j in range(values.shape[0]):
11+
acc = 0.0
12+
for t in range(values.shape[1]):
13+
if rates[i] == -1.0:
14+
acc = NAN
15+
break
16+
else:
17+
acc += values[j, t] / ((1.0 + rates[i]) ** t)
18+
out[i, j] = acc

numpy_financial/_financial.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
from decimal import Decimal
1515

16-
import numba as nb
1716
import numpy as np
17+
from _cfinancial import _cnpv
1818

1919
__all__ = ['fv', 'pmt', 'nper', 'ipmt', 'ppmt', 'pv', 'rate',
2020
'irr', 'npv', 'mirr',
@@ -844,20 +844,6 @@ def irr(values, *, raise_exceptions=False):
844844
return eirr[np.argmin(abs_eirr)]
845845

846846

847-
@nb.njit
848-
def _npv_native(rates, values, out):
849-
for i in range(rates.shape[0]):
850-
for j in range(values.shape[0]):
851-
acc = 0.0
852-
for t in range(values.shape[1]):
853-
if rates[i] == -1.0:
854-
acc = np.nan
855-
break
856-
else:
857-
acc += values[j, t] / ((1.0 + rates[i]) ** t)
858-
out[i, j] = acc
859-
860-
861847
def npv(rate, values):
862848
r"""Return the NPV (Net Present Value) of a cash flow series.
863849
@@ -948,7 +934,7 @@ def npv(rate, values):
948934

949935
output_shape = _get_output_array_shape(rate_inner, values_inner)
950936
out = np.empty(output_shape)
951-
_npv_native(rate_inner, values_inner, out)
937+
_cnpv(rate_inner, values_inner, out)
952938
return _ufunc_like(out)
953939

954940

numpy_financial/meson.build

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
py.extension_module(
2+
'_cfinancial',
3+
'_cfinancial.pyx',
4+
install: true,
5+
subdir: 'numpy_financial',
6+
)
7+
8+
python_sources = [
9+
'_financial.py',
10+
]
11+
12+
py.install_sources(
13+
python_sources,
14+
subdir: 'numpy_financial',
15+
)

pyproject.toml

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
[build-system]
2-
requires = ["poetry-core"]
3-
build-backend = "poetry.core.masonry.api"
4-
2+
build-backend = "mesonpy"
3+
requires = [
4+
"meson-python>=0.15.0",
5+
"Cython>=3.0.9",
6+
"numpy>=1.23.5",
7+
]
58

6-
[tool.poetry]
9+
[project]
710
name = "numpy-financial"
811
version = "2.0.0"
12+
requires-python = ">=3.10"
913
description = "Simple financial functions"
1014
license = "BSD-3-Clause"
1115
authors = ["Travis E. Oliphant et al."]
@@ -34,30 +38,41 @@ classifiers = [
3438
"Operating System :: Unix",
3539
"Operating System :: MacOS",
3640
]
37-
packages = [{include = "numpy_financial"}]
38-
39-
[tool.poetry.dependencies]
40-
python = "^3.10"
41-
numpy = "^1.23"
42-
numba = "^0.59.1"
43-
44-
[tool.poetry.group.test.dependencies]
45-
pytest = "^8.0"
46-
hypothesis = {extras = ["numpy"], version = "^6.99.11"}
47-
pytest-xdist = {extras = ["psutil"], version = "^3.5.0"}
48-
49-
50-
[tool.poetry.group.docs.dependencies]
51-
sphinx = "^7.0"
52-
numpydoc = "^1.5"
53-
pydata-sphinx-theme = "^0.15"
54-
myst-parser = "^2.0.0"
55-
5641

42+
[project.optional-dependencies]
43+
test = [
44+
"pytest",
45+
"pytest-xdist",
46+
"hypothesis",
47+
]
48+
doc = [
49+
"sphinx>=7.0",
50+
"numpydoc>=1.5",
51+
"pydata-sphinx-theme>=0.15",
52+
"myst-parser>=2.0.0",
53+
]
54+
dev = [
55+
"ruff>=0.3.0",
56+
"asv>=0.6.0",
57+
]
5758

58-
[tool.poetry.group.lint.dependencies]
59-
ruff = "^0.3"
6059

60+
[tool.spin]
61+
package = 'numpy_financial'
6162

62-
[tool.poetry.group.bench.dependencies]
63-
asv = "^0.6"
63+
[tool.spin.commands]
64+
"Build" = [
65+
"spin.cmds.meson.build",
66+
"spin.cmds.meson.test",
67+
"spin.cmds.build.sdist",
68+
"spin.cmds.pip.install",
69+
]
70+
"Documentation" = [
71+
"spin.cmds.meson.docs"
72+
]
73+
"Environments" = [
74+
"spin.cmds.meson.shell",
75+
"spin.cmds.meson.ipython",
76+
"spin.cmds.meson.python",
77+
"spin.cmds.meson.run"
78+
]

tests/meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test_sources = [
2+
'__init__.py',
3+
'test_financial.py',
4+
]
5+
6+
py.install_sources(
7+
test_sources,
8+
subdir: 'tests',
9+
)

0 commit comments

Comments
 (0)