From d7289a2189d09d6074b4580bfe567a6059432a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9D=E3=82=AD?= Date: Sat, 2 Jan 2021 23:52:33 +0500 Subject: [PATCH] Docstrings - written docstrings for methods --- examples/async_example.py | 38 ++++++++++++++++++++++++--------- examples/sync_example.py | 42 +++++++++++++++++++++++++++---------- spamprotection/__init__.py | 1 + spamprotection/client.py | 43 ++++++++++++++++++++++++++++++++++---- spamprotection/sync.py | 43 ++++++++++++++++++++++++++++++++++---- spamprotection/types.py | 8 ++++++- 6 files changed, 145 insertions(+), 30 deletions(-) diff --git a/examples/async_example.py b/examples/async_example.py index 360750f..6a14678 100644 --- a/examples/async_example.py +++ b/examples/async_example.py @@ -8,7 +8,7 @@ client = SPBClient() async def main(): # calling for status - user = input("Enter a Username or UserID to check Spam Prediction on SPB: ") + user = input("Enter a Username or UserID: ") status = await client.check_blacklist(user) # check if status got a successful response if status.success: @@ -18,12 +18,20 @@ async def main(): async def text_parser(status: Blacklist): - text = "Private TelegramID: {}\n".format(status.private_telegram_id) + text = "Private TelegramID: {}\n".format( + status.private_telegram_id + ) text += "Entity Type: {}\n".format(status.entity_type) if status.attributes.is_blacklisted: - text += "Blacklist Flag: {}\n".format(status.attributes.blacklist_flag) - text += "Blacklist Reason: {}\n".format(status.attributes.blacklist_reason) - text += "Original PrivateID: {}\n".format(status.attributes.original_private_id) + text += "Blacklist Flag: {}\n".format( + status.attributes.blacklist_flag + ) + text += "Blacklist Reason: {}\n".format( + status.attributes.blacklist_reason + ) + text += "Original PrivateID: {}\n".format( + status.attributes.original_private_id + ) if status.attributes.is_potential_spammer: text += "This user is a Spammer\n" if status.attributes.is_operator: @@ -36,11 +44,21 @@ async def text_parser(status: Blacklist): text += "This user is an Intellivoid Verified Account\n" if status.attributes.is_official: text += "This is an Official Account\n" - text += "Language: {}\n".format(status.language_prediction.language) - text += "Language Probability: {}\n".format(status.language_prediction.probability) - text += "Ham Prediction: {}\n".format(status.spam_prediction.ham_prediction) - text += "Spam Prediction: {}\n".format(status.spam_prediction.spam_prediction) - text += "Last Updated On: {}\n".format(status.last_updated) + text += "Language: {}\n".format( + status.language_prediction.language + ) + text += "Language Probability: {}\n".format( + status.language_prediction.probability + ) + text += "Ham Prediction: {}\n".format( + status.spam_prediction.ham_prediction + ) + text += "Spam Prediction: {}\n".format( + status.spam_prediction.spam_prediction + ) + text += "Last Updated On: {}\n".format( + status.last_updated + ) return text diff --git a/examples/sync_example.py b/examples/sync_example.py index cc619eb..6ce5660 100644 --- a/examples/sync_example.py +++ b/examples/sync_example.py @@ -7,7 +7,7 @@ client = SPBClient() def main(): # calling for status - user = input("Enter a Username or UserID to check Spam Prediction on SPB: ") + user = input("Enter a Username or UserID: ") status = client.check_blacklist(user) # check if status got a successful response if status.success: @@ -17,12 +17,22 @@ def main(): def text_parser(status: Blacklist): - text = "Private TelegramID: {}\n".format(status.private_telegram_id) - text += "Entity Type: {}\n".format(status.entity_type) + text = "Private TelegramID: {}\n".format( + status.private_telegram_id + ) + text += "Entity Type: {}\n".format( + status.entity_type + ) if status.attributes.is_blacklisted: - text += "Blacklist Flag: {}\n".format(status.attributes.blacklist_flag) - text += "Blacklist Reason: {}\n".format(status.attributes.blacklist_reason) - text += "Original PrivateID: {}\n".format(status.attributes.original_private_id) + text += "Blacklist Flag: {}\n".format( + status.attributes.blacklist_flag + ) + text += "Blacklist Reason: {}\n".format( + status.attributes.blacklist_reason + ) + text += "Original PrivateID: {}\n".format( + status.attributes.original_private_id + ) if status.attributes.is_potential_spammer: text += "This user is a Spammer\n" if status.attributes.is_operator: @@ -35,11 +45,21 @@ def text_parser(status: Blacklist): text += "This user is an Intellivoid Verified Account\n" if status.attributes.is_official: text += "This is an Official Account\n" - text += "Language: {}\n".format(status.language_prediction.language) - text += "Language Probability: {}\n".format(status.language_prediction.probability) - text += "Ham Prediction: {}\n".format(status.spam_prediction.ham_prediction) - text += "Spam Prediction: {}\n".format(status.spam_prediction.spam_prediction) - text += "Last Updated On: {}\n".format(status.last_updated) + text += "Language: {}\n".format( + status.language_prediction.language + ) + text += "Language Probability: {}\n".format( + status.language_prediction.probability + ) + text += "Ham Prediction: {}\n".format( + status.spam_prediction.ham_prediction + ) + text += "Spam Prediction: {}\n".format( + status.spam_prediction.spam_prediction + ) + text += "Last Updated On: {}\n".format( + status.last_updated + ) return text diff --git a/spamprotection/__init__.py b/spamprotection/__init__.py index 4cda3b4..09f04c5 100644 --- a/spamprotection/__init__.py +++ b/spamprotection/__init__.py @@ -1,3 +1,4 @@ +"""Initial Directory.""" from .client import SPBClient # noqa: F401 __version__ = "0.0.4" diff --git a/spamprotection/client.py b/spamprotection/client.py index b489a3e..7aec4e7 100644 --- a/spamprotection/client.py +++ b/spamprotection/client.py @@ -8,11 +8,24 @@ from .types import Blacklist class SPBClient: def __init__( - self, *, host: str = "https://api.intellivoid.net/spamprotection/v1/lookup" + self, + *, + host: str = "https://api.intellivoid.net/spamprotection/v1/lookup" ) -> None: self._host = host - async def do_request(self, user_id: str, method: str = "get"): + async def do_request( + self, + user_id: str, + ): + """[Requests to the url] + + Args: + user_id (str): [username or user_id can be passed into the arg] + + Returns: + [json]: [json response of the output] + """ async with aiohttp.ClientSession() as ses: request = await ses.get(f"{self._host}?query={user_id}") try: @@ -20,14 +33,36 @@ class SPBClient: except JSONDecodeError: return await request.text(), request - async def raw_output(self, user_id: Union[int, str]) -> Union[Blacklist, bool]: + async def raw_output( + self, + user_id: Union[int, str] + ): + """[raw json output] + + Args: + user_id (Union[int, str]): [can pass user_id or username] + + Returns: + [json]: [returns json response] + """ try: data, _ = await self.do_request(user_id) return data except UnknownError: return False - async def check_blacklist(self, user_id: Union[int, str]) -> Union[Blacklist, bool]: + async def check_blacklist( + self, + user_id: Union[int, str] + ) -> Union[Blacklist, bool]: + """[checks spb for blacklist] + + Args: + user_id (Union[int, str]): [can pass user_id or username] + + Returns: + Union[Blacklist, bool]: [Blacklist type] + """ try: data, _ = await self.do_request(user_id) return Blacklist(**data) diff --git a/spamprotection/sync.py b/spamprotection/sync.py index edfe97e..3210dd2 100644 --- a/spamprotection/sync.py +++ b/spamprotection/sync.py @@ -9,25 +9,60 @@ from typing import Union class SPBClient: def __init__( - self, *, host: str = "https://api.intellivoid.net/spamprotection/v1/lookup" + self, + *, + host: str = "https://api.intellivoid.net/spamprotection/v1/lookup" ) -> None: self._host = host - def do_request(self, user_id: str, method: str = "get"): + def do_request( + self, + user_id: str, + ): + """[Requests to the url] + + Args: + user_id (str): [username or user_id can be passed into the arg] + + Returns: + [json]: [json response of the output] + """ request = requests.get(f"{self._host}?query={user_id}") try: return request.json(), request except JSONDecodeError: return request.text(), request - def raw_output(self, user_id: Union[int, str]) -> Union[Blacklist, bool]: + def raw_output( + self, + user_id: Union[int, str] + ): + """[raw json output] + + Args: + user_id (Union[int, str]): [can pass user_id or username] + + Returns: + [json]: [returns json response] + """ try: data, _ = self.do_request(user_id) return data except UnknownError: return False - def check_blacklist(self, user_id: Union[int, str]) -> Union[Blacklist, bool]: + def check_blacklist( + self, + user_id: Union[int, str] + ) -> Union[Blacklist, bool]: + """[checks spb for blacklist] + + Args: + user_id (Union[int, str]): [can pass user_id or username] + + Returns: + Union[Blacklist, bool]: [Blacklist type] + """ try: data, _ = self.do_request(user_id) return Blacklist(**data) diff --git a/spamprotection/types.py b/spamprotection/types.py index 52bba60..0299fd9 100644 --- a/spamprotection/types.py +++ b/spamprotection/types.py @@ -11,7 +11,13 @@ class SPB: class Blacklist(SPB): - def __init__(self, success: bool, response_code: int, results=dict, **kwargs): + def __init__( + self, + success: bool, + response_code: int, + results=dict, + **kwargs + ): self.success = success self.response_code = response_code try: