def rewrap(text, width=None):
 if (width is None):
  width = 80
 text = textwrap.dedent(text)
 def needs_wrapping(line):
  return (not line[0].isspace())
 wrapped_text = ''
 for (do_wrap, lines) in itertools.groupby(text.splitlines(True), key=needs_wrapping):
  paragraph = ''.join(lines)
  if do_wrap:
   paragraph = textwrap.fill(paragraph, width)
  wrapped_text += paragraph
 return wrapped_text

def rewrap ( text , width = None ) : if ( width is None ) : width = 80 text = textwrap . dedent ( text ) def needs_wrapping ( line ) : return ( not line [ 0 ] . isspace ( ) ) wrapped_text = '' for ( do_wrap , lines ) in itertools . groupby ( text . splitlines ( True ) , key = needs_wrapping ) : paragraph = '' . join ( lines ) if do_wrap : paragraph = textwrap . fill ( paragraph , width ) wrapped_text += paragraph return wrapped_text
rewrap text for output to the console .