输出/打印“可读”字典 [英] Output/Print a "readable" dictionary

查看:181
本文介绍了输出/打印“可读”字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备一个api,并使用 docstrings 作为文档。 api服务选择相关的ApiClass方法并加入每个docstring以创建文档。这样,程序开发人员和api的用户都达到了相同的文档。

I am preparing an api, and using docstrings as documentation. An api service selects the related ApiClass methods and join each docstring to create the documentation. This way, both program developers and users of the api reached the same documentation.

我的类结构如下:

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 返回最终的文档字符串,所以当我请求服务文档时,输出是相当可读的

I am using HttpResponse to return the final documentation string. So when i request service documentation, output is quite readable


ApiService1

ApiService1

    here is the documentation
        * some thing
        * some other thing
    keep on explanation

ApiService2

ApiService2

    Another doc...


所有这些都很棒,但是有一些变量,如 API_STATUS 字典ans列表,我希望将它们添加到文档中。但是当我解析它们到字符串,或者调用 repr 函数时,所有格式化都将消失

All is great up to here, but there is some variables like API_STATUS dictionary ans some lists, which i wish to add them to the documentation. But when i parse them to string, or call repr function, all formatting is gone


{1:'some status'2:'some other status message',3:'.....',4:'........',....}

{1: 'some status' 2: 'some other status message', 3: '.....', 4: '........', ....}

这使得它不可读(因为dict有大约50个元素)。

which makes it unreadable (since dict has about 50 elements.).

我做不想写作是一个docstring(因为在将来的更新,相关的dict可能会被更新和dicstring可能被遗忘)

I do not want to write is down as a docstring (because in future updates, related dict may be updated and dicstring might be forgotten)

有没有办法添加我的字典到我的响应文档字符串(在将它作为 HttpResponse 返回之前),而不删除符号缩写?

Is there a way to add my dictionary to my responsing documentation string (before returning it as HttpResponse) without removing sytling indents?

推荐答案

使用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'}

这篇关于输出/打印“可读”字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆