Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add indentation feature to reprlib.Repr #92734

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
@finefoot

Description

@finefoot

Withreprlib.Repr, it's possible to print object representations with a limit of recursive depth to increase readability of large objects. "This is used in the Python debugger and may be useful in other contexts as well."[1] For example, I'm usingreprlib to log lines for debugging. In the same line of thought, it makes sense to be able to format the output to increase readability even further. One measure to do so is to insert line breaks with indentation.

Example

>>>example= [1, [2,"foo",b"bar", {"a":1,"b":"abc def ghi","c": {1:2,3:4,5: [],6: {}}}],3]

At first glance,pprint.PrettyPrinter might be an alternative solution:

>>>importpprint>>>pprint.pprint(example,indent=4)[1,    [2,'foo',b'bar',        {'a':1,'b':'abc def ghi','c': {1:2,3:4,5: [],6: {}}}],3]>>>pprint.pprint(example,indent=4,depth=3)[1, [2,'foo',b'bar', {'a':1,'b':'abc def ghi','c': {...}}],3]

However, in the first example above, the innerdict has not been expanded by line breaks and indentation. In the second example, after reducingdepth, everything is suddenly printed onto one line only. This is the case, because the motivation and goal ofpprint is to optimize output for a givenwidth topretty print - not to produce consistent output for debugging purposes. This becomes even more apparent when reducingwidth:

>>>pprint.pprint(example,indent=4,depth=3,width=10)[1,    [2,'foo',b'bar',        {'a':1,'b':'abc ''def ''ghi','c': {1:2,3:4,5: [   ],6: {   }}}],3]

Note how strings that are too long forwidth, like"abc def ghi" inexample, get split into multiple lines. This is not optimal for logging output where the width of the output (i.e. the actual line length) is usually not restricted.

Usingjson.dumps produces a similar output as the requested indentation feature forreprlib:

>>>importjson>>>print(json.dumps({"a":1,"b": [2,3]},indent=4)){"a":1,"b": [2,3    ]}

However, it is subject to the known restrictions when converting Python objects to JSON:

>>>json.dumps(example)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/lib/python3.9/json/__init__.py",line231,indumpsreturn_default_encoder.encode(obj)File"/usr/lib/python3.9/json/encoder.py",line199,inencodechunks=self.iterencode(o,_one_shot=True)File"/usr/lib/python3.9/json/encoder.py",line257,initerencodereturn_iterencode(o,0)File"/usr/lib/python3.9/json/encoder.py",line179,indefaultraiseTypeError(f'Object of type{o.__class__.__name__} 'TypeError:Objectof typebytesisnotJSONserializable

Therefore, it would be useful to add the following indentation feature toreprlib:

>>>importreprlib>>>print(reprlib.repr(example))# regular output[1, [2,'foo',b'bar', {'a':1,'b':'abc def ghi','c': {1:2,3:4,5: [],6: {}}}],3]>>>reprlib.aRepr.indent=4>>>print(reprlib.repr(example))# output with 4 spaces indentation[1,    [2,'foo',b'bar',        {'a':1,'b':'abc def ghi','c': {1:2,3:4,5: [],6: {},            },        },    ],3,]>>>reprlib.aRepr.maxlevel=3>>>print(reprlib.repr(example))# same as above, but with reduced depth[1,    [2,'foo',b'bar',        {'a':1,'b':'abc def ghi','c': {...},        },    ],3,]>>>reprlib.aRepr.indent="........">>>print(reprlib.repr(example))# custom indentation string[........1,........[................2,................'foo',................b'bar',................{........................'a':1,........................'b':'abc def ghi',........................'c': {...},................},........],........3,]

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp