String and line formatting in Python
Question: I want to make the following command to be formatted in python, in other to
comply with the 80 character per line policy:

    
    
    cmd = """elastic-mapreduce --create --alive \
    --instance-type m1.xlarge\
    --num-instances 5 \
    --supported-product mapr \
    --name m7 \
    --args "--edition,m7"
    """
    

Although in the code it will look like multiple lines, when `cmd` gets
executed I wanted it to look like

    
    
    elastic-mapreduce --create --alive --instance-type m1.xlarge --num-instances 5 supported-product mapr name m7 args "--edition,m7"
    

That's a similar problem I'm having here:

    
    
    raise ValueError, "%s hadoop %s version is not supported" % (self.distribution, version)
    

that line it's too long, I would like to do something like:

    
    
    raise ValueError,\ 
    "%s hadoop %s version is not supported" % (self.distribution, version)
    


Answer: As for your first one bit of code you could write a method to 'linearize' your
whitespace

    
    
     import re
     def linearize_whitespace_regex(text):
          formatted = text.strip().replace('\n',' ').replace('\r',' ').replace('\t',' ')
          formatted = re.sub(r'\s{2,}',' ',formatted)
          return formatted
    

I used the regular expressions library, this of course could be accomplished
without that import using your own parsing:

    
    
    def linearize_whitespace_manual(text):
          formatted = text.strip().replace('\n',' ').replace('\r',' ').replace('\t',' ')
          ws_buf = ''
          format_buf = ''
          for i in formatted:
              if i == ' ':
                    if len(ws_buf) < 1:
                        ws_buf += i
              else:
                    format_buf += ws_buf + i
                    ws_buf = ''
          return format_buf
    

and output:

    
    
    >>> cmd = """elastic-mapreduce --create --alive \
    ... --instance-type m1.xlarge\
    ... --num-instances 5 \
    ... --supported-product mapr \
    ... --name m7 \
    ... --args "--edition,m7"
    ... """
    >>> linearize_whitespace_regex(cmd)
    'elastic-mapreduce --create --alive --instance-type m1.xlarge--num-instances 5 --supported-product mapr --name m7 --args "--edition,m7"'
    >>> linearize_whitespace_manual(cmd)
    'elastic-mapreduce --create --alive --instance-type m1.xlarge--num-instances 5 --supported-product mapr --name m7 --args "--edition,m7"'
    

As for your second question raising exceptions can also be done in this
fashion:

    
    
    raise Exception("My error message")
    

So you could write it like this:

    
    
    raise ValueError("%s hadoop %s version is not supported" % (self.distribution, version))
    

And fulfill your line requirement in any of these ways:

    
    
    raise ValueError("%s hadoop %s version is not supported"
        % (self.distribution, version))
    
    
    raise ValueError(
        "%s hadoop %s version is not supported" % (self.distribution, version))
    
    
    raise ValueError(
         "%s hadoop %s version is not supported"
         % (self.distribution, version)
    )
    

