I have this line of code which goes over the line and when testing for pep8 errors I get:
line too long. So to try and fix this I used slash('\') but then I get continuation line over-indented for visual indent. What can I do to fix this?
Things I've tried:
if first_index < 0 or second_index > \
self._number_of_plates - 1:
raise ValueError
continuation line over-indented for visual indent
if first_index < 0 \
or second_index > \
self._number_of_plates - 1:
raise ValueError
continuation line over-indented for visual indent
if first_index < 0 or \
second_index > self._number_of_plates - 1:
raise ValueError
continuation line over-indented for visual indent
if first_index \
< 0 or second_index \
> self._number_of_plates - 1:
raise ValueError
continuation line over-indented for visual indent
The line-extending backslash has the issue of having trailing whitespace that can break your code. This is a popular fix and is PEP8-compliant:
if (first_index < 0 or
second_index > self._number_of_plates - 1):
A continuation line is indented farther than it should be for a visual indent.
Anti-pattern
In this example, the string "World" is indented two spaces farther than it should be.
print("Python", ("Hello",
"World"))
Best practice
print("Python", ("Hello",
"World"))
reference: https://www.flake8rules.com/rules/E127.html
For lines that are too long (e.g. > 79 characters), you can use parentheses to group your conditions:
if (first_index < 0
or second_index > self._number_of_plates - 1
or condition2
and candition3):
raise ValueError
Note that any boolean conditions (or, and) should go at the start of the line before the condition.
In your case, there is a special rule because of the if (...) construct:
When the conditional part of an if-statement is long enough to require
that it be written across multiple lines, it's worth noting that the
combination of a two character keyword (i.e. if), plus a single space,
plus an opening parenthesis creates a natural 4-space indent for the
subsequent lines of the multiline conditional. This can produce a
visual conflict with the indented suite of code nested inside the
if-statement, which would also naturally be indented to 4 spaces. This
PEP takes no explicit position on how (or whether) to further visually
distinguish such conditional lines from the nested suite inside the
if-statement. Acceptable options in this situation include, but are
not limited to:
# No extra indentation.
if (this_is_one_thing and
that_is_another_thing):
do_something()
# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
that_is_another_thing):
# Since both conditions are true, we can frobnicate.
do_something()
# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
and that_is_another_thing):
do_something()
(Also see the discussion of whether to break before or after binary
operators below.)
Source: PEP 8 Style Guide
This is what the popular black formatter does:
if (
first_index < 0
or second_index > self._number_of_plates - 1
):
raise ValueError
This looks fine to me. However, it is probably a good idea to refactor so that the code isn't 7 levels of indentation deep in the first place!
Related
Usually I can make comment with # or """ for multiline comments. But in the following cases,
if i > 0:
if (df.loc[i, 'data'] <= level1) and \ # Comment
(df.loc[i - 1, 'data'] > level1) and \ # Comment
not ideal_state:
ideal_state_time = df.loc[i,'data']
ideal_state = True
I got the error
File "<ipython-input-24-07959bc4f436>", line 121
if (df.loc[i, 'data'] <= level1) and \ # Comment
^
SyntaxError: unexpected character after line continuation character
What is going on? What's wrong with commenting after the slash? I put the slash there because otherwise it will return an error.
You can try replacing \ (back-slashes) with ()(brackets) as shown below
if( (df.loc[i, 'data'] <= level1) and # Comment
(df.loc[i - 1, 'data'] > level1) and # Comment
not ideal_state
):
ideal_state_time = df.loc[i,'data']
ideal_state = True
You can see in PEP8, it's recommended to use brackets
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read())```
The backslash allows your if to span multiple lines, as it says something like "ignore the upcoming char (newline)"
your interpreter reads it like this:
if (df.loc[i, 'data'] <= level1) and # Comment (df.loc[i - 1, 'data'] > lev...
And then your interpreter is right, your comment sign does not belong there.
Line continuations may never carry comments.
You're allowed to comment again after not ideal_state:
the backslash "\" is the line continuation character. i.e
print "massive super long string that doesn't fit" + \
"on a single line"
only newline charecters/whitespace are allowed after it.
I am trying to find the positions of patterns in a file, and I am using Python regex. When I run the below code, several start positions are printed but only one end position, the one corresponding to the latest start position, is printed. The bottom print statement is also only printed once. Why is there not a match.end() value for every match.start() value? File is a .obj file.
import re
import binascii
def findByte(b,file):
f = open(file, "rb").read()
f = binascii.hexlify(f)
regex = b + "(?=(?:[\\da-fA-F]{2})*$)"
for match in (re.finditer(regex, f)):
s = match.start()
print("S" + str(s))
e = match.end()
print("E" + str(e))
g = match.group()
print(g)
print ('String match "%s" at %d:%d' % (g, s, e))
findByte("ca","demo.obj")
When I run it, the below answers are printed.
S0
S64
S184
S252
E254
ca
String match "ca" at 252:254
If I instead write
def findByte(b,file):
f = open(file, "rb").read()
f = binascii.hexlify(f)
regex = b + "(?=(?:[\\da-fA-F]{2})*$)"
m = re.findall(regex,f)
print(m)
findByte("ca","demo.obj")
The printed value is
['ca', 'ca', 'ca', 'ca']
Checking the actual code you pasted, it's clear you've committed the cardinal Python sin of mixing tabs and spaces, and it's biting you (try selecting the leading whitespace in your own code on this page; you'll see some of it is selected as blocks of four spaces at a time, while other parts have single space granularity). Your editor is showing you tabs as being equivalent to four spaces, but in the code you pasted, you have purely tab based indentation up through print("S" + str(s)), then four spaces followed by a tab as the indentation for subsequent lines.
Most such mixed indentation stuff is rejected by Python 3, but Python 2 is more flexible (it gives you the rope to hang yourself), which may be what is happening here (Python 2 is end of life as of the beginning of this year, so if you're writing new code, I'd suggest switching for this and many other reasons). Your code looks like it's all in the for loop, but most of it isn't.
Replace all your tabs with four space indents, and reconfigure your editor to always expand tabs to spaces in the future, so you aren't bit by this in the future; Python style is consistent four space indents with no tabs for a reason.
Given:
e = 'a' + 'b' + 'c' + 'd'
How do I write the above in two lines?
e = 'a' + 'b' +
'c' + 'd'
What is the line? You can just have arguments on the next line without any problems:
a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5,
blahblah6, blahblah7)
Otherwise you can do something like this:
if (a == True and
b == False):
or with explicit line break:
if a == True and \
b == False:
Check the style guide for more information.
Using parentheses, your example can be written over multiple lines:
a = ('1' + '2' + '3' +
'4' + '5')
The same effect can be obtained using explicit line break:
a = '1' + '2' + '3' + \
'4' + '5'
Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.
From PEP 8 -- Style Guide for Python Code:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read())
Another such case is with assert statements.
Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. Some examples:
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)file_2.write(file_1.read())
PEP8 now recommends the opposite convention (for breaking at binary operations) used by mathematicians and their publishers to improve readability.
Donald Knuth's style of breaking before a binary operator aligns operators vertically, thus reducing the eye's workload when determining which items are added and subtracted.
From PEP8: Should a line break before or after a binary operator?:
Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"[3].
Following the tradition from mathematics usually results in more readable code:
# Yes: easy to match operators with operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested.
[3]: Donald Knuth's The TeXBook, pages 195 and 196
The danger in using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was.
See Python Idioms and Anti-Idioms (for Python 2 or Python 3) for more.
Put a \ at the end of your line or enclose the statement in parens ( .. ). From IBM:
b = ((i1 < 20) and
(i2 < 30) and
(i3 < 40))
or
b = (i1 < 20) and \
(i2 < 30) and \
(i3 < 40)
You can break lines in between parenthesises and braces. Additionally, you can append the backslash character \ to a line to explicitly break it:
x = (tuples_first_value,
second_value)
y = 1 + \
2
From the horse's mouth: Explicit line
joining
Two or more physical lines may be
joined into logical lines using
backslash characters (\), as follows:
when a physical line ends in a
backslash that is not part of a string
literal or comment, it is joined with
the following forming a single logical
line, deleting the backslash and the
following end-of-line character. For
example:
if 1900 < year < 2100 and 1 <= month <= 12 \
and 1 <= day <= 31 and 0 <= hour < 24 \
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
return 1
A line ending in a backslash cannot
carry a comment. A backslash does not
continue a comment. A backslash does
not continue a token except for string
literals (i.e., tokens other than
string literals cannot be split across
physical lines using a backslash). A
backslash is illegal elsewhere on a
line outside a string literal.
If you want to break your line because of a long literal string, you can break that string into pieces:
long_string = "a very long string"
print("a very long string")
will be replaced by
long_string = (
"a "
"very "
"long "
"string"
)
print(
"a "
"very "
"long "
"string"
)
Output for both print statements:
a very long string
Notice the parenthesis in the affectation.
Notice also that breaking literal strings into pieces allows to use the literal prefix only on parts of the string and mix the delimiters:
s = (
'''2+2='''
f"{2+2}"
)
One can also break the call of methods (obj.method()) in multiple lines.
Enclose the command in parenthesis "()" and span multiple lines:
> res = (some_object
.apply(args)
.filter()
.values)
For instance, I find it useful on chain calling Pandas/Holoviews objects methods.
It may not be the Pythonic way, but I generally use a list with the join function for writing a long string, like SQL queries:
query = " ".join([
'SELECT * FROM "TableName"',
'WHERE "SomeColumn1"=VALUE',
'ORDER BY "SomeColumn2"',
'LIMIT 5;'
])
Taken from The Hitchhiker's Guide to Python (Line Continuation):
When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.
A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behaviour holds for curly and square braces.
However, more often than not, having to split a long logical line is a sign that you are trying to do too many things at the same time, which may hinder readability.
Having that said, here's an example considering multiple imports (when exceeding line limits, defined on PEP-8), also applied to strings in general:
from app import (
app, abort, make_response, redirect, render_template, request, session
)
The following line of code outputs SyntaxError: invalid syntax
for (i in range(-WIDTH,WIDTH)):
The next one works without errors. I have no idea what the syntax error is supposed to be here. So I am just asking out of curiosity. My guess is that the brackets prevent the expression from being evaluated.
for i in range(-WIDTH,WIDTH):
Your parentheses are essentially just confusing the parser.
There are a couple of reasons you could have an open paren after a for, most notably using tuple unpacking:
>>> for (x, y) in zip(range(5), range(6, 11)):
... print(x, '->', y)
...
0 -> 6
1 -> 7
2 -> 8
3 -> 9
4 -> 10
Additionally, parens can be used in loads of places in Python for simple grouping, such as when breaking up long lines:
>>> s = ("This is "
... "a really awkward way "
... "to write a "
... "long string "
... "over several lines")
>>>
>>> s
'This is a really awkward way to write a long string over several lines'
So the parser won't really complain about it.
However, as you know, for is supposed to read like this:
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
Which means that by grouping this way, you're constructing an invalid loop. Essentially, yours reads that there is no in because it's grouped into the target_list by your parentheses. Hope this makes sense.
A way to see more clearly what's happening: write the rest of your for loop (in expression_list) after your close paren. Then you will get a clearer error about how it is interpreting this statement.
>>> for (i in range(-WIDTH, WIDTH)) in range(-WIDTH, WIDTH):
... print(i)
...
File "<stdin>", line 1
SyntaxError: can't assign to comparison
So it will let you do it, but the result of x in y will be a boolean, which cannot be the target of an assignment. The original error you got is because it got to your : before it found your in, which is plain old invalid syntax, as if you just wrote for x:.
Given:
e = 'a' + 'b' + 'c' + 'd'
How do I write the above in two lines?
e = 'a' + 'b' +
'c' + 'd'
What is the line? You can just have arguments on the next line without any problems:
a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5,
blahblah6, blahblah7)
Otherwise you can do something like this:
if (a == True and
b == False):
or with explicit line break:
if a == True and \
b == False:
Check the style guide for more information.
Using parentheses, your example can be written over multiple lines:
a = ('1' + '2' + '3' +
'4' + '5')
The same effect can be obtained using explicit line break:
a = '1' + '2' + '3' + \
'4' + '5'
Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.
From PEP 8 -- Style Guide for Python Code:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read())
Another such case is with assert statements.
Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. Some examples:
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)file_2.write(file_1.read())
PEP8 now recommends the opposite convention (for breaking at binary operations) used by mathematicians and their publishers to improve readability.
Donald Knuth's style of breaking before a binary operator aligns operators vertically, thus reducing the eye's workload when determining which items are added and subtracted.
From PEP8: Should a line break before or after a binary operator?:
Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"[3].
Following the tradition from mathematics usually results in more readable code:
# Yes: easy to match operators with operands
income = (gross_wages
+ taxable_interest
+ (dividends - qualified_dividends)
- ira_deduction
- student_loan_interest)
In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested.
[3]: Donald Knuth's The TeXBook, pages 195 and 196
The danger in using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was.
See Python Idioms and Anti-Idioms (for Python 2 or Python 3) for more.
Put a \ at the end of your line or enclose the statement in parens ( .. ). From IBM:
b = ((i1 < 20) and
(i2 < 30) and
(i3 < 40))
or
b = (i1 < 20) and \
(i2 < 30) and \
(i3 < 40)
You can break lines in between parenthesises and braces. Additionally, you can append the backslash character \ to a line to explicitly break it:
x = (tuples_first_value,
second_value)
y = 1 + \
2
From the horse's mouth: Explicit line
joining
Two or more physical lines may be
joined into logical lines using
backslash characters (\), as follows:
when a physical line ends in a
backslash that is not part of a string
literal or comment, it is joined with
the following forming a single logical
line, deleting the backslash and the
following end-of-line character. For
example:
if 1900 < year < 2100 and 1 <= month <= 12 \
and 1 <= day <= 31 and 0 <= hour < 24 \
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
return 1
A line ending in a backslash cannot
carry a comment. A backslash does not
continue a comment. A backslash does
not continue a token except for string
literals (i.e., tokens other than
string literals cannot be split across
physical lines using a backslash). A
backslash is illegal elsewhere on a
line outside a string literal.
If you want to break your line because of a long literal string, you can break that string into pieces:
long_string = "a very long string"
print("a very long string")
will be replaced by
long_string = (
"a "
"very "
"long "
"string"
)
print(
"a "
"very "
"long "
"string"
)
Output for both print statements:
a very long string
Notice the parenthesis in the affectation.
Notice also that breaking literal strings into pieces allows to use the literal prefix only on parts of the string and mix the delimiters:
s = (
'''2+2='''
f"{2+2}"
)
One can also break the call of methods (obj.method()) in multiple lines.
Enclose the command in parenthesis "()" and span multiple lines:
> res = (some_object
.apply(args)
.filter()
.values)
For instance, I find it useful on chain calling Pandas/Holoviews objects methods.
It may not be the Pythonic way, but I generally use a list with the join function for writing a long string, like SQL queries:
query = " ".join([
'SELECT * FROM "TableName"',
'WHERE "SomeColumn1"=VALUE',
'ORDER BY "SomeColumn2"',
'LIMIT 5;'
])
Taken from The Hitchhiker's Guide to Python (Line Continuation):
When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.
A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behaviour holds for curly and square braces.
However, more often than not, having to split a long logical line is a sign that you are trying to do too many things at the same time, which may hinder readability.
Having that said, here's an example considering multiple imports (when exceeding line limits, defined on PEP-8), also applied to strings in general:
from app import (
app, abort, make_response, redirect, render_template, request, session
)