Skip to content

Commit 66642e4

Browse files
committed
Restructure python exposed functions to a seperate module
1 parent 8bd6d1a commit 66642e4

6 files changed

Lines changed: 96 additions & 42 deletions

File tree

cpp/bindings/bindings.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@ namespace py = pybind11;
66

77
PYBIND11_MODULE(_core, m)
88
{
9-
m.doc() = "C++ core library extension exposed via pybind11";
10-
11-
m.def("add", &core_lib::add, py::arg("a"), py::arg("b"), "Return the sum of a and b.");
12-
m.def("subtract", &core_lib::subtract, py::arg("a"), py::arg("b"), "Return the difference of a and b.");
13-
m.def("multiply", &core_lib::multiply, py::arg("a"), py::arg("b"), "Return the product of a and b.");
14-
m.def(
15-
"divide",
16-
&core_lib::divide,
17-
py::arg("a"),
18-
py::arg("b"),
19-
"Return the quotient of a divided by b. "
20-
"Raises ValueError (from C++ std::invalid_argument) if b is exactly zero."
21-
);
9+
m.doc() = "Test";
10+
py::module math = m.def_submodule("math", "math module");
11+
math.def("add", &core_lib::add);
12+
math.def("subtract", &core_lib::subtract);
13+
math.def("multiply", &core_lib::multiply);
14+
math.def("divide", &core_lib::divide);
2215
}

python/docs/api.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ API Reference
66
Arithmetic functions
77
--------------------
88

9-
.. autofunction:: core_lib.add
9+
.. autofunction:: core_lib.math.add
1010

11-
.. autofunction:: core_lib.subtract
11+
.. autofunction:: core_lib.math.subtract
1212

13-
.. autofunction:: core_lib.multiply
13+
.. autofunction:: core_lib.math.multiply
1414

15-
.. autofunction:: core_lib.divide
15+
.. autofunction:: core_lib.math.divide

python/src/core_lib/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
"""C++ core library extension exposed via pybind11."""
2-
3-
from core_lib._core import add, divide, multiply, subtract
4-
5-
__all__ = ["add", "divide", "multiply", "subtract"]

python/src/core_lib/math.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from core_lib._core import math as _math
2+
3+
4+
def add(a: float, b: float) -> float:
5+
"""Return the sum of two numbers.
6+
7+
Args:
8+
a: The first number.
9+
b: The second number.
10+
11+
Returns:
12+
The sum of a and b.
13+
"""
14+
return _math.add(a, b)
15+
16+
17+
def subtract(a: float, b: float) -> float:
18+
"""Return the result of subtracting one number from another.
19+
20+
Args:
21+
a: The number to subtract from.
22+
b: The number to subtract.
23+
24+
Returns:
25+
The result of a minus b.
26+
"""
27+
return _math.subtract(a, b)
28+
29+
30+
def multiply(a: float, b: float) -> float:
31+
"""Return the product of two numbers.
32+
33+
Args:
34+
a: The first number.
35+
b: The second number.
36+
37+
Returns:
38+
The product of a and b.
39+
"""
40+
return _math.multiply(a, b)
41+
42+
43+
def divide(a: float, b: float) -> float:
44+
"""Return the result of dividing one number by another.
45+
46+
Args:
47+
a: The numerator.
48+
b: The denominator.
49+
50+
Returns:
51+
The result of dividing a by b.
52+
53+
Raises:
54+
ZeroDivisionError: If b is zero.
55+
"""
56+
return _math.divide(a, b)
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
"""Integration tests for the core_lib package."""
1+
import pytest
22

3-
import core_lib
3+
import core_lib.math
44

55

6-
def test_core_not_directly_importable_as_public() -> None:
7-
"""Verify _core is private (not in __all__)."""
8-
assert "_core" not in core_lib.__all__
6+
def test_chained_arithmetic() -> None:
7+
# (10 + 5) * 3 / 5 - 2 = 7.0
8+
result = core_lib.math.add(10.0, 5.0)
9+
result = core_lib.math.multiply(result, 3.0)
10+
result = core_lib.math.divide(result, 5.0)
11+
result = core_lib.math.subtract(result, 2.0)
12+
assert result == 7.0
13+
14+
15+
def test_divide_by_zero_in_chain() -> None:
16+
result = core_lib.math.subtract(5.0, 5.0)
17+
with pytest.raises(ValueError) as _:
18+
core_lib.math.divide(10.0, result)

python/tests/unit/test_core_lib.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
import pytest
44

5-
import core_lib
5+
import core_lib.math
66

77

88
def test_add() -> None:
9-
assert core_lib.add(1.0, 2.0) == 3.0
10-
assert core_lib.add(-1.0, 1.0) == 0.0
11-
assert core_lib.add(0.0, 0.0) == 0.0
9+
assert core_lib.math.add(1.0, 2.0) == 3.0
10+
assert core_lib.math.add(-1.0, 1.0) == 0.0
11+
assert core_lib.math.add(0.0, 0.0) == 0.0
1212

1313

1414
def test_subtract() -> None:
15-
assert core_lib.subtract(5.0, 3.0) == 2.0
16-
assert core_lib.subtract(0.0, 5.0) == -5.0
17-
assert core_lib.subtract(3.0, 3.0) == 0.0
15+
assert core_lib.math.subtract(5.0, 3.0) == 2.0
16+
assert core_lib.math.subtract(0.0, 5.0) == -5.0
17+
assert core_lib.math.subtract(3.0, 3.0) == 0.0
1818

1919

2020
def test_multiply() -> None:
21-
assert core_lib.multiply(3.0, 4.0) == 12.0
22-
assert core_lib.multiply(-2.0, 3.0) == -6.0
23-
assert core_lib.multiply(0.0, 100.0) == 0.0
21+
assert core_lib.math.multiply(3.0, 4.0) == 12.0
22+
assert core_lib.math.multiply(-2.0, 3.0) == -6.0
23+
assert core_lib.math.multiply(0.0, 100.0) == 0.0
2424

2525

2626
def test_divide() -> None:
27-
assert core_lib.divide(10.0, 2.0) == 5.0
28-
assert core_lib.divide(7.0, 2.0) == 3.5
29-
assert core_lib.divide(-6.0, 3.0) == -2.0
27+
assert core_lib.math.divide(10.0, 2.0) == 5.0
28+
assert core_lib.math.divide(7.0, 2.0) == 3.5
29+
assert core_lib.math.divide(-6.0, 3.0) == -2.0
3030

3131

3232
def test_divide_by_zero() -> None:
3333
with pytest.raises(ValueError):
34-
core_lib.divide(1.0, 0.0)
34+
core_lib.math.divide(1.0, 0.0)
3535
with pytest.raises(ValueError):
36-
core_lib.divide(-5.0, 0.0)
36+
core_lib.math.divide(-5.0, 0.0)

0 commit comments

Comments
 (0)