1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{gen [-p PAD] [COUNT [START]]}
19
20 Generate a sequence of C{COUNT} integers, starting at C{START}. If
21 C{START} is not specified, then the sequence starts at 0. If no
22 arguments are specified, then the sequence starts at 0 and does not
23 terminate.
24
25 If the C{-p} flag is specified, then the generated integers are
26 converted to strings and left padded so that each string contains
27 C{PAD} characters. C{len(str(COUNT + START))} must not exceed C{PAD}.
28 """
29
30 import osh.core
31 import osh.args
32
33 Option = osh.args.Option
34
35
38
39
40 -def gen(count = None, start = 0, pad = None):
41 """Generate a sequence of C{count} integers, starting at C{start}. If no arguments
42 are provided then the sequence starts at 0 and does not terminate. If C{pad} is
43 specified, and is an integer, then the output values are strings, padded to the
44 specified length. The length of the generated strings must not exceed C{pad}.
45 """
46 args = [count, start]
47 if pad is not None:
48 args.append(Option('-p', pad))
49 return _Gen().process_args(*args)
50
51 -class _Gen(osh.core.Generator):
52
53 _count = None
54 _start = None
55 _format = None
56
57
58
61
62
63
66
68 args = self.args()
69 self._count = args.next_int()
70 self._start = args.next_int()
71 pad = args.int_arg('-p')
72 if self._start is None:
73 self._start = 0
74 if pad is not None:
75 self._format = '%%0%sd' % pad
76 if len(str(self._count + self._start)) > pad:
77 self.usage()
78 if args.has_next():
79 self.usage()
80
81
82
83
85 if self._count is None:
86 n = 0
87 while True:
88 self._format_and_send(n)
89 n += 1
90 else:
91 for x in xrange(self._start, self._start + self._count):
92 self._format_and_send(x)
93
94
95
101