V
Victor Hooi
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/...-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
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/...-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