From e639889944f36f07bc77a307e6d3145c1580e178 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Wed, 4 Jul 2018 19:50:42 +0800 Subject: [PATCH] Add some docstrings --- none/__init__.py | 18 ++++++++++++++++-- none/helpers.py | 12 +++--------- none/session.py | 8 ++------ none_demo/__init__.py | 4 ++-- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/none/__init__.py b/none/__init__.py index d18c45c9..f3a1c128 100644 --- a/none/__init__.py +++ b/none/__init__.py @@ -67,17 +67,30 @@ class NoneBot(CQHttp): _bot = None -def init(config_object: Any = None) -> NoneBot: +def init(config_object: Any = None) -> None: + """ + Initialize NoneBot instance. + + This function must be called at the very beginning of code, + otherwise the get_bot() function will return None and nothing + is gonna work properly. + + :param config_object: configuration object + """ global _bot _bot = NoneBot(config_object) if _bot.config.DEBUG: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) - return _bot def get_bot() -> NoneBot: + """ + Get the NoneBot instance. + + :raise ValueError: instance not initialized + """ if _bot is None: raise ValueError('NoneBot instance has not been initialized') # noinspection PyTypeChecker @@ -85,6 +98,7 @@ def get_bot() -> NoneBot: def run(host: str = None, port: int = None, *args, **kwargs) -> None: + """Run the NoneBot instance.""" get_bot().run(host=host, port=port, *args, **kwargs) diff --git a/none/helpers.py b/none/helpers.py index 38179a20..49c366a8 100644 --- a/none/helpers.py +++ b/none/helpers.py @@ -6,9 +6,7 @@ from . import NoneBot, expression def context_id(ctx: Dict[str, Any]) -> str: - """ - Calculate a unique id representing the current user. - """ + """Calculate a unique id representing the current user.""" src = '' if ctx.get('group_id'): src += f'/group/{ctx["group_id"]}' @@ -22,9 +20,7 @@ def context_id(ctx: Dict[str, Any]) -> str: async def send(bot: NoneBot, ctx: Dict[str, Any], message: Union[str, Dict[str, Any], List[Dict[str, Any]]], *, ignore_failure: bool = True) -> None: - """ - Send a message ignoring failure by default. - """ + """Send a message ignoring failure by default.""" try: if ctx.get('post_type') == 'message': await bot.send(ctx, message) @@ -46,7 +42,5 @@ async def send(bot: NoneBot, ctx: Dict[str, Any], async def send_expr(bot: NoneBot, ctx: Dict[str, Any], expr: Union[str, Sequence[str], Callable], **kwargs): - """ - Sending a expression message ignoring failure by default. - """ + """Sending a expression message ignoring failure by default.""" return await send(bot, ctx, expression.render(expr, **kwargs)) diff --git a/none/session.py b/none/session.py index 156cf963..7ee3606c 100644 --- a/none/session.py +++ b/none/session.py @@ -14,16 +14,12 @@ class BaseSession: async def send(self, message: Union[str, Dict[str, Any], List[Dict[str, Any]]], *, ignore_failure: bool = True) -> None: - """ - Send a message ignoring failure by default. - """ + """Send a message ignoring failure by default.""" return await send(self.bot, self.ctx, message, ignore_failure=ignore_failure) async def send_expr(self, expr: Union[str, Sequence[str], Callable], **kwargs): - """ - Sending a expression message ignoring failure by default. - """ + """Sending a expression message ignoring failure by default.""" return await send_expr(self.bot, self.ctx, expr, **kwargs) diff --git a/none_demo/__init__.py b/none_demo/__init__.py index 5d5fbe2d..629f4a8a 100644 --- a/none_demo/__init__.py +++ b/none_demo/__init__.py @@ -3,8 +3,8 @@ from os import path import none from none_demo import config -bot = none.init(config) -app = bot.asgi +none.init(config) +app = none.get_bot().asgi if __name__ == '__main__': none.load_builtin_plugins()