Mercurial > hg > config
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 698:3481ac810b60 | 699:72d2a2e09c6a |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 4 """ | |
| 5 slice a arbitrarily sized list | |
| 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 | |
| 17 def slice(container, n_chunks): | |
| 18 size = int(len(container)/n_chunks) | |
| 19 | |
| 20 | |
| 21 class Parser(argparse.ArgumentParser): | |
| 22 """CLI option parser""" | |
| 23 def __init__(self, **kwargs): | |
| 24 kwargs.setdefault('description', __doc__) | |
| 25 argparse.ArgumentParser.__init__(self, **kwargs) | |
| 26 self.add_argument('N', type=int, | |
| 27 help="number of chunks") | |
| 28 self.add_argument('-M', '--len', dest='length', type=int, | |
| 29 help="length of list") | |
| 30 self.options = None | |
| 31 | |
| 32 def parse_args(self, *args, **kw): | |
| 33 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
| 34 self.validate(options) | |
| 35 self.options = options | |
| 36 return options | |
| 37 | |
| 38 def validate(self, options): | |
| 39 """validate options""" | |
| 40 | |
| 41 def main(args=sys.argv[1:]): | |
| 42 """CLI""" | |
| 43 | |
| 44 # parse command line options | |
| 45 parser = Parser() | |
| 46 options = parser.parse_args(args) | |
| 47 | |
| 48 if __name__ == '__main__': | |
| 49 main() | |
| 50 |
