Package osh :: Package command :: Module gen
[frames] | no frames]

Source Code for Module osh.command.gen

  1  # osh 
  2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 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  # CLI 
36 -def _gen():
37 return _Gen()
38 39 # API
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 # object interface 58
59 - def __init__(self):
60 osh.core.Generator.__init__(self, 'p:', (0, 2))
61 62 # BaseOp interface 63
64 - def doc(self):
65 return __doc__
66
67 - def setup(self):
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 # Generator interface 83
84 - def execute(self):
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 # For use by this class 95
96 - def _format_and_send(self, x):
97 if self._format is None: 98 self.send(x) 99 else: 100 self.send(self._format % x)
101