Mercurial > hg > numerics
comparison numerics/check.py @ 12:a929d14c0701
start data checks
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 11 Sep 2014 15:31:04 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:5609225fb254 | 12:a929d14c0701 |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 data integrity checking | |
6 """ | |
7 | |
8 # imports | |
9 import argparse | |
10 import os | |
11 import subprocess | |
12 import sys | |
13 | |
14 # module globals | |
15 __all__ = ['main', 'CheckParser'] | |
16 string = (str, unicode) | |
17 | |
18 class NumberOfColumnsException(Exception): | |
19 """wrong number of columns""" | |
20 | |
21 def check_column_lengths(*rows): | |
22 """ensure all column lengths are the same and return number""" | |
23 lengths = set([len(row) for row in rows]) | |
24 if len(lengths) > 1: | |
25 raise NumberOfColumnsException("Multiple numbers of columns: {}".format(', '.join(lengths))) | |
26 return lengths.pop() | |
27 | |
28 | |
29 class CheckParser(argparse.ArgumentParser): | |
30 """CLI option parser""" | |
31 def __init__(self, **kwargs): | |
32 kwargs.setdefault('description', __doc__) | |
33 argparse.ArgumentParser.__init__(self, **kwargs) | |
34 self.options = None | |
35 | |
36 def parse_args(self, *args, **kw): | |
37 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
38 self.validate(options) | |
39 self.options = options | |
40 return options | |
41 | |
42 def validate(self, options): | |
43 """validate options""" | |
44 | |
45 def main(args=sys.argv[1:]): | |
46 """CLI""" | |
47 | |
48 # parse command line options | |
49 parser = Parser() | |
50 options = parser.parse_args(args) | |
51 | |
52 if __name__ == '__main__': | |
53 main() | |
54 |