comparison numerics/manipulate.py @ 57:f16d5f013739

stub for manipulating data
author Jeff Hammel <k0scist@gmail.com>
date Tue, 20 Jan 2015 16:32:16 -0800
parents
children 537fba7c9def
comparison
equal deleted inserted replaced
56:06e487f90e05 57:f16d5f013739
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 manipulate CSV data
6 """
7
8 # imports
9 import argparse
10 import os
11 import subprocess
12 import sys
13
14 # module globals
15 __all__ = ['main', 'Parser']
16 here = os.path.dirname(os.path.realpath(__file__))
17 string = (str, unicode)
18
19 def ensure_dir(directory):
20 """ensure a directory exists"""
21 if os.path.exists(directory):
22 if not os.path.isdir(directory):
23 raise OSError("Not a directory: '{}'".format(directory))
24 return directory
25 os.makedirs(directory)
26 return directory
27
28
29 class Parser(argparse.ArgumentParser):
30 """CLI option parser"""
31 def __init__(self, **kwargs):
32 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
33 kwargs.setdefault('description', __doc__)
34 argparse.ArgumentParser.__init__(self, **kwargs)
35 self.options = None
36
37 def parse_args(self, *args, **kw):
38 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
39 self.validate(options)
40 self.options = options
41 return options
42
43 def validate(self, options):
44 """validate options"""
45
46 def main(args=sys.argv[1:]):
47 """CLI"""
48
49 # parse command line options
50 parser = Parser()
51 options = parser.parse_args(args)
52
53 if __name__ == '__main__':
54 main()
55