|
1 | | -from fastapi import APIRouter, Query, HTTPException |
2 | | -from typing import Annotated, List, Union |
| 1 | +from fastapi import APIRouter, HTTPException |
| 2 | +from typing import List |
3 | 3 | import numpy as np |
4 | 4 |
|
5 | 5 | basic_math_router = APIRouter( |
|
11 | 11 |
|
12 | 12 | @basic_math_router.get("/addition") |
13 | 13 | async def addition( |
14 | | - q: Annotated[ |
15 | | - List[Union[int, float]], |
16 | | - Query(alias="numerical-array", title="List of numerical values"), |
17 | | - ], |
| 14 | + q: List[int | float], |
18 | 15 | ): |
19 | | - """This Python function calculates the sum of a list of numerical values provided as a query parameter. |
| 16 | + """Calculate the sum of a list of numbers. |
| 17 | +
|
| 18 | + This endpoint takes a query parameter `q` which is a list of numbers |
| 19 | + and returns their sum. |
20 | 20 |
|
21 | 21 | Parameters |
22 | 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. |
| 23 | + q : List[int | float] |
| 24 | + A list of numbers (integers or floats) to be added together. |
30 | 25 |
|
31 | 26 | Returns |
32 | 27 | ------- |
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. |
| 28 | + float |
| 29 | + The sum of all the numbers in the input list. |
36 | 30 |
|
| 31 | + Raises |
| 32 | + ------ |
| 33 | + HTTPException |
| 34 | + If any error occurs during the summation process. |
37 | 35 | """ |
38 | 36 | try: |
39 | | - array = np.array(q) |
40 | | - return np.sum(array) |
| 37 | + return np.sum(q) |
41 | 38 | except Exception as E: |
42 | 39 | raise HTTPException(status_code=400, detail=f"{E}") |
43 | 40 |
|
44 | 41 |
|
45 | 42 | @basic_math_router.get("/subtraction") |
46 | 43 | 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 | | - ], |
| 44 | + q1: List[int | float], |
| 45 | + q2: List[int | float], |
55 | 46 | ): |
56 | | - """This Python function performs subtraction on two arrays of numerical values provided as input. |
| 47 | + """Perform element-wise subtraction of two lists of numbers. |
| 48 | +
|
| 49 | + This endpoint takes two query parameters, `q1` and `q2`, which are two lists |
| 50 | + of numbers. It performs element-wise subtraction (q1 - q2). |
57 | 51 |
|
58 | 52 | Parameters |
59 | 53 | ---------- |
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. |
| 54 | + q1 : List[int | float] |
| 55 | + A list of numbers from which to subtract. |
| 56 | + q2 : List[int | float] |
| 57 | + A list of numbers to subtract. |
74 | 58 |
|
75 | 59 | Returns |
76 | 60 | ------- |
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. |
| 61 | + npt.NDArray[np.number] |
| 62 | + An array of numbers representing the result of the subtraction. |
| 63 | + This is serialized to a JSON list. |
81 | 64 |
|
| 65 | + Raises |
| 66 | + ------ |
| 67 | + HTTPException |
| 68 | + If any error occurs during the subtraction, for example, if the |
| 69 | + lists have different lengths. |
82 | 70 | """ |
83 | 71 | try: |
84 | | - array1 = np.array(q1) |
85 | | - array2 = np.array(q2) |
86 | | - return np.subtract(array1, array2) |
| 72 | + return np.subtract(q1, q2) |
87 | 73 | except Exception as E: |
88 | 74 | raise HTTPException(status_code=400, detail=f"{E}") |
0 commit comments