Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output/Print a "readable" dictionary

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.

My class structure is like:

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

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

ApiService1

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

ApiService2

    Another doc...

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: '........', ....}

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

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)

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

like image 390
FallenAngel Avatar asked Dec 07 '22 14:12

FallenAngel


1 Answers

Use 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'}
like image 188
okm Avatar answered Dec 20 '22 05:12

okm