Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Add pretty-print to __repr__ #3

Closed
fmassa opened this issue Sep 14, 2018 · 1 comment
Closed

[Feature request] Add pretty-print to __repr__ #3

fmassa opened this issue Sep 14, 2018 · 1 comment

Comments

@fmassa
Copy link

fmassa commented Sep 14, 2018

I think it would be nice to have __repr__ of CfgNode have a prettier print, which has one field per line with proper indentation. It currently prints everything in a single line (as is the default for dict), and makes searching for specific arguments a bit difficult.
While it's possible to use external libraries like pprint to achieve this, I think that having it built-in would be valuable.

A possible draft implementation:

def _addindent(s_, numSpaces):
    s = s_.split("\n")
    # don't do anything for single-line stuff
    if len(s) == 1:
        return s_
    first = s.pop(0)
    s = [(numSpaces * " ") + line for line in s]
    s = "\n".join(s)
    s = first + "\n" + s
    return s

class CfgNode(dict):
   ...
    def __repr__(self):
        r = object.__repr__(self)
        attrs = {k: v for k, v in self.items()}
        if attrs:
            r += ": \n"
            s = []
            for k, v in attrs.items():
                attr_str = "{}: {}".format(repr(k), repr(v))
                attr_str = _addindent(attr_str, 2)
                s.append(attr_str)
            r += "  " + "\n  ".join(s)
        return r
@rbgirshick
Copy link
Owner

This seems like a reasonable request. I'll implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants