Re: [portland] PEP 8 Line Length Compliance

2015-05-21 Thread Luciano Ramalho
Of course in the first syntax rule I mention I should have said:

Whenever you open a parenthesis, brace or bracket, every newline is
considered as plain space by the parser UNTIL YOU CLOSE THE
PARENTHESIS, BRACE OR BRACKET.

Which is rather obvious...

Best,

Luciano

On Thu, May 21, 2015 at 12:13 PM, Luciano Ramalho luci...@ramalho.org wrote:
 You can escape the newline and the end of a line by typing \ as the
 last character, but I think this is ugly and error prone and I avoid
 it whenever I can -- which is almost always.

 There are two syntax rules of Python that are not so well-known that
 help avoiding the \:

 - Whenever you open a parenthesis, brace or bracket, every newline is
 considered as plain space by the parser. Very often, adding
 parenthesis around an expression does not change it's meaning, so
 that's one way to make one logical line span multiple physical lines
 without resorting to \.

 - Two or more string literals with no intervening tokens except
 whitespace are parsed as a single string literal.

 Putting those two rules together, the result is that this snippet:

 text = ('a bb ccc  e '
'ff ggg ')

 Is exactly the same as this one, as far as the Python interpreter is 
 concerned:

 text = 'a bb ccc  e ff ggg hh'

 But note that I appended a trailing space to the first string literal,
 otherwise the parser would see this:

 text = 'a bb ccc  eff ggg hh'


 HTH.

 Best,

 Luciano



 On Thu, May 21, 2015 at 11:53 AM, Rich Shepard rshep...@appl-ecosys.com 
 wrote:
   I use emacs for writing code (among other editing tasks) and have the line
 length set to 78 characters. That's the recommended maximum line length in
 PEP 8. However, when I have a lot of text for an import command, or within a
 single-quoted string, python complains about unexpected endings if I break
 the string with a newline prior to column 78.

   Should I just ignore that PEP 8 suggestion or is there a line continuation
 character (such as \) that can be applied?

 Rich
 ___
 Portland mailing list
 Portland@python.org
 https://mail.python.org/mailman/listinfo/portland



 --
 Luciano Ramalho
 |  Author of Fluent Python (O'Reilly, 2015)
 | http://shop.oreilly.com/product/0636920032519.do
 |  Professor em: http://python.pro.br
 |  Twitter: @ramalhoorg



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg
___
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland


Re: [portland] PEP 8 Line Length Compliance

2015-05-21 Thread Luciano Ramalho
You can escape the newline and the end of a line by typing \ as the
last character, but I think this is ugly and error prone and I avoid
it whenever I can -- which is almost always.

There are two syntax rules of Python that are not so well-known that
help avoiding the \:

- Whenever you open a parenthesis, brace or bracket, every newline is
considered as plain space by the parser. Very often, adding
parenthesis around an expression does not change it's meaning, so
that's one way to make one logical line span multiple physical lines
without resorting to \.

- Two or more string literals with no intervening tokens except
whitespace are parsed as a single string literal.

Putting those two rules together, the result is that this snippet:

text = ('a bb ccc  e '
   'ff ggg ')

Is exactly the same as this one, as far as the Python interpreter is concerned:

text = 'a bb ccc  e ff ggg hh'

But note that I appended a trailing space to the first string literal,
otherwise the parser would see this:

text = 'a bb ccc  eff ggg hh'


HTH.

Best,

Luciano



On Thu, May 21, 2015 at 11:53 AM, Rich Shepard rshep...@appl-ecosys.com wrote:
   I use emacs for writing code (among other editing tasks) and have the line
 length set to 78 characters. That's the recommended maximum line length in
 PEP 8. However, when I have a lot of text for an import command, or within a
 single-quoted string, python complains about unexpected endings if I break
 the string with a newline prior to column 78.

   Should I just ignore that PEP 8 suggestion or is there a line continuation
 character (such as \) that can be applied?

 Rich
 ___
 Portland mailing list
 Portland@python.org
 https://mail.python.org/mailman/listinfo/portland



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg
___
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland


Re: [portland] PEP 8 Line Length Compliance

2015-05-21 Thread Rich Shepard

On Thu, 21 May 2015, Luciano Ramalho wrote:


You can escape the newline and the end of a line by typing \ as the
last character, but I think this is ugly and error prone and I avoid
it whenever I can -- which is almost always.


Luciano,

  Thanks. That's what I commonly use, but haven't applied to python.


- Whenever you open a parenthesis, brace or bracket, every newline is
considered as plain space by the parser. Very often, adding parenthesis
around an expression does not change it's meaning, so that's one way to
make one logical line span multiple physical lines without resorting to
\.


  This is how it works with long wxPython widget strings. I did not make the
connection to enclosing characters.


- Two or more string literals with no intervening tokens except
whitespace are parsed as a single string literal.


  Unless the string is delineated with single quotes (as in a content list
for a drop-down picklist control). :-)

  I'm used to seeing (and writing) newlines in bash scripts and other text
so now I know python recognizes it I can use this solution.

Much appreciated,

Rich
___
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland


Re: [portland] PEP 8 Line Length Compliance

2015-05-21 Thread Luciano Ramalho
On Thu, May 21, 2015 at 12:24 PM, Rich Shepard rshep...@appl-ecosys.com wrote:
 On Thu, 21 May 2015, Luciano Ramalho wrote:
 - Two or more string literals with no intervening tokens except
 whitespace are parsed as a single string literal.


   Unless the string is delineated with single quotes (as in a content list
 for a drop-down picklist control). :-)

The syntax I described works for any of the valid string delimiters in
Python: single quotes, double quotes, triple single quotes and triple
double quotes, and the adjacent string literals do not need to use the
same delimiter, you can mix and match and they'll still be parsed as
one big string literal.

   I'm used to seeing (and writing) newlines in bash scripts and other text
 so now I know python recognizes it I can use this solution.

The problem with the trailing \ is if someone accidentally types a
space after it, the newline is no longer escaped, and most editors
don't highlight this error, so you get a confusing message from the
interpreter.

Best,

Luciano



 Much appreciated,


 Rich
 ___
 Portland mailing list
 Portland@python.org
 https://mail.python.org/mailman/listinfo/portland



-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg
___
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland


Re: [portland] PEP 8 Line Length Compliance

2015-05-21 Thread Robert Lugg
I prefer Luciano's suggestions.  That said, another way is to build up the 
string from a list and then join the list elements.  For your case this is 
overkill:

l_res = list()
l_res.append('a bb ccc  e ')
l_res.append('ff ggg ')
res = ''.join(l_res)

Robert

-Original Message-
From: Portland [mailto:portland-bounces+robert.lugg=synopsys@python.org] On 
Behalf Of Luciano Ramalho
Sent: Thursday, May 21, 2015 8:42 AM
To: Python Users Group -- Portland, Oregon USA
Subject: Re: [portland] PEP 8 Line Length Compliance

On Thu, May 21, 2015 at 12:24 PM, Rich Shepard rshep...@appl-ecosys.com wrote:
 On Thu, 21 May 2015, Luciano Ramalho wrote:
 - Two or more string literals with no intervening tokens except 
 whitespace are parsed as a single string literal.


   Unless the string is delineated with single quotes (as in a content 
 list for a drop-down picklist control). :-)

The syntax I described works for any of the valid string delimiters in
Python: single quotes, double quotes, triple single quotes and triple double 
quotes, and the adjacent string literals do not need to use the same delimiter, 
you can mix and match and they'll still be parsed as one big string literal.

   I'm used to seeing (and writing) newlines in bash scripts and other 
 text so now I know python recognizes it I can use this solution.

The problem with the trailing \ is if someone accidentally types a space 
after it, the newline is no longer escaped, and most editors don't highlight 
this error, so you get a confusing message from the interpreter.

Best,

Luciano



 Much appreciated,


 Rich
 ___
 Portland mailing list
 Portland@python.org
 https://mail.python.org/mailman/listinfo/portland



--
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
| http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg
___
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland
___
Portland mailing list
Portland@python.org
https://mail.python.org/mailman/listinfo/portland