Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 9884f59

Browse files
Adding comment to __init__ for clarity
1 parent 2bec4f8 commit 9884f59

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

projects/url_shortener/app/services/url_shortener.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@
66

77

88
class URLShortener(BaseModel):
9-
url: str
10-
_shortener: Optional[pyshorteners.Shortener] = PrivateAttr()
9+
url: str
10+
_shortener: Optional[pyshorteners.Shortener] = PrivateAttr()
1111

12-
class Config:
13-
arbitrary_types_allowed = True
12+
class Config:
13+
arbitrary_types_allowed = True
1414

15-
def __init__(self, **data):
16-
shortener = data.pop("shortener", None) or pyshorteners.Shortener()
17-
super().__init__(**data)
18-
self._shortener = shortener
15+
def __init__(self, **data):
16+
"""
17+
Initialize the URLShortener. If no 'shortener' is provided in data,
18+
a new pyshorteners.Shortener instance is created.
19+
"""
20+
shortener = data.pop("shortener", None) or pyshorteners.Shortener()
21+
super().__init__(**data)
22+
self._shortener = shortener
1923

20-
def shorten_url(self) -> Union[str, Exception]:
21-
if self._shortener is None:
22-
raise ValueError("Shortener is not initialized.")
24+
def shorten_url(self) -> Union[str, Exception]:
25+
if self._shortener is None:
26+
raise ValueError("Shortener is not initialized.")
2327

24-
try:
25-
return str(self._shortener.tinyurl.short(self.url))
26-
except (ParsingError, ValidationError) as error:
27-
return error
28-
except Exception as generic_error:
29-
return generic_error
28+
try:
29+
return str(self._shortener.tinyurl.short(self.url))
30+
except (ParsingError, ValidationError) as error:
31+
return error
32+
except Exception as generic_error:
33+
return generic_error

0 commit comments

Comments
 (0)