view python/slice.py @ 741:9681a0bd74d6

add utility for slicing video files
author Jeff Hammel <k0scist@gmail.com>
date Sun, 28 Jun 2015 11:56:13 -0700
parents de7bf9523e21
children
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
slice a arbitrarily sized list
"""

# imports
import argparse
import os
import subprocess
import sys

__all__ = ['slice', 'main', 'Parser']

def slice(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"""
    def __init__(self, **kwargs):
        kwargs.setdefault('description', __doc__)
        argparse.ArgumentParser.__init__(self, **kwargs)
        self.add_argument('N', type=int,
                          help="number of chunks")
        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):
        options = argparse.ArgumentParser.parse_args(self, *args, **kw)
        self.validate(options)
        self.options = options
        return options

    def validate(self, options):
        """validate options"""

def main(args=sys.argv[1:]):
    """CLI"""

    # parse command line options
    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()