Python and PEP8 - Recommendations on breaking up long lines?

Victor Hooi victorhooi at gmail.com
Wed Nov 27 21:03:29 EST 2013


Hi,

Also, forgot two other examples that are causing me grief:

    cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
                    [[os.path.relpath(filename, foobar_input_folder)] for filename in filenames])

I've already broken it up using the parentheses, not sure what's the tidy way to break it up again to fit under 80? In this case, the 80-character mark is hitting me around the "for filename" towards the end.

and:

                    if os.path.join(root, file) not in previously_processed_files and os.path.join(root, file)[:-3] not in previously_processed_files:

In this case, the 80-character mark is actually partway through "previously processed files" (the first occurrence)...

Cheers,
Victor

On Thursday, 28 November 2013 12:57:13 UTC+11, Victor Hooi  wrote:
> Hi,
> 
> 
> 
> I'm running pep8 across my code, and getting warnings about my long lines (> 80 characters).
> 
> 
> 
> I'm wonder what's the recommended way to handle the below cases, and fit under 80 characters.
> 
> 
> 
> First example - multiple context handlers:
> 
> 
> 
>             with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as output:
> 
> 
> 
> and in my case, with indents, the 80-character marks is just before the ending "as output".
> 
> 
> 
> What's the standard recognised way to split this across multiple lines, so that I'm under 80 characters?
> 
> 
> 
> I can't just split after the "as input," as that isn't valid syntax, and there's no convenient parentheses for me to split over.
> 
> 
> 
> Is there a standard Pythonic way?
> 
> 
> 
> Second example - long error messages:
> 
> 
> 
>             self.logger.error('Unable to open input or output file - %s. Please check you have sufficient permissions and the file and parent directory exist.' % e)
> 
> 
> 
> I can use triple quotes:
> 
> 
> 
>             self.logger.error(
> 
>                 """Unable to open input or output file - %s. Please check you
> 
>                 have sufficient permissions and the file and parent directory
> 
>                 exist.""" % e)
> 
> 
> 
> However, that will introduce newlines in the message, which I don't want.
> 
> 
> 
> I can use backslashes:
> 
> 
> 
>             self.logger.error(
> 
>                 'Unable to open input or output file - %s. Please check you\
> 
>                 have sufficient permissions and the file and parent directory\
> 
>                 exist.' % e)
> 
> 
> 
> which won't introduce newlines.
> 
> 
> 
> Or I can put them all as separate strings, and trust Python to glue them together:
> 
> 
> 
>             self.logger.error(
> 
>                 'Unable to open input or output file - %s. Please check you'
> 
>                 'have sufficient permissions and the file and parent directory'
> 
>                 'exist.' % e)
> 
> 
> 
> Which way is the recommended Pythonic way?
> 
> 
> 
> Third example - long comments:
> 
> 
> 
>             """ NB - We can't use Psycopg2's parametised statements here, as
> 
>             that automatically wraps everything in single quotes.
> 
>             So s3://my_bucket/my_file.csv.gz would become s3://'my_bucket'/'my_file.csv.gz'.
> 
>             Hence, we use Python's normal string formating - this could
> 
>             potentially exposes us to SQL injection attacks via the config.yaml
> 
>             file.
> 
>             I'm not aware of any easy ways around this currently though - I'm
> 
>             open to suggestions though.
> 
>             See
> 
>             http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
> 
>             for further information. """
> 
> 
> 
> In this case, I'm guessing a using triple quotes (""") is a better idea with multi-line comments, right?
> 
> 
> 
> However, I've noticed that I can't seem to put in line-breaks inside the comment without triggering a warning. For example, trying to put in another empty line in between lines 6 and 7 above causes a warning.
> 
> 
> 
> Also, how would I split up the long URLs? Breaking it up makes it annoying to use the URL. Thoughts?
> 
> 
> 
> Cheers,
> 
> Victor



More information about the Python-list mailing list