changeset 700:2a3323626bcf

wfm
author Jeff Hammel <k0scist@gmail.com>
date Wed, 13 Aug 2014 11:24:09 -0700
parents 72d2a2e09c6a
children de7bf9523e21
files python/slice.py
diffstat 1 files changed, 21 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/python/slice.py	Tue Aug 12 16:49:58 2014 -0700
+++ b/python/slice.py	Wed Aug 13 11:24:09 2014 -0700
@@ -12,11 +12,17 @@
 import sys
 
 # module globals
-__all__ = ['main', 'Parser']
+__all__ = ['slice', 'main', 'Parser']
 
 def slice(container, n_chunks):
-    size = int(len(container)/n_chunks)
-    
+    size = int(len(container)/(n_chunks-1))
+    retval = []
+    start = 0
+    for i in range(n_chunks-1):
+        retval.append(container[start:start+size])
+        start += size
+    retval.append(container[start:])
+    return retval
 
 class Parser(argparse.ArgumentParser):
     """CLI option parser"""
@@ -25,8 +31,8 @@
         argparse.ArgumentParser.__init__(self, **kwargs)
         self.add_argument('N', type=int,
                           help="number of chunks")
-        self.add_argument('-M', '--len', dest='length', type=int,
-                          help="length of list")
+        self.add_argument('-M', '--len', dest='length', type=int, default=29,
+                          help="length of list [DEFAULT: %(default)s]")
         self.options = None
 
     def parse_args(self, *args, **kw):
@@ -45,6 +51,16 @@
     parser = Parser()
     options = parser.parse_args(args)
 
+    # generate list
+    seq = range(options.length)
+
+    # chunk list
+    output = slice(seq, options.N)
+
+    # print output
+    for chunk in output:
+        print (",".join([str(i) for i in chunk]))
+
 if __name__ == '__main__':
     main()