Fal*_*gel 3 python django formatting dictionary
我正在准备api,并使用docstrings作为文档.api服务选择相关的ApiClass方法并连接每个docstring以创建文档.这样,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
Run Code Online (Sandbox Code Playgroud)
我HttpResponse用来返回最后的文档字符串.因此,当我请求服务文档时,输出非常易读
ApiService1
Run Code Online (Sandbox Code Playgroud)here is the documentation * some thing * some other thing keep on explanationApiService2
Run Code Online (Sandbox Code Playgroud)Another doc...
一切都很好,但有一些变量,如API_STATUS字典和一些列表,我希望将它们添加到文档中.但是当我将它们解析为字符串或调用repr函数时,所有格式都消失了
{1:'某种状态'2:'其他状态信息',3:'.....',4:'........',....}
这使得它不可读(因为dict有大约50个元素.).
我不想写作文档字符串(因为在将来的更新中,相关的字典可能会被更新,dicstring可能会被遗忘)
有没有办法将我的字典添加到我的响应文档字符串(在返回之前HttpResponse),而不删除sytling缩进?
okm*_*okm 14
使用pprint:
>>> API_STATUS = {1: 'some status', 2: 'some other status message'}
>>> import pprint
>>> pprint.pprint(API_STATUS, width=1)
{1: 'some status',
2: 'some other status message'}
Run Code Online (Sandbox Code Playgroud)