Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make PyDev autoformat the maximum number of characters per line?

I am using PyDev for Python programming in Eclipse.

Is it possible to make PyDev auto-format my code to limit the maximum number of characters on a single line?

like image 616
rauldg Avatar asked Mar 28 '13 10:03

rauldg


People also ask

How to reformat with Black python?

To get started right away with sensible defaults, choose the python file you want to format and then write black filename.py in the terminal, then the black will format your python file. Let's look at this simple example, here is my two python functions in my python file called sample_code.py.

How to format the python code?

You can set your formatting options through the menus Tools > Options > Text Editor > Python > Formatting and its nested tabs. Formatting options by default are set to match a superset of the PEP 8 style guide.

How to run Black on code?

Black can be installed by running pip install black . It requires Python 3.7+ to run. If you want to format Jupyter Notebooks, install with pip install 'black[jupyter]' .

What does Black do in python?

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.


1 Answers

With standard PyDev

It seems that this option is only available in Eclipse's Java Editor.

The Java editor allows you to create "profiles" for the code formatter, while PyDev's options for the code formatter are very limited.

However,

You can hack this. PythonTidy.py is an awesome script that cleans up Python code to make it follow PEP8 conventions, and that can be tweaked with your own settings.

PythonTidy (code cleanup & formatting)

Get here (homepage) the source for PythonTidy.

You will see inside the file, at the beginning of the code and just after the comments, that many settings are defined.

The first one of these is COL_LIMIT with its default value set to 72. Now you can use PythonTidy to format your code the way you want.

Integration with PyDev

Now you have to make this work with PyDev's formatting. This blog post will explain it really better than me, but still I'll sum up the steps :

  • Write a Jython interface betwenn PyDev's editor (PyEdit) and PythonTidy. This blog's author already wrote a wrapper script in the public domain available in the above link or here in case the link goes 404.
  • Save this file anywhere you want, with the name pyedit_pythontidy.py, along with the PythonTidy.py file.
  • Configure PyDev to use this script as its Code Formatter. You can do this in Preferences > PyDev > Scripting PyDev

    • Note #1: I really recommend reading the original blog post to have a better understanding

    • Note #2: The wrapper script author did not implement Code Block formatting, so this means you can only format a full file. This should not be that hard to implement, up to you.

    • Note #3: All credits goes to bear330 for the PyDev integration part.

like image 72
pistache Avatar answered Oct 01 '22 02:10

pistache