markwallsgrove

Python Iterator & argparse example

Feb 24th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import argparse
  2. import sys
  3.  
  4. class Config(object):
  5.     def __init__(self, fileLoc):
  6.         self.configFile = open(fileLoc, 'r')
  7.  
  8.     def __iter__(self):
  9.         return self
  10.  
  11.     def next(self):
  12.         # throws StopIteration exception
  13.         line = self.configFile.next()
  14.         # TODO: change line here
  15.         return line
  16.  
  17. def main(fileLoc):
  18.     config = Config(fileLoc)
  19.     for line in config:
  20.         sys.stdout.write(line)
  21.  
  22. if __name__ == '__main__':
  23.     parser = argparse.ArgumentParser(description='parse a file')
  24.     parser.add_argument('file', help='file to parse')
  25.     args = parser.parse_args()
  26.     main(args.file)
Advertisement
Add Comment
Please, Sign In to add comment