Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
feat: add docstring to Result and ResultDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
HamidMolareza committed Mar 31, 2023
1 parent 234f870 commit 381998e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions def_result/Result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class Result:
""" Stores result of function """
success: bool
detail: Optional[ResultDetail] = None
value: Optional[Any] = None
Expand All @@ -15,13 +16,16 @@ def __init__(self, success: bool, detail: Optional[ResultDetail] = None, value:

@staticmethod
def ok(value: Optional[Any] = None, detail: Optional[ResultDetail] = None):
""" Returns a success result. """
return Result(True, detail=detail, value=value)

@staticmethod
def fail(detail: Optional[ResultDetail] = None):
""" Returns a failure result. """
return Result(False, detail)

def code(self, default_success_code: int = 200, default_error_code: int = 500) -> int:
""" Returns the result code. """
if self.detail and self.detail.code:
return self.detail.code
return default_success_code if self.success else default_error_code
5 changes: 5 additions & 0 deletions def_result/ResultDetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class ResultDetail:
""" Stores the details of a result. """

title: str
message: Optional[str]
code: Optional[int]
Expand All @@ -20,9 +22,12 @@ def __init__(self, title: str,
self.more_data = more_data

def is_instance_of(self, cls):
""" Checks if the result detail is an instance of the given class. """
return isinstance(self, cls)

def add_more_data(self, data: Any) -> None:
""" Adds more data to the result detail """

if not data:
return
if not self.more_data:
Expand Down

0 comments on commit 381998e

Please sign in to comment.