Automatically indent python code

2,533 views
Skip to first unread message

Claudio Pedrazzi

unread,
Dec 25, 2023, 12:05:52 PM12/25/23
to BBEdit Talk
Hi everyone,
this is my first post here and I hope I am in the right place. I have a python code of about a thousand lines, contained in a .py file, and I would like to fix, possibly automatically, the indentation (i.e. at the first level four characters, then eight, and so on, as the standard requires).
To explain myself better, the code is correct and it works, but some parts are indented 2 characters, some 3, and some 4. I am trying to do this with BBedit, of which I have the pro version (the paid one). I don't know if this is the right way but I use Markup > Utilities > Format and I always get an error: Unespected EOF; Document ended in the middle of a tag. Maybe I'm not using the right function? Are there ways to get this result?  
If not, are there other free tools to perform this task?
I have to say that I am not very proficient with scripting and such extensions, I would like a simple "menu based" solution, of course if it exists.

Thanks a lots in advance
Best regards
Claudio


jj

unread,
Dec 25, 2023, 3:09:05 PM12/25/23
to BBEdit Talk
Claudio,

You should take a look at :
HTH

Jean Jourdain

Claudio Pedrazzi

unread,
Dec 26, 2023, 8:30:07 AM12/26/23
to BBEdit Talk
Hi Jean

thanks a lot for the reply!

So can I infer from your answer, that BBEdit is not able to do the requested task, even in the paid (pro) version?

Claudio

jj

unread,
Dec 27, 2023, 10:23:25 AM12/27/23
to BBEdit Talk
Hi Claudio,

You should never infer that something cannot be done with BBEdit.
BBEdit has an incredibly useful toolset for editing text and code.
So there is certainly a way with regular expressions to do what you want. 

But ... reformatting Python code is not as simple as it sounds because of indentation,
line width wrapping, etc.

The recommended Black Python formatter is a tool that addresses those difficulties.

You can install it on you system with Homebrew.

Once Homebrew is installed, use it in the terminal to install black:

    % brew install black
   
Once black is installed, you can use it from the terminal:
    
    % black /path/to/your/python_file.py
  
Or you can also create a BBEdit Text Filter that will allow you to use it from BBEdit.

Create a file with this content at this location ~/Library/Application Support/BBEdit/Text Filters/black.sh.

#!/usr/bin/env zsh

black -q -

Give the black.sh shell script the executable permissions.
You should now have in BBEdit a menu Text > Apply Text Filter > black you can use to reformat you python files.

If you need help on black, in the terminal type:

    % black --help

HTH

Jean Jourdain

Claudio Pedrazzi

unread,
Dec 27, 2023, 1:22:00 PM12/27/23
to bbe...@googlegroups.com
Hi Jean
I sincerely thank you for your rather comprehensive and instructive reply.  I installed black (it also installs with pip3 install black) and tried it on my code.  It does much more than just fix the indentation! I don't always agree with the changes.  Also I do not like that it overwrites the original source file.  However, this has nothing to do with my question, which I consider adequately answered.  I now understand that indenting Python code is not as "straightforward" as I thought.
Thanks again for the clarification
Claudio Pedrazzi

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/e79ac135-dfd9-41d7-a314-0ccda42b49c7n%40googlegroups.com.

Todd Ingalls

unread,
Dec 27, 2023, 2:11:06 PM12/27/23
to BBEdit Talk
Hi Claudio
Just to note there are other python formatters available,  for instance yapf https://pypi.org/project/yapf/ which you mind find of value. It can be used in BBedit in a similar way

Rick Yentzer

unread,
Jan 8, 2024, 7:50:05 AM1/8/24
to BBEdit Talk
Hi Claudio,
I mainly use Black for Python formatting as well. 
They have nice documentation that shows how to customize its output. 
That may get you closer to what you want. https://black.readthedocs.io/en/stable/index.html
Reply all
Reply to author
Forward
0 new messages