Need cleanup advice for multiline string

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Aug 11 22:32:56 CEST 2009


Robert Dailey wrote:
> On Aug 11, 3:08 pm, Robert Dailey <rcdai... at gmail.com> wrote:
>> Hey guys. Being a C++ programmer, I like to keep variable definitions
>> close to the location in which they will be used. This improves
>> readability in many ways. However, when I have a multi-line string
>> definition at function level scope, things get tricky because of the
>> indents. In this case indents are serving two purposes: For syntax and
>> actual text output. The tabs for function scope should not be included
>> in the contents of the string. Below is the code I am trying to
>> improve. Notice how it looks ugly/unreadable because of the way the
>> string contents are shifted all the way to the left edge of the
>> document. This breaks the flow of scope. Would you guys solve this
>> problem by moving failMsg into global scope? Perhaps through some
>> other type of syntax?
>>
>> Help is appreciated!
>>
>> def RunCommand( commandList ):
>>    commandString =
>>    print( 'Running Command:',  )
>>    cmd = subprocess.Popen( commandList )
>>    returnCode = cmd.wait()
>>    if returnCode:
>>       failMsg = '''\
>> *************************************************
>> The following command returned exit code [{:#x}].
>> This represents failure of some form. Please review
>> the command output for more details on the issue.
>> ------------
>> {}
>> *************************************************
>> '''
>>       commandString = ' '.join( commandList )
>>       raise CommandFailure( failMsg.format( returnCode,
>> commandString ) )
> 
> And yes, I recognize there are syntax errors. Ignore those for now.
For starters take a look at http://tinyurl.com/o2o8r8 , just about every 
combination of string concatenation going there.  I assume that one of 
these will let you leave failMsg where it belongs.

-- 
Kindest regards.

Mark Lawrence.




More information about the Python-list mailing list