1

1. Summary

I don't find, how I can automatically wrap long lines in py files. It would be nice, if someone tell me any command line or Sublime Text tool.

At the time I need to wrap long lines manually.

2. Argumentation

PEP8 E501 rule. Even if I set max line length, for example, to 120, some lines exceed the limit.

3. Requirements

  1. Windows 10 64-bit Enterprise LTSB EN,
  2. Python 3.6.3,
  3. Sublime Text, Build 3143.

4. Example

My SashaIsTheWorld.py file:

amazing = True
spectacular = True
excellent = True
breathtaking = True
exciting = True
fantastic = True
unmatched = True
magnificent = True
breakneck = True
fascinating = True

if amazing and spectacular and excellent and breathtaking and exciting and fantastic and unmatched and magnificent and breakneck and fascinating is True:
    print("It about Sasha")
else:
    print("She's the one for me, She's my ecstasy, She's the one I need. She's one in a million, She's once in a lifetime, She made me discover one of the stars above us.")
D:\SashaPythonista>flake8 SashaIsTheWorld.py
SashaIsTheWorld.py:12:121: E501 line too long (153 > 120 characters)
SashaIsTheWorld.py:15:121: E501 line too long (177 > 120 characters)

5. Expected behavior

For example:

amazing = True
spectacular = True
excellent = True
breathtaking = True
exciting = True
fantastic = True
unmatched = True
magnificent = True
breakneck = True
fascinating = True

if amazing and spectacular and excellent and breathtaking and exciting and fantastic \
        and unmatched and magnificent and breakneck and fascinating is True:
    print("It about Sasha")
else:
    print("She's the one for me, She's my ecstasy, She's the one I need. "
          "She's one in a million, She's once in a lifetime, She made me discover one of the stars above us.")

6. Not helped

  1. Google search,
  2. For Python:
    1. autopep8 with --agressive and --experimental,
    2. pyformat.
  3. For Sublime Text:
    1. AutoWrap not have “smart” wrapping. This package not add \ symbol and additional quotes.
    2. Daniel Beck plugin also haven't “smart” wrapping.
    3. Sublime-Wrap-Plus works only for triple quoted strings.
    4. WrapStatement not works for cases in my example.

7. Do not offer

Please, don't tell, that:

  1. I should not have long lines in the code,
  2. I need to wrap all long lines manually,
  3. I need to use ruler. Yes, I use it.
1

Too long for a comment, perhaps not an answer, as it points towards refactoring, rather than a tool to help wrap long lines.

If this is for work, then they should tell you how; if it is for private code, then don't sweat it, just have long lines if you want to.

That being said, long lines are a code smell. Off the opt of my head, I can think of the following reasons for long lines (feel free to add more).

  1. very long strings
  2. deeply nested branching (many ifs)
  3. complicated conditionals (if (A =B) and (V C> 123456) or (dictionary['key'] = 'value') or ....

1) can be handled by manually breaking the long strings, of which I would not expect many.

2 & 3 both suggest refactoring ,and I see from your high rep on (Russian) Stack Overflow, that you ought to know about that :-)

For 1) some coding standards forbid besting conditionals more than X deep (my current company says 4, ymmv). If there are many more, it might mean that the function is trying to do more than one thing - which is bad

For 2 & 3, such complexity is better hidden in a function, and replaced by if ConditionsValid():

In both cases, we end up with (more) smaller functions, which are easier to read, maintain and unit test.

I am guessing that you know all of this, so am writing for other readers of the question.

Nevertheless, my recommendation is not to look for a tool which wraps long lines, but to ask yourself why you have so many longs lines (and to forgive yourself a few, if you can justify it).

You might also want to look (or ask) on Programmers

Please let me know what you think.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.