Thomas Gratier Thomas Gratier - 5 months ago 17
Python Question

Automatic correction of python sphinx line length

Usually, best practices related to sphinx says that you must limit the line length to less than 80 characters.

So I want a way to automatically limit my line length to less than 80 characters (with no hyphens) and when the correction happens, it has to put the correct indent (if possible).
The only function I've seen who seems to fit a bit is the Textwrap http://docs.python.org/library/textwrap.html
Someone has some experiences with this? a better idea maybe? The function is already bundle in sphinx but I don't find it ? I miss a topic in stackoverflow?

Any input is welcome.

Answer

A descent editor is what you need.

In vim for example this is handled by doing:

:set textwidth=80

Then all you have to do is start typing and vim will continue on the nextline.

Marking text and doing a gq reformats the selection according to the setting of the "textwidth" variable.

Ex.

In those days spirits were brave, the stakes were high, men were real men, women were real women and small furry creatures from Alpha Centauri were real small furry creatures from Alpha Centauri.

In vim after setting tw=30, selecting text and doing a gq:

In those days spirits were
brave, the stakes were high,
men were real men, women were
real women and small furry
creatures from Alpha Centauri
were real small furry
creatures from Alpha Centauri.