Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle long path name for pep8 compliance?

Tags:

python

pep8

How would I handle long path name like below for pep8 compliance? Is 79 characters per line a must even if it becomes somewhat unreadable?

def setUp(self):
    self.patcher1 = patch('projectname.common.credential.CredentialCache.mymethodname')                 
like image 362
James C. Avatar asked May 06 '15 13:05

James C.


3 Answers

There are multiple ways to do this:

  1. Use a variable to store this

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

    An assignment like v = ("a" "b" "c") gets converted into v = "abc":

    def setUp(self):
        self.patcher1 = patch(
            "projectname.common.credential."
            "CredentialCache.mymethodname")
    
  3. Tell pep8 that we don't use 80-column terminals anymore with --max-line-length=100 (or some sufficiently reasonable value). (Hat Tip @chepner below :) )

like image 195
Anshul Goyal Avatar answered Sep 29 '22 00:09

Anshul Goyal


The 79-character limit in PEP8 is based more on historical beliefs than in actual readability. All of PEP8 is a guideline, but this one is more frequently ignored than most of the recommendation. The pep8 tool even has a specific option for changing the value of what is considered "too long".

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

I frequently just disable the test altogether:

pep8 --ignore E501 myscript.py
like image 21
chepner Avatar answered Sep 29 '22 02:09

chepner


I prefer variant with concatenation.

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

Also concatenation braces are not required when calling a function.

like image 26
Pavel Patrin Avatar answered Sep 29 '22 00:09

Pavel Patrin