Multi-line strings with formatting

Discussion in 'Python' started by gburdell1@gmail.com, Mar 23, 2007.

  1. Guest

    When constructing a particularly long and complicated command to be
    sent to the shell, I usually do something like this, to make the
    command as easy as possible to follow:

    commands.getoutput(
    'mycommand -S %d -T %d ' % (s_switch, t_switch) +
    '-f1 %s -f2 %s ' % (filename1, filename2) +
    '> %s' % (log_filename)
    )

    Can anyone suggest a better way to construct the command, especially
    without the "+" sign at the end of each line (except the last) ? If I
    take out the "+", then I need to move all the variables to the end, as
    so:

    commands.getoutput(
    'mycommand -S %d -T %d '
    '-f1 %s -f2 %s '
    '> %s'
    % (s_switch, t_switch, filename1, filename2, log_filename)
    )

    or:

    commands.getoutput(
    '''mycommand -S %d -T %d \
    -f1 %s -f2 %s \
    > %s'''

    % (s_switch, t_switch, filename1, filename2, log_filename)
    )

    but having the variables line-by-line as in the first example is so
    much easier to edit, is it not?
    , Mar 23, 2007
    #1
    1. Advertising

  2. On Fri, 2007-03-23 at 09:54 -0700, wrote:
    > When constructing a particularly long and complicated command to be
    > sent to the shell, I usually do something like this, to make the
    > command as easy as possible to follow:
    >
    > commands.getoutput(
    > 'mycommand -S %d -T %d ' % (s_switch, t_switch) +
    > '-f1 %s -f2 %s ' % (filename1, filename2) +
    > '> %s' % (log_filename)
    > )
    >
    > Can anyone suggest a better way to construct the command, especially
    > without the "+" sign at the end of each line (except the last) ? If I
    > take out the "+", then I need to move all the variables to the end, as
    > so:
    >
    > commands.getoutput(
    > 'mycommand -S %d -T %d '
    > '-f1 %s -f2 %s '
    > '> %s'
    > % (s_switch, t_switch, filename1, filename2, log_filename)
    > )
    >
    > or:
    >
    > commands.getoutput(
    > '''mycommand -S %d -T %d \
    > -f1 %s -f2 %s \
    > > %s'''

    > % (s_switch, t_switch, filename1, filename2, log_filename)
    > )


    You get the best of both worlds, i.e. one big multiline string with
    in-line parameters, by using a mapping:

    commands.getoutput(
    '''mycommand -S %(s_switch)d -T %(t_switch)d \
    -f1 %(filename1)s -f2 %(filename2)s \
    > %(log_filename)s'''

    % locals() )

    Of course I'm assuming that s_switch etc. are local variables. If
    they're not, well, they ought to be.

    -Carsten
    Carsten Haese, Mar 23, 2007
    #2
    1. Advertising

  3. Steve Holden Guest

    Carsten Haese wrote:
    > On Fri, 2007-03-23 at 09:54 -0700, wrote:
    >> When constructing a particularly long and complicated command to be
    >> sent to the shell, I usually do something like this, to make the
    >> command as easy as possible to follow:
    >>
    >> commands.getoutput(
    >> 'mycommand -S %d -T %d ' % (s_switch, t_switch) +
    >> '-f1 %s -f2 %s ' % (filename1, filename2) +
    >> '> %s' % (log_filename)
    >> )
    >>
    >> Can anyone suggest a better way to construct the command, especially
    >> without the "+" sign at the end of each line (except the last) ? If I
    >> take out the "+", then I need to move all the variables to the end, as
    >> so:
    >>
    >> commands.getoutput(
    >> 'mycommand -S %d -T %d '
    >> '-f1 %s -f2 %s '
    >> '> %s'
    >> % (s_switch, t_switch, filename1, filename2, log_filename)
    >> )
    >>
    >> or:
    >>
    >> commands.getoutput(
    >> '''mycommand -S %d -T %d \
    >> -f1 %s -f2 %s \
    >> > %s'''

    >> % (s_switch, t_switch, filename1, filename2, log_filename)
    >> )

    >
    > You get the best of both worlds, i.e. one big multiline string with
    > in-line parameters, by using a mapping:
    >
    > commands.getoutput(
    > '''mycommand -S %(s_switch)d -T %(t_switch)d \
    > -f1 %(filename1)s -f2 %(filename2)s \
    > > %(log_filename)s'''

    > % locals() )
    >
    > Of course I'm assuming that s_switch etc. are local variables. If
    > they're not, well, they ought to be.
    >
    > -Carsten
    >
    >

    If that doesn't suit then build a list:

    l = [
    'mycommand -S %d -T %d ' % (s_switch, t_switch) ,
    '-f1 %s -f2 %s ' % (filename1, filename2) ,
    '> %s' % (log_filename)
    ]

    and then return commands.getoutput("".join(l)).

    regards
    Steve

    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
    Steve Holden, Mar 23, 2007
    #3
  4. Paul McGuire Guest

    On Mar 23, 1:25 pm, Steve Holden <> wrote:
    > Carsten Haese wrote:
    > > On Fri, 2007-03-23 at 09:54 -0700, wrote:
    > >> When constructing a particularly long and complicated command to be
    > >> sent to the shell, I usually do something like this, to make the
    > >> command as easy as possible to follow:

    >
    > >> commands.getoutput(
    > >> 'mycommand -S %d -T %d ' % (s_switch, t_switch) +
    > >> '-f1 %s -f2 %s ' % (filename1, filename2) +
    > >> '> %s' % (log_filename)
    > >> )

    >
    > >> Can anyone suggest a better way to construct the command, especially
    > >> without the "+" sign at the end of each line (except the last) ? If I
    > >> take out the "+", then I need to move all the variables to the end, as
    > >> so:

    >
    > >> commands.getoutput(
    > >> 'mycommand -S %d -T %d '
    > >> '-f1 %s -f2 %s '
    > >> '> %s'
    > >> % (s_switch, t_switch, filename1, filename2, log_filename)
    > >> )

    >
    > >> or:

    >
    > >> commands.getoutput(
    > >> '''mycommand -S %d -T %d \
    > >> -f1 %s -f2 %s \
    > >> > %s'''
    > >> % (s_switch, t_switch, filename1, filename2, log_filename)
    > >> )

    >
    > > You get the best of both worlds, i.e. one big multiline string with
    > > in-line parameters, by using a mapping:

    >
    > > commands.getoutput(
    > > '''mycommand -S %(s_switch)d -T %(t_switch)d \
    > > -f1 %(filename1)s -f2 %(filename2)s \
    > > > %(log_filename)s'''

    > > % locals() )

    >
    > > Of course I'm assuming that s_switch etc. are local variables. If
    > > they're not, well, they ought to be.

    >
    > > -Carsten

    >
    > If that doesn't suit then build a list:
    >
    > l = [
    > 'mycommand -S %d -T %d ' % (s_switch, t_switch) ,
    > '-f1 %s -f2 %s ' % (filename1, filename2) ,
    > '> %s' % (log_filename)
    > ]
    >
    > and then return commands.getoutput("".join(l)).
    >
    > regards
    > Steve
    >
    > --
    > Steve Holden +44 150 684 7255 +1 800 494 3119
    > Holden Web LLC/Ltd http://www.holdenweb.com
    > Skype: holdenweb http://del.icio.us/steve.holden
    > Recent Ramblings http://holdenweb.blogspot.com- Hide quoted text -
    >
    > - Show quoted text -


    This list might be even simpler to follow:

    l = [
    'mycommand',
    '-S', s_switch,
    '-T', t_switch,
    '-f1', filename1,
    '-f2', filename2,
    '>', log_filename
    ]
    cmd = " ".join(l)

    (and I'm glad I'm not the only one who uses 'l' for a scratch list
    variable...)

    -- Paul
    Paul McGuire, Mar 24, 2007
    #4
  5. On Fri, 23 Mar 2007 19:39:53 -0700, Paul McGuire wrote:

    > (and I'm glad I'm not the only one who uses 'l' for a scratch list
    > variable...)


    Yes, and come the revolution, every last one of you will be down the salt
    mines.

    I don't mind using capital L as a variable, but l looks too much like I
    and 1 in most typefaces. Capital O is another nasty one. I also try to
    avoid using a as a variable name, because a is a definite article
    in English (like "the") and that makes it difficult to write grammatical
    sentences about what you're doing.


    --
    Steven.
    Steven D'Aprano, Mar 24, 2007
    #5
  6. On Fri, 2007-03-23 at 09:54 -0700, wrote:
    > When constructing a particularly long and complicated command to be
    > sent to the shell, I usually do something like this, to make the
    > command as easy as possible to follow:
    > commands.getoutput(
    > 'mycommand -S %d -T %d ' % (s_switch, t_switch) +
    > '-f1 %s -f2 %s ' % (filename1, filename2) +
    > '> %s' % (log_filename)
    > )
    > Can anyone suggest a better way to construct the command, especially
    > without the "+" sign at the end of each line (except the last) ?


    Paul McGuire wrote:
    > This list might be even simpler to follow:
    >
    > l = [
    > 'mycommand',
    > '-S', s_switch,
    > '-T', t_switch,
    > '-f1', filename1,
    > '-f2', filename2,
    > '>', log_filename
    > ]
    > cmd = " ".join(l)


    And if you use the subprocess module, you won't even need (or want) the
    final join.

    STeVe
    Steven Bethard, Mar 24, 2007
    #6
  7. Steve Holden Guest

    Steven D'Aprano wrote:
    > On Fri, 23 Mar 2007 19:39:53 -0700, Paul McGuire wrote:
    >
    >> (and I'm glad I'm not the only one who uses 'l' for a scratch list
    >> variable...)

    >
    > Yes, and come the revolution, every last one of you will be down the salt
    > mines.
    >

    Better that than up against the wall, I suppose.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Recent Ramblings http://holdenweb.blogspot.com
    Steve Holden, Mar 25, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    733
    Malcolm
    Jun 24, 2006
  2. Jiri Barton

    unicode in multi-line strings

    Jiri Barton, Sep 18, 2008, in forum: Python
    Replies:
    1
    Views:
    334
    Diez B. Roggisch
    Sep 18, 2008
  3. goldtech
    Replies:
    33
    Views:
    994
    Sibylle Koczian
    May 2, 2010
  4. Navindra Umanee

    multi-line strings with """

    Navindra Umanee, Jul 15, 2005, in forum: Ruby
    Replies:
    1
    Views:
    191
    YANAGAWA Kazuhisa
    Jul 15, 2005
  5. John Trupiano
    Replies:
    1
    Views:
    166
    John Trupiano
    Sep 5, 2008
Loading...

Share This Page