How to handle a long path name to match pep8?

How do I handle a long path name as shown below to match pep8? Is 79 characters on line a mandatory, even if it becomes somewhat unreadable?

def setUp(self): self.patcher1 = patch('projectname.common.credential.CredentialCache.mymethodname') 
+5
source share
4 answers

There are several ways to do this:

  • Use a variable to store this

     def setUp(self): path = 'projectname.common.credential.CredentialCache.mymethodname' self.patcher1 = patch(path) 
  • String concatenation:

    An assignment of type v = ("a" "b" "c") converted to v = "abc" :

     def setUp(self): self.patcher1 = patch( "projectname.common.credential." "CredentialCache.mymethodname") 
  • Tell pep8 that we no longer use 80-column terminals with -max-line-length = 100 (or some reasonably reasonable value). (@Chepner hat tip below :))

+7
source

The 79 character limit in PEP8 is based on historical beliefs rather than real readability. All PEP8 is a guideline, but this one is more often ignored than most recommendations. The pep8 tool even has a specific option to change the value of what is considered too long.

 pep8 --max-line-length 100 myscript.py 

I often just turned off validation completely:

 pep8 --ignore E501 myscript.py 
+2
source

I prefer the concatenation option.

 def setUp(self): self.patcher1 = patch( "projectname.common.credential." "CredentialCache.mymethodname") 

Also, when calling a function, concatenation brackets are not required.

+1
source

The 80-column guide is intended not only for people coding on the 1980 barkley unix terminals, but also ensures consistency in projects. Because of this, you can customize the GUI of your IDE as you see fit and make sure that this is good for all of your various projects.

Alas, sometimes the best solution is to break it, this is an extremely rare case, but it will certainly happen. And jusst for this reason, you can mark this line with a comment: # noinspection PyPep8 so you can turn your code into:

 def setUp(self): # noinspection PyPep8 self.patcher1 = patch('projectname.common.credential.CredentialCache.mymethodname') 

This will allow you to follow the recommendations of pep8, including line restrictions, and not worry about this false message. Unfortunately, this directive is not supported by all checkers, but it slowly gets there.

+1
source

All Articles