Sublime Forum

How to beautify python dictionaries in SublimeText plugin?

#1

Could anyone please suggest me a package that can beautify python dictionaries? Ie: input: (python dict) => output (python dict)

I’m not interested on using pprint.pformat as the output is really ugly and I’d like to get the same output than prettyprinter… unfortunately I can’t use prettyprinter on SublimeText as it won’t install with the old python 3.3.5 (ie: i’ll get "colorama requires Python '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*' but the running Python is 3.3.5" error)

I was looking for something that provided a similar output to this https://codebeautify.org/python-formatter-beautifier or this https://www.cleancss.com/python-beautify/

Grrr… being stuck with python3.3.5 is a PITA :confused:

Thanks in advance.

0 Likes

#2

How about posting a dict in the format you’d like to see in this thread, so we can more easily understand what you expect?

Maybe black is to your liking?

1 Like

#3

How about posting a dict in the format you’d like to see in this thread, so we can more easily understand what you expect?

That’s why I’d posted 2 websites that provide exactly the same output I’d like to get.

Maybe black is to your liking?

Yeah, maybe… do you know if black can be used programatically and be used with python3.3.5?

Right now I’m using this little hack:

def python_beautify(self, text, opts):
    out = json.dumps(ast.literal_eval(text), **opts)
    out = out.replace("null", "None")
    out = out.replace("true", "True")
    out = out.replace("false", "False")
    return out

But as you can see… this hack will fail on many non trivial cases as it’s not an actual realy beautifier/parser

0 Likes

#4

… and be used with python3.3.5?

I’ll self-answer myself here, it can’t… https://github.com/psf/black/blob/master/setup.py#L34

0 Likes

#5

For the first one, cloudflare blocked me out, and for the second one I wasn’t exactly interested in generating a nested dict (and maybe also with a list inside) just so I could see an example for what you consider good formating.

It has a CLI, so you can use it with any language as long as you install it onto your system Python (and then use the subprocess module within ST).

0 Likes

#6

I see, I consider a good formating what json.dumps spits out when using indent=4

Yeah, in some of my plugins I’m not afraid of using python external tools like (autopep8, flake, isort) through subprocess. For this particular plugin I’d prefer avoid calling subprocess as I’ll be handling multiple selections per view.

I haven’t found anything suitable for python3.3.5 yet… there was this one called python-tidy which was 2.5 where i used 2to3 but it’s using compiler module… not sure if porting that to 3.3.5 would take me much effort :confused:

Summing up, if anyone knows a suitable module to beautify python code with the good old python3.3.5 pls let me know, thanks!

0 Likes