annotate python/slice.py @ 699:72d2a2e09c6a

stub: slicing
author Jeff Hammel <k0scist@gmail.com>
date Tue, 12 Aug 2014 16:49:58 -0700
parents
children 2a3323626bcf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
699
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 slice a arbitrarily sized list
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import subprocess
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 # module globals
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 __all__ = ['main', 'Parser']
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def slice(container, n_chunks):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 size = int(len(container)/n_chunks)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 class Parser(argparse.ArgumentParser):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """CLI option parser"""
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def __init__(self, **kwargs):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 kwargs.setdefault('description', __doc__)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 argparse.ArgumentParser.__init__(self, **kwargs)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 self.add_argument('N', type=int,
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 help="number of chunks")
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 self.add_argument('-M', '--len', dest='length', type=int,
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 help="length of list")
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 self.options = None
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 def parse_args(self, *args, **kw):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 self.validate(options)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 self.options = options
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 return options
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 def validate(self, options):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 """validate options"""
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 def main(args=sys.argv[1:]):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 """CLI"""
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 # parse command line options
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 parser = Parser()
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 options = parser.parse_args(args)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 if __name__ == '__main__':
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 main()
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50