How to to break up a long format string line of code

This is the place for queries that don't fit in any of the other categories.

How to to break up a long format string line of code

Postby memilanuk » Tue Feb 26, 2013 6:57 pm

So... I'm working thru a tutorial, and one of the examples is giving me some grief. I've got everything working per se, but it seems like I can either have everything on one long line and run afoul of pep8 (more than 79 char in a given line of code) or break the line for readability, at which point the indentation shows up as tabs in my string. The third option of breaking the line but not indenting it gives the correct looking output, but the code just looks... wrong.

The worked example uses a completely different method - i.e. constructing the output one line first and appending each subsequent line to that so that no any one line is too long. Obviously that works, but I'm trying to figure out if there is some way to make it work otherwise.

I know pep8 is 'just' a style guide, i.e. optional, but I'd like to try to adhere to it when/where possible. Any suggestions?

The program selects several distinct (i.e. non-repeating) nouns, verbs, etc. from lists of gibberish and then returns a formatted string for printing. Below is just the code for the string creation and formatting portion of the program.

My original version:

Code: Select all
    poem = """{0} {7} {1}\n
    {0} {7} {1} {4} {10} the {8} {2}\n
    {12}, the {1}{5}\n
    the {2} {6} {11} a {9} {3}""".format(article, n1, n2, n3, v1, v2, v3,
    adj1, adj2, adj3, prep1, prep2, adv1)


Which returns output that looks like this:

Code: Select all
An exuberant mango

    An exuberant mango explodes upon the glistening aardvark

    sensuously, the mangomeows

    the aardvark bounces within a furry extrovert


This version works, but the line is waaaaay too long (the actual code doesn't wrap, that is just from the forum):

Code: Select all
    poem = """{0} {7} {1}\n{0} {7} {1} {4} {10} the {8} {2}\n{12}, the {1}{5}\nthe {2} {6} {11} a {9} {3}""".format(article, n1, n2, n3, v1, v2, v3, adj1, adj2, adj3, prep1, prep2, adv1)


Output (i.e. what I want it to look like):

Code: Select all
A balding judge
A balding judge curdles upon the incredulous extrovert
tantalizingly, the judgebounces
the extrovert explodes in a fragrant fossil


This way works, but looks just... wrong:

Code: Select all
    poem = """{0} {7} {1}\n
{0} {7} {1} {4} {10} the {8} {2}\n
{12}, the {1}{5}\n
the {2} {6} {11} a {9} {3}""".format(article, n1, n2, n3, v1, v2, v3,
    adj1, adj2, adj3, prep1, prep2, adv1)


The worked solution for this problem:

Code: Select all
    poem = "{} {} {}\n\n".format(article, adj1, n1)
    poem = poem + "{} {} {} {} {} the {} {}\n".format(article, adj1, n1, v1, prep1, adj2, n2)
    poem = poem + "{}, the {} {}\n".format(adv1, n1, v2)
    poem = poem + "the {} {} {} a {} {}".format(n2, v3, prep2, adj3, n3)


Like I said, it obviously works but I was hoping there was a way to the desired (formatted) results without having to build the output literally line by line...

TIA,

Monte
Last edited by Yoriz on Thu Feb 28, 2013 6:59 pm, edited 1 time in total.
Reason: Changed to a more descriptive title
memilanuk
 
Posts: 7
Joined: Wed Feb 20, 2013 7:16 pm

Re: Tab from code indent showing up in my string

Postby Yoriz » Tue Feb 26, 2013 7:20 pm

Code: Select all
article = 'A'
n1 = 'judge'
n2 = 'extrovert'
n3 = 'fossil'
v1 = 'curdles'
v2 = 'bounces'
v3 = 'explodes'
adj1 = 'balding'
adj2 ='incredulous'
adj3 = 'fragrant'
prep1 = 'upon'
prep2 = 'in'
adv1 = 'tantalizingly'

poem = "{0} {7} {1}\n{0} {7} {1} {4} {10} the {8} {2}\n{12}, the {1}{5}\nthe"\
       " {2} {6} {11} a {9} {3}".format(article, n1, n2, n3, v1, v2, v3, adj1,
       adj2, adj3, prep1, prep2, adv1)

print poem
>>>
A balding judge
A balding judge curdles upon the incredulous extrovert
tantalizingly, the judgebounces
the extrovert explodes in a fragrant fossil
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Tab from code indent showing up in my string

Postby memilanuk » Tue Feb 26, 2013 7:50 pm

Ah. Going with single quotes instead of triple quotes seems to allow things to break up more the way I expected.

Cool, thanks!

Monte
memilanuk
 
Posts: 7
Joined: Wed Feb 20, 2013 7:16 pm

Re: Tab from code indent showing up in my string

Postby Jaro » Tue Feb 26, 2013 8:25 pm

memilanuk wrote:Ah. Going with single quotes instead of triple quotes seems to allow things to break up more the way I expected.

Cool, thanks!

Monte


Well, here's another option, right out of the standard library. First of all:

Code: Select all
import textwrap


and then modify your code like this:

Code: Select all
    poem = """\
    {0} {7} {1}
    {0} {7} {1} {4} {10} the {8} {2}
    {12}, the {1}{5}
    the {2} {6} {11} a {9} {3}""".format(article, n1, n2, n3, v1, v2, v3,
    adj1, adj2, adj3, prep1, prep2, adv1)
    poem=textwrap.dedent(poem)


I noticed that you had added a newline character at the end of each line, so I deleted them. You don't need them when using triple quotes.
Code: Select all
<function signature at 0xb73f910c>
User avatar
Jaro
 
Posts: 8
Joined: Sat Feb 23, 2013 6:16 pm

Re: Tab from code indent showing up in my string

Postby micseydel » Tue Feb 26, 2013 8:34 pm

How about this?
Code: Select all
poem = ("{0} {7} {1}\n"
    "{0} {7} {1} {4} {10} the {8} {2}\n"
    "{12}, the {1}{5}\n"
    "the {2} {6} {11} a {9} {3}").format(article, n1, n2, n3, v1, v2, v3,
    adj1, adj2, adj3, prep1, prep2, adv1)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Tab from code indent showing up in my string

Postby memilanuk » Tue Feb 26, 2013 11:40 pm

jaro,

I'd came across the textwrap module when I was searching last night... I wasn't clear on the usage and whether it did what I was after, but it looks like it does.

Thanks,

Monte
memilanuk
 
Posts: 7
Joined: Wed Feb 20, 2013 7:16 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 4 guests