import sys
import re
import argparse

from patterns import pre_patterns


def prepare(infile):
    """
    Apply pre_patterns from patterns to infile

    :infile: input file

    """

    try:
        for line in infile:
            result = line
            for pattern in pre_patterns:
                result = re.sub(pattern[0], pattern[1], result)
                sys.stdout.write(result)
    except (KeyboardInterrupt):
        pass


def main():
    parser = argparse.ArgumentParser(description='Parse file[s]')
    parser.add_argument('file', nargs='*', default=[],
                        help='file[s] to do some work')
    parser.add_argument('-s', '--stat', action='store_const', const=True,
                        help='get statistics')
    args = parser.parse_args()

    if not args.file:
        prepare(sys.stdin)
    else:
        for filename in args.file:
            with open(filename, 'r') as f:
                prepare(f)

    # if outfilename:
        # outfile = open(outfilename, 'w')
    # else:
        # outfile = sys.stdout

    pass

if __name__ == '__main__':
    main()

import sys
import re
import argparse
from argparse import RawDescriptionHelpFormatter

from patterns import pre_patterns


def prepare(infile, outfile=sys.stdout):
    """
    Apply pre_patterns from patterns to infile

    :infile: input file

    """

    try:
        for line in infile:
            result = line
            for pattern in pre_patterns:
                result = re.sub(pattern[0], pattern[1], result, re.VERBOSE)
            outfile.write(result)
    except (KeyboardInterrupt):
        pass
    except:
        raise


def main():
    parser = argparse.ArgumentParser(description=\
    """
    Parse file[s]\n\n
    examlpe: cat error_log | tail -n 1000 | ./direlog.py
    """, formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument('file', nargs='*', default=[],
                        help='file[s] to do some work')
    parser.add_argument('-s', '--stat', action='store_const', const=True,
                        help='get statistics')
    args = parser.parse_args()

    if not args.file:
        prepare(sys.stdin)
    else:
        for filename in args.file:
            with open(filename, 'r') as f:
                prepare(f)

    pass

if __name__ == '__main__':
    main()

Add some info and outfile to prepare function
Add some info and outfile to prepare function

  import sys
  import re
  import argparse
+ from argparse import RawDescriptionHelpFormatter
  
  from patterns import pre_patterns
  
  
- def prepare(infile):
+ def prepare(infile, outfile=sys.stdout):
      """
      Apply pre_patterns from patterns to infile
  
      :infile: input file
  
      """
  
      try:
          for line in infile:
              result = line
              for pattern in pre_patterns:
-                 result = re.sub(pattern[0], pattern[1], result)
+                 result = re.sub(pattern[0], pattern[1], result, re.VERBOSE)
-                 sys.stdout.write(result)
+             outfile.write(result)
      except (KeyboardInterrupt):
          pass
+     except:
+         raise
  
  
  def main():
-     parser = argparse.ArgumentParser(description='Parse file[s]')
+     parser = argparse.ArgumentParser(description=\
+     """
+     Parse file[s]\n\n
+     examlpe: cat error_log | tail -n 1000 | ./direlog.py
+     """, formatter_class=RawDescriptionHelpFormatter)
      parser.add_argument('file', nargs='*', default=[],
                          help='file[s] to do some work')
      parser.add_argument('-s', '--stat', action='store_const', const=True,
                          help='get statistics')
      args = parser.parse_args()
  
      if not args.file:
          prepare(sys.stdin)
      else:
          for filename in args.file:
              with open(filename, 'r') as f:
                  prepare(f)
  
-     # if outfilename:
-         # outfile = open(outfilename, 'w')
-     # else:
-         # outfile = sys.stdout
- 
      pass
  
  if __name__ == '__main__':
      main()
  
Add some info and outfile to prepare function
## Code Before:
import sys
import re
import argparse

from patterns import pre_patterns


def prepare(infile):
    """
    Apply pre_patterns from patterns to infile

    :infile: input file

    """

    try:
        for line in infile:
            result = line
            for pattern in pre_patterns:
                result = re.sub(pattern[0], pattern[1], result)
                sys.stdout.write(result)
    except (KeyboardInterrupt):
        pass


def main():
    parser = argparse.ArgumentParser(description='Parse file[s]')
    parser.add_argument('file', nargs='*', default=[],
                        help='file[s] to do some work')
    parser.add_argument('-s', '--stat', action='store_const', const=True,
                        help='get statistics')
    args = parser.parse_args()

    if not args.file:
        prepare(sys.stdin)
    else:
        for filename in args.file:
            with open(filename, 'r') as f:
                prepare(f)

    # if outfilename:
        # outfile = open(outfilename, 'w')
    # else:
        # outfile = sys.stdout

    pass

if __name__ == '__main__':
    main()

## Instruction:
Add some info and outfile to prepare function
## Code After:
import sys
import re
import argparse
from argparse import RawDescriptionHelpFormatter

from patterns import pre_patterns


def prepare(infile, outfile=sys.stdout):
    """
    Apply pre_patterns from patterns to infile

    :infile: input file

    """

    try:
        for line in infile:
            result = line
            for pattern in pre_patterns:
                result = re.sub(pattern[0], pattern[1], result, re.VERBOSE)
            outfile.write(result)
    except (KeyboardInterrupt):
        pass
    except:
        raise


def main():
    parser = argparse.ArgumentParser(description=\
    """
    Parse file[s]\n\n
    examlpe: cat error_log | tail -n 1000 | ./direlog.py
    """, formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument('file', nargs='*', default=[],
                        help='file[s] to do some work')
    parser.add_argument('-s', '--stat', action='store_const', const=True,
                        help='get statistics')
    args = parser.parse_args()

    if not args.file:
        prepare(sys.stdin)
    else:
        for filename in args.file:
            with open(filename, 'r') as f:
                prepare(f)

    pass

if __name__ == '__main__':
    main()
