Python strings and coding conventions

K

koranthala

Hi,
Python Coding Convention (PEP 8) suggests :
Maximum Line Length

Limit all lines to a maximum of 79 characters.

I have a string which is ~110 char long. It is a string which I am
going to print in a text file as a single string.
i.e. in that text file, each line is taken as a different record -
so it has to be in a single line.

Now, how can I write this code - while following PEP 8?
I tried blockstrings, but as shown in the example below:.... abcd
.... efgh
.... ''''\nabcd\nefgh\n'
it has "\n" inserted - which will disallow printing it to a single
line.

I thought about other options like concatenating strings etc, but
it seems very kludgy - esp since the whole string has a single meaning
and cannot be easily split to many logically. Then I thought of
creating a blockstring and then removing "\n", but it seemed
kludgier...

I am sure this is a very usual issue and I am missing some very
very simple solution. But I cannot think of it at all...
 
R

Robert Kern

Hi,
Python Coding Convention (PEP 8) suggests :
Maximum Line Length

Limit all lines to a maximum of 79 characters.

I have a string which is ~110 char long. It is a string which I am
going to print in a text file as a single string.
i.e. in that text file, each line is taken as a different record -
so it has to be in a single line.

Now, how can I write this code - while following PEP 8?
I tried blockstrings, but as shown in the example below:
... abcd
... efgh
... '''
'\nabcd\nefgh\n'
it has "\n" inserted - which will disallow printing it to a single
line.

I thought about other options like concatenating strings etc, but
it seems very kludgy - esp since the whole string has a single meaning
and cannot be easily split to many logically. Then I thought of
creating a blockstring and then removing "\n", but it seemed
kludgier...

I usually use implicit concatenation:

s = ('some long text that '
'needs to be split')

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
M

Mensanator

I usually use implicit concatenation:

s = ('some long text that '
� � � 'needs to be split')

Damn! I didn't know you could do that! And if I
saw it in a program listing, such would never occur
to me. I was going to suggest the stupid way:
ga = ['four score and seven years ago ', \
'our fathers brought forth ', \
'on this continent a new nation ', \
'conceived in liberty and dedicated ', \
'to the proposition that all men ', \
'are created equal']'four score and seven years ago our fathers brought forth on this
continent a new nation conceived in liberty and dedicated to the
proposition that all men are created equal'
 
K

koranthala

I usually use implicit concatenation:

s = ('some long text that '
      'needs to be split')

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

This is a very good method.
I found another method too - on further investigation'abcefg'
Only problem being that it doesnt support indentation.
So, implicit concatenation it is...
 
M

Marc 'BlackJack' Rintsch

Damn! I didn't know you could do that! And if I saw it in a program
listing, such would never occur to me. I was going to suggest the stupid
way:
ga = ['four score and seven years ago ', \
'our fathers brought forth ', \
'on this continent a new nation ', \
'conceived in liberty and dedicated ', \
'to the proposition that all men ', \
'are created equal']

What are all those line continuation characters ('\') for? You are aware
that they are unnecessary here?

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steve Holden

Mensanator said:
Damn! I didn't know you could do that! And if I
saw it in a program listing, such would never occur
to me. I was going to suggest the stupid way:
Another option is escaping the newlines in triple-quoted strings:

regards
Steve
 
M

MRAB

This is a very good method.
I found another method too - on further investigation
'abcefg'
Only problem being that it doesnt support indentation.
So, implicit concatenation it is...
Another possibility is continuation _plus_ implicit concatenation.

def example():
message = "now is the time " \
"for all good people ..."
print message
 
M

Mensanator

Damn! I didn't know you could do that! And if I saw it in a program
listing, such would never occur to me. I was going to suggest the stupid
way:
ga = ['four score and seven years ago ', \
� � � � � 'our fathers brought forth ', \
� � � � � 'on this continent a new nation ', \
� � � � � 'conceived in liberty and dedicated ', \
� � � � � 'to the proposition that all men ', \
� � � � � 'are created equal']

What are all those line continuation characters ('\') for? �You are aware
that they are unnecessary here?

Actually, I wasn't aware of that. A quick review shows
why. In the old manuals, implicit line continuation
was in a seperate chapter (2.1.6) from implicit (2.1.5)
so if you didn't read past 2.1.5 you would have missed it.

The 2.6 manual is much better in this regard as it is
now difficult to miss.

Thanks for pointing that out as lists are just about
the only place I use line continuation.
 
R

Roy Smith

Mensanator said:
Actually, I wasn't aware of that. A quick review shows
why. In the old manuals, implicit line continuation
was in a seperate chapter (2.1.6) from implicit (2.1.5)
so if you didn't read past 2.1.5 you would have missed it.

My philosophy about line continuation is to assume lines can be continued
after just about any piece of punctuation. If I'm wrong, the computer will
tell me, and then I make the computer happy by adding a \. It's easier
than looking it up, and way easier than memorizing the details.
 
M

Mensanator

My philosophy about line continuation is to assume lines can be continued
after just about any piece of punctuation. �If I'm wrong, the computer will
tell me, and then I make the computer happy by adding a \. �It's easier
than looking it up, and way easier than memorizing the details.

I proably got mine from Visual Basic where there are no
exceptions to explicit line continuation marks. A least
adding them when not necessary doesn't cause a problem.
 
J

John Machin

I proably got mine from Visual Basic where there are no
exceptions to explicit line continuation marks. A least
adding them when not necessary doesn't cause a problem.

Fugly code is not a problem?
 
J

John Machin

Is that how you justify breaing "explicit is
better than implicit"?

I don't '''justify breaing "explicit is better than implicit"''' and
wasn't attempting to do so.

The Zen is more than one saying. No one saying trumps all others. Some
may appear to be in conflict with others. One must consider the
combined effect. Other sayings relevant to your problem are:

Beautiful is better than ugly.
Simple is better than complex.
Readability counts.

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top