我正在准备一个api,并使用文档字符串作为文档。 api 服务选择相关的 ApiClass 方法并连接每个文档字符串以创建文档。这样,程序开发人员和 API 用户都可以获得相同的文档。
我的班级结构是这样的:
API_STATUS = {
1: 'some status',
2: 'some other status message'
}
class MyApi:
def __init__(self):
blah blah blah
def ApiService1(self, some_param):
"""
here is the documentation
* some thing
* some other thing
keep on explanation
"""
do some job
def ApiService2(self, some_param):
""""
Another doc...
"""
do some other job
我在用HttpResponse
返回最终的文档字符串。因此,当我请求服务文档时,输出非常可读
API服务1
here is the documentation
* some thing
* some other thing
keep on explanation
API服务2
Another doc...
到目前为止一切都很好,但是有一些变量,例如API_STATUS
字典和一些列表,我希望将它们添加到文档中。但是当我将它们解析为字符串或调用时repr
功能,所有格式都消失了
{1: '某些状态' 2: '其他一些状态消息', 3: '.....', 4: '........', ....}
这使得它不可读(因为 dict 有大约 50 个元素。)。
我不想将其写为文档字符串(因为在将来的更新中,相关的字典可能会更新并且字典字符串可能会被忘记)
有没有办法将我的字典添加到我的响应文档字符串中(在将其返回之前)HttpResponse
)而不删除样式缩进?