annotate python/example/slice.py @ 904:30006a5583fa

fix hgrc.py for python3
author Jeff Hammel <k0scist@gmail.com>
date Thu, 11 Apr 2024 08:23:38 -0700
parents 6194eec4e3b7
children
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
700
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
14 __all__ = ['slice', 'main', 'Parser']
699
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def slice(container, n_chunks):
700
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
17 size = int(len(container)/(n_chunks-1))
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
18 retval = []
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
19 start = 0
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
20 for i in range(n_chunks-1):
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
21 retval.append(container[start:start+size])
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
22 start += size
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
23 retval.append(container[start:])
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
24 return retval
699
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 class Parser(argparse.ArgumentParser):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """CLI option parser"""
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 def __init__(self, **kwargs):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 kwargs.setdefault('description', __doc__)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 argparse.ArgumentParser.__init__(self, **kwargs)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 self.add_argument('N', type=int,
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 help="number of chunks")
700
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
33 self.add_argument('-M', '--len', dest='length', type=int, default=29,
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
34 help="length of list [DEFAULT: %(default)s]")
699
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 self.options = None
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 def parse_args(self, *args, **kw):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 self.validate(options)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 self.options = options
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 return options
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 def validate(self, options):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 """validate options"""
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 def main(args=sys.argv[1:]):
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 """CLI"""
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 # parse command line options
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 parser = Parser()
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 options = parser.parse_args(args)
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
700
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
53 # generate list
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
54 seq = range(options.length)
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
55
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
56 # chunk list
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
57 output = slice(seq, options.N)
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
58
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
59 # print output
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
60 for chunk in output:
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
61 print (",".join([str(i) for i in chunk]))
Jeff Hammel <k0scist@gmail.com>
parents: 699
diff changeset
62
699
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 if __name__ == '__main__':
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 main()
72d2a2e09c6a stub: slicing
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65