Skip to content

Commit 8edf127

Browse files
authored
Merge pull request #9 from drod75/david
Add and subtract routes
2 parents 9a8b576 + dcdc5b4 commit 8edf127

1 file changed

Lines changed: 87 additions & 2 deletions

File tree

src/api/routers/basic_math.py

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,88 @@
1-
from fastapi import APIRouter
1+
from fastapi import APIRouter, Query, HTTPException
2+
from typing import Annotated, List, Union
3+
import numpy as np
24

3-
basic_math_router = APIRouter()
5+
basic_math_router = APIRouter(
6+
prefix="/basic_math",
7+
tags=["basic_math"],
8+
responses={404: {"description": "Not Found"}},
9+
)
10+
11+
12+
@basic_math_router.get("/addition")
13+
async def addition(
14+
q: Annotated[
15+
List[Union[int, float]],
16+
Query(alias="numerical-array", title="List of numerical values"),
17+
],
18+
):
19+
"""This Python function calculates the sum of a list of numerical values provided as a query parameter.
20+
21+
Parameters
22+
----------
23+
q : Annotated[
24+
List[Union[int, float]],
25+
Query(alias="numerical-array", title="List of numerical values"),
26+
]
27+
The parameter `q` is a list of numerical values that are passed as a query parameter to the
28+
endpoint `/addition`. The values in the list can be integers or floats. The endpoint calculates the
29+
sum of all the numerical values in the list using NumPy's `np.sum` function.
30+
31+
Returns
32+
-------
33+
The code defines a route for performing addition operation on a list of numerical values provided
34+
as a query parameter. The function takes a list of numerical values as input, converts it to a NumPy
35+
array, calculates the sum of the array elements, and returns the result.
36+
37+
"""
38+
try:
39+
array = np.array(q)
40+
return np.sum(array)
41+
except Exception as E:
42+
raise HTTPException(status_code=400, detail=f"{E}")
43+
44+
45+
@basic_math_router.get("/subtraction")
46+
async def subtraction(
47+
q1: Annotated[
48+
List[Union[int, float]],
49+
Query(alias="numerical-array-1", title="List of numerical values 1."),
50+
],
51+
q2: Annotated[
52+
List[Union[int, float]],
53+
Query(alias="numerical-array-2", title="List of numerical values 2"),
54+
],
55+
):
56+
"""This Python function performs subtraction on two arrays of numerical values provided as input.
57+
58+
Parameters
59+
----------
60+
q1 : Annotated[
61+
List[Union[int, float]],
62+
Query(alias="numerical-array-1", title="List of numerical values 1."),
63+
]
64+
The parameter `q2` in the code snippet represents a list of numerical values 1. It is defined as a
65+
query parameter with an alias of "numerical-array-1" and a title of "List of numerical values 1".
66+
This parameter is expected to be a list containing elements that are either floats or ints.
67+
q2 : Annotated[
68+
List[Union[int, float]],
69+
Query(alias="numerical-array-2", title="List of numerical values 2"),
70+
]
71+
The parameter `q2` in the code snippet represents a list of numerical values 2. It is defined as a
72+
query parameter with an alias of "numerical-array-2" and a title of "List of numerical values 2".
73+
This parameter is expected to be a list containing elements that are either floats or ints.
74+
75+
Returns
76+
-------
77+
The code snippet defines an endpoint for subtraction operation where it takes two lists of
78+
numerical values as input and returns the result of subtracting the elements of the second list from
79+
the elements of the first list. The result returned is the element-wise subtraction of the two input
80+
arrays.
81+
82+
"""
83+
try:
84+
array1 = np.array(q1)
85+
array2 = np.array(q2)
86+
return np.subtract(array1, array2)
87+
except Exception as E:
88+
raise HTTPException(status_code=400, detail=f"{E}")

0 commit comments

Comments
 (0)