mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-05 19:46:47 +00:00
💡 add utils docstring
This commit is contained in:
@ -88,6 +88,10 @@ module.exports = context => ({
|
|||||||
title: "nonebot.log 模块",
|
title: "nonebot.log 模块",
|
||||||
path: "log"
|
path: "log"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: "nonebot.utils 模块",
|
||||||
|
path: "utils"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: "nonebot.exception 模块",
|
title: "nonebot.exception 模块",
|
||||||
path: "exception"
|
path: "exception"
|
||||||
|
@ -19,4 +19,7 @@
|
|||||||
* [nonebot.log](log.html)
|
* [nonebot.log](log.html)
|
||||||
|
|
||||||
|
|
||||||
|
* [nonebot.utils](utils.html)
|
||||||
|
|
||||||
|
|
||||||
* [nonebot.exception](exception.html)
|
* [nonebot.exception](exception.html)
|
||||||
|
68
docs/api/utils.md
Normal file
68
docs/api/utils.md
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
---
|
||||||
|
contentSidebar: true
|
||||||
|
sidebarDepth: 0
|
||||||
|
---
|
||||||
|
|
||||||
|
# NoneBot.utils 模块
|
||||||
|
|
||||||
|
|
||||||
|
## `run_sync(func)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
一个用于包装 sync function 为 async function 的装饰器
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `func: Callable[..., Any]`: 被装饰的同步函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **返回**
|
||||||
|
|
||||||
|
|
||||||
|
* Callable[..., Awaitable[Any]]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## _class_ `DataclassEncoder`
|
||||||
|
|
||||||
|
基类:`json.encoder.JSONEncoder`
|
||||||
|
|
||||||
|
|
||||||
|
* **类型**
|
||||||
|
|
||||||
|
`json.JSONEncoder`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
`JSONEncoder` used when encoding `Message` (List of dataclasses)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### `default(o)`
|
||||||
|
|
||||||
|
Implement this method in a subclass such that it returns
|
||||||
|
a serializable object for `o`, or calls the base implementation
|
||||||
|
(to raise a `TypeError`).
|
||||||
|
|
||||||
|
For example, to support arbitrary iterators, you could
|
||||||
|
implement default like this:
|
||||||
|
|
||||||
|
```default
|
||||||
|
def default(self, o):
|
||||||
|
try:
|
||||||
|
iterable = iter(o)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return list(iterable)
|
||||||
|
# Let the base class default method raise the TypeError
|
||||||
|
return JSONEncoder.default(self, o)
|
||||||
|
```
|
@ -7,4 +7,5 @@ NoneBot Api Reference
|
|||||||
- `nonebot.config <config.html>`_
|
- `nonebot.config <config.html>`_
|
||||||
- `nonebot.sched <sched.html>`_
|
- `nonebot.sched <sched.html>`_
|
||||||
- `nonebot.log <log.html>`_
|
- `nonebot.log <log.html>`_
|
||||||
|
- `nonebot.utils <utils.html>`_
|
||||||
- `nonebot.exception <exception.html>`_
|
- `nonebot.exception <exception.html>`_
|
||||||
|
11
docs_build/utils.rst
Normal file
11
docs_build/utils.rst
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
---
|
||||||
|
contentSidebar: true
|
||||||
|
sidebarDepth: 0
|
||||||
|
---
|
||||||
|
|
||||||
|
NoneBot.utils 模块
|
||||||
|
=================
|
||||||
|
|
||||||
|
.. automodule:: nonebot.utils
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
@ -10,6 +10,14 @@ from nonebot.typing import Any, Callable, Awaitable, overrides
|
|||||||
|
|
||||||
|
|
||||||
def run_sync(func: Callable[..., Any]) -> Callable[..., Awaitable[Any]]:
|
def run_sync(func: Callable[..., Any]) -> Callable[..., Awaitable[Any]]:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
一个用于包装 sync function 为 async function 的装饰器
|
||||||
|
:参数:
|
||||||
|
* ``func: Callable[..., Any]``: 被装饰的同步函数
|
||||||
|
:返回:
|
||||||
|
- Callable[..., Awaitable[Any]]
|
||||||
|
"""
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def _wrapper(*args: Any, **kwargs: Any) -> Any:
|
async def _wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
@ -22,6 +30,12 @@ def run_sync(func: Callable[..., Any]) -> Callable[..., Awaitable[Any]]:
|
|||||||
|
|
||||||
|
|
||||||
class DataclassEncoder(json.JSONEncoder):
|
class DataclassEncoder(json.JSONEncoder):
|
||||||
|
"""
|
||||||
|
:类型:
|
||||||
|
``json.JSONEncoder``
|
||||||
|
:说明:
|
||||||
|
``JSONEncoder`` used when encoding ``Message`` (List of dataclasses)
|
||||||
|
"""
|
||||||
|
|
||||||
@overrides(json.JSONEncoder)
|
@overrides(json.JSONEncoder)
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
|
Reference in New Issue
Block a user