Skip to content

Commit f3d0726

Browse files
committed
Added docstrings
Fixed typos
1 parent 3e60bfb commit f3d0726

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from pydantic import BaseModel
22

33

4-
class ConversioHexResponse(BaseModel):
4+
class ConversionHexResponse(BaseModel):
5+
"""Hexadecimal value response"""
56
value: str
67

78

8-
class ConversioDecResponse(BaseModel):
9+
class ConversionDecResponse(BaseModel):
10+
"""Integer value response"""
911
value: int
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from fastapi import APIRouter
22

3-
from .views import calc_dec2hex, calc_hex2dec
3+
from .views import conv_dec2hex, conv_hex2dec
44

55

66
def init_routes():
77
router = APIRouter(prefix="/calculator", tags=["calculator"])
88

9-
router.add_api_route("/convert/dec2hex", calc_dec2hex, methods={"POST"})
10-
router.add_api_route("/convert/hex2dec", calc_hex2dec, methods={"POST"})
9+
router.add_api_route("/convert/dec2hex", conv_dec2hex, methods={"POST"})
10+
router.add_api_route("/convert/hex2dec", conv_hex2dec, methods={"POST"})
1111

1212
return router
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class ConverterService:
2+
"""Data conversion service"""
23
def __init__(self):
34
pass
45

56
def dec2hex(self, value: int) -> str:
7+
"""Convert integer to hexadecimal string with '0x' prefix"""
68
return hex(value)
79

810
def hex2dec(self, value: str) -> int:
11+
"""Convert hexadecimal string (with or without prefix) into integer.
12+
13+
Raises `ValueError` for incorrect input"""
914
return int(value, 16)

sample_fastapi/app/resources/calculator/views.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
from fastapi import Depends, HTTPException, status
22

3-
from .models import ConversioDecResponse, ConversioHexResponse
3+
from .models import ConversionDecResponse, ConversionHexResponse
44
from .services import ConverterService
55

66

7-
async def calc_dec2hex(value: int, converter: ConverterService = Depends(ConverterService)) -> ConversioHexResponse:
8-
return ConversioHexResponse(
7+
async def conv_dec2hex(value: int, converter: ConverterService = Depends(ConverterService)) -> ConversionHexResponse:
8+
"""Convert integer input into hexadecimal string"""
9+
return ConversionHexResponse(
910
value=converter.dec2hex(value),
1011
)
1112

1213

13-
async def calc_hex2dec(value: str, converter: ConverterService = Depends(ConverterService)) -> ConversioDecResponse:
14+
async def conv_hex2dec(value: str, converter: ConverterService = Depends(ConverterService)) -> ConversionDecResponse:
15+
"""Convert hexadecimal string input into integer"""
1416
try:
15-
return ConversioDecResponse(
17+
return ConversionDecResponse(
1618
value=converter.hex2dec(value),
1719
)
1820
except ValueError:

sample_fastapi/app/resources/hello/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33

44
class HelloMessage(BaseModel):
5+
"""Message response to hello request"""
56
message: str

sample_fastapi/app/resources/hello/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55

66
async def respond_hello() -> HelloMessage:
7+
"""Respond with "Hello, World!" message in a JSON"""
78
return HelloMessage(message="Hello, World!")
89

910

1011
async def respond_name_greet(name: str = Query()) -> HelloMessage:
12+
"""Respond with "Hello, <name>!" message in a JSON"""
1113
return HelloMessage(message=f"Hello, {name}!")

0 commit comments

Comments
 (0)