
import re
import sys


def main(argv):
    examples = {}
    requires = set()
    for filename in argv[1:]:
        lines = open(filename, 'rU').readlines()
        if len(lines) > 0 and lines[0].startswith('// NOCOMPILE'):
            continue
        requires.update(line for line in lines if line.startswith('goog.require'))
        examples[filename] = [line for line in lines if not line.startswith('goog.require')]
    for require in sorted(requires):
        print require,
    for filename in sorted(examples.keys()):
        print '// ', filename
        print '(function(){'
        for line in examples[filename]:
            print line,
        print '})();'


if __name__ == '__main__':
    sys.exit(main(sys.argv))


import re
import sys


def main(argv):
    examples = {}
    requires = set()
    for filename in argv[1:]:
        lines = open(filename, 'rU').readlines()
        if len(lines) > 0 and lines[0].startswith('// NOCOMPILE'):
            continue
        requires.update(line for line in lines if line.startswith('goog.require'))
        examples[filename] = [line for line in lines if not line.startswith('goog.require')]
    for require in sorted(requires):
        sys.stdout.write(require)
    for filename in sorted(examples.keys()):
        sys.stdout.write('// ' + filename + '\n')
        sys.stdout.write('(function(){\n')
        for line in examples[filename]:
            sys.stdout.write(line)
        sys.stdout.write('})();\n')


if __name__ == '__main__':
    sys.exit(main(sys.argv))

Use write to avoid newline problems
Use write to avoid newline problems

  
  import re
  import sys
  
  
  def main(argv):
      examples = {}
      requires = set()
      for filename in argv[1:]:
          lines = open(filename, 'rU').readlines()
          if len(lines) > 0 and lines[0].startswith('// NOCOMPILE'):
              continue
          requires.update(line for line in lines if line.startswith('goog.require'))
          examples[filename] = [line for line in lines if not line.startswith('goog.require')]
      for require in sorted(requires):
-         print require,
+         sys.stdout.write(require)
      for filename in sorted(examples.keys()):
-         print '// ', filename
-         print '(function(){'
+         sys.stdout.write('// ' + filename + '\n')
+         sys.stdout.write('(function(){\n')
          for line in examples[filename]:
-             print line,
-         print '})();'
+             sys.stdout.write(line)
+         sys.stdout.write('})();\n')
  
  
  if __name__ == '__main__':
      sys.exit(main(sys.argv))
  
Use write to avoid newline problems
## Code Before:

import re
import sys


def main(argv):
    examples = {}
    requires = set()
    for filename in argv[1:]:
        lines = open(filename, 'rU').readlines()
        if len(lines) > 0 and lines[0].startswith('// NOCOMPILE'):
            continue
        requires.update(line for line in lines if line.startswith('goog.require'))
        examples[filename] = [line for line in lines if not line.startswith('goog.require')]
    for require in sorted(requires):
        print require,
    for filename in sorted(examples.keys()):
        print '// ', filename
        print '(function(){'
        for line in examples[filename]:
            print line,
        print '})();'


if __name__ == '__main__':
    sys.exit(main(sys.argv))

## Instruction:
Use write to avoid newline problems
## Code After:

import re
import sys


def main(argv):
    examples = {}
    requires = set()
    for filename in argv[1:]:
        lines = open(filename, 'rU').readlines()
        if len(lines) > 0 and lines[0].startswith('// NOCOMPILE'):
            continue
        requires.update(line for line in lines if line.startswith('goog.require'))
        examples[filename] = [line for line in lines if not line.startswith('goog.require')]
    for require in sorted(requires):
        sys.stdout.write(require)
    for filename in sorted(examples.keys()):
        sys.stdout.write('// ' + filename + '\n')
        sys.stdout.write('(function(){\n')
        for line in examples[filename]:
            sys.stdout.write(line)
        sys.stdout.write('})();\n')


if __name__ == '__main__':
    sys.exit(main(sys.argv))
