Skip to content

Commit d8736cd

Browse files
authored
Merge pull request #148 from nanotaboada/feature/docstring
chore: add missing class and function docstring
2 parents f1ad660 + 510f2f7 commit d8736cd

File tree

6 files changed

+130
-16
lines changed

6 files changed

+130
-16
lines changed

.pylintrc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[MASTER]
22
disable=
3-
C0111, # missing-docstring
43
C0114, # missing-module-docstring
5-
C0115, # missing-class-docstring
6-
C0116, # missing-function-docstring
74
C0301, # line-too-long
5+
R0902, # too-many-instance-attributes
86
R0903, # too-few-public-methods
9-
W0718, # broad-exception-caught
7+
R0913, # too-many-arguments

data/players-sqlite3.db

4 KB
Binary file not shown.

models/player_model.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,38 @@
88

99

1010
class MainModel(BaseModel):
11+
"""
12+
Base model configuration for all Pydantic models in the application.
13+
14+
This class sets a common configuration for alias generation and name population
15+
for any model that inherits from it. It uses camelCase for JSON field names.
16+
17+
Attributes:
18+
model_config (ConfigDict): Configuration for Pydantic models, including:
19+
alias_generator (function): A function to generate field aliases.
20+
Here, it uses `to_camel` to convert field names to camelCase.
21+
populate_by_name (bool): Allows population of fields by name when using Pydantic models.
22+
"""
1123
model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)
1224

1325

1426
class PlayerModel(MainModel):
27+
"""
28+
Pydantic model representing a football Player.
29+
30+
Attributes:
31+
id (int): The unique identifier for the player.
32+
first_name (str): The first name of the player.
33+
middle_name (Optional[str]): The middle name of the player, if any.
34+
last_name (str): The last name of the player.
35+
date_of_birth (Optional[str]): The date of birth of the player, if provided.
36+
squad_number (int): The unique squad number assigned to the player.
37+
position (str): The playing position of the player.
38+
abbr_position (Optional[str]): The abbreviated form of the player's position, if any.
39+
team (Optional[str]): The team to which the player belongs, if any.
40+
league (Optional[str]): The league where the team plays, if any.
41+
starting11 (Optional[bool]): Indicates if the player is in the starting 11, if provided.
42+
"""
1543
id: int
1644
first_name: str
1745
middle_name: Optional[str]

routes/player_route.py

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020

2121
def get_orm_session():
22+
"""
23+
Dependency function to yield a scoped SQLAlchemy ORM session.
24+
25+
Yields:
26+
OrmSession: An instance of a scoped SQLAlchemy ORM session.
27+
"""
2228
orm_session = OrmSession()
2329
try:
2430
yield orm_session
@@ -31,12 +37,22 @@ def get_orm_session():
3137
@api_router.post(
3238
"/players/",
3339
status_code=status.HTTP_201_CREATED,
34-
summary="Creates a new Player",
40+
summary="Creates a new Player"
3541
)
3642
def post(
3743
player_model: PlayerModel = Body(...),
38-
orm_session: Session = Depends(get_orm_session),
44+
orm_session: Session = Depends(get_orm_session)
3945
):
46+
"""
47+
Endpoint to create a new player.
48+
49+
Args:
50+
player_model (PlayerModel): The data model representing a Player.
51+
orm_session (Session): The SQLAlchemy ORM session.
52+
53+
Raises:
54+
HTTPException: HTTP 409 Conflict error if the Player already exists.
55+
"""
4056
player = player_service.retrieve_by_id(orm_session, player_model.id)
4157

4258
if player:
@@ -59,6 +75,15 @@ def post(
5975
def get_all(
6076
orm_session: Session = Depends(get_orm_session)
6177
):
78+
"""
79+
Endpoint to retrieve all players.
80+
81+
Args:
82+
orm_session (Session): The SQLAlchemy ORM session.
83+
84+
Returns:
85+
List[PlayerModel]: A list of data models representing all players.
86+
"""
6287
players = player_service.retrieve_all(orm_session)
6388

6489
return players
@@ -72,9 +97,22 @@ def get_all(
7297
)
7398
@cache(expire=CACHING_TIME_IN_SECONDS)
7499
def get_by_id(
75-
player_id: int = Path(..., title="The Id of the Player"),
100+
player_id: int = Path(..., title="The ID of the Player"),
76101
orm_session: Session = Depends(get_orm_session)
77102
):
103+
"""
104+
Endpoint to retrieve a Player by its ID.
105+
106+
Args:
107+
player_id (int): The ID of the Player to retrieve.
108+
orm_session (Session): The SQLAlchemy ORM session.
109+
110+
Returns:
111+
PlayerModel: A data model representing the Player.
112+
113+
Raises:
114+
HTTPException: Not found error if the Player with the specified ID does not exist.
115+
"""
78116
player = player_service.retrieve_by_id(orm_session, player_id)
79117

80118
if not player:
@@ -94,6 +132,19 @@ def get_by_squad_number(
94132
squad_number: int = Path(..., title="The Squad Number of the Player"),
95133
orm_session: Session = Depends(get_orm_session)
96134
):
135+
"""
136+
Endpoint to retrieve a Player by its Squad Number.
137+
138+
Args:
139+
squad_number (int): The Squad Number of the Player to retrieve.
140+
orm_session (Session): SQLAlchemy ORM session.
141+
142+
Returns:
143+
PlayerModel: A data model representing the Player.
144+
145+
Raises:
146+
HTTPException: HTTP 404 Not Found error if the Player with the specified Squad Number does not exist.
147+
"""
97148
player = player_service.retrieve_by_squad_number(orm_session, squad_number)
98149

99150
if not player:
@@ -110,11 +161,21 @@ def get_by_squad_number(
110161
summary="Updates an existing Player"
111162
)
112163
def put(
113-
player_id: int = Path(..., title="The Id of the Player"),
164+
player_id: int = Path(..., title="The ID of the Player"),
114165
player_model: PlayerModel = Body(...),
115-
orm_session: Session = Depends(get_orm_session),
116-
166+
orm_session: Session = Depends(get_orm_session)
117167
):
168+
"""
169+
Endpoint to entirely update an existing Player.
170+
171+
Args:
172+
player_id (int): The ID of the Player to update.
173+
player_model (PlayerModel): The data model representing the Player to update.
174+
orm_session (Session): The SQLAlchemy ORM session.
175+
176+
Raises:
177+
HTTPException: HTTP 404 Not Found error if the player with the specified ID does not exist.
178+
"""
118179
player = player_service.retrieve_by_id(orm_session, player_id)
119180

120181
if not player:
@@ -133,13 +194,23 @@ def put(
133194
summary="Deletes an existing Player"
134195
)
135196
def delete(
136-
player_id: int = Path(..., title="The Id of the Player"),
197+
player_id: int = Path(..., title="The ID of the Player"),
137198
orm_session: Session = Depends(get_orm_session)
138199
):
200+
"""
201+
Endpoint to delete an existing Player.
202+
203+
Args:
204+
player_id (int): The ID of the Player to delete.
205+
orm_session (Session): The SQLAlchemy ORM session.
206+
207+
Raises:
208+
HTTPException: HTTP 404 Not Found error if the Player with the specified ID does not exist.
209+
"""
139210
player = player_service.retrieve_by_id(orm_session, player_id)
140211

141212
if not player:
142-
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Player not found")
213+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
143214

144215
player_service.delete(orm_session, player_id)
145216

schemas/player_schema.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,31 @@
77

88

99
class Player(Base):
10+
"""
11+
SQLAlchemy schema describing a database table of football players.
12+
13+
Attributes:
14+
id (Integer): The primary key for the player record.
15+
first_name (String): The first name of the player (not nullable).
16+
middle_name (String): The middle name of the player.
17+
last_name (String): The last name of the player (not nullable).
18+
date_of_birth (String): The date of birth of the player.
19+
squad_number (Integer): The squad number of the player (not nullable, unique).
20+
position (String): The playing position of the player (not nullable).
21+
abbr_position (String): The abbreviated form of the player's position.
22+
team (String): The team to which the player belongs.
23+
league (String): The league where the team plays.
24+
starting11 (Boolean): Indicates if the player is in the starting 11.
25+
"""
26+
1027
__tablename__ = "players"
1128

12-
id = Column(Integer, primary_key=True, index=True)
29+
id = Column(Integer, primary_key=True)
1330
first_name = Column(String, name='firstName', nullable=False)
1431
middle_name = Column(String, name='middleName')
1532
last_name = Column(String, name='lastName', nullable=False)
1633
date_of_birth = Column(String, name="dateOfBirth")
17-
squad_number = Column(Integer, name='squadNumber', index=True, nullable=False)
34+
squad_number = Column(Integer, name='squadNumber', unique=True, nullable=False)
1835
position = Column(String, nullable=False)
1936
abbr_position = Column(String, name='abbrPosition')
2037
team = Column(String)

services/player_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def retrieve_by_squad_number(orm_session: Session, squad_number: int):
8282

8383

8484
def update(orm_session: Session, player_model: PlayerModel):
85-
"""Updates an existing Player in the database.
85+
"""Updates (entirely) an existing Player in the database.
8686
8787
Args:
8888
orm_session: The SQLAlchemy ORM session instance.
@@ -115,7 +115,7 @@ def update(orm_session: Session, player_model: PlayerModel):
115115

116116

117117
def delete(orm_session: Session, player_id: int):
118-
"""Deletes a Player from the database.
118+
"""Deletes an existing Player from the database.
119119
120120
Args:
121121
orm_session: The SQLAlchemy ORM session instance.

0 commit comments

Comments
 (0)