home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / rsa / engine.py < prev    next >
Text File  |  1993-12-17  |  1KB  |  54 lines

  1. #
  2. # $Id: engine.py,v 1.1 1993/12/17 14:34:41 guido Exp $
  3. #
  4.  
  5. from report import *
  6.  
  7. from merge import gluetimes
  8.  
  9. class Engine:
  10.     #
  11.     #
  12.     #
  13.     def __init__(self, g):
  14.         self.df = g.data    # the data-generator function
  15.         self.datalen = g.datalen
  16.         self.stock = '\0' * self.datalen
  17.         self.instock = 0
  18.     #
  19.     #
  20.     #
  21.     def expand(self, nbytes):
  22.         reportnl('Engine().expand(' + `nbytes` + ')')
  23.         #
  24.         # first use up the data that we still have in stock
  25.         #
  26.         if nbytes < self.instock:
  27.             # it's even less than that
  28.             return self._keeprest(nbytes)
  29.         result = self.stock[self.datalen-self.instock:]
  30.         nbytes = nbytes - self.instock
  31.         #
  32.         # self.instock should actually set to zero now, but
  33.         # we won't use it for a short period
  34.         #
  35.         quot, rem = divmod(nbytes, self.datalen)
  36.         result = result + gluetimes(quot, self.df)
  37.         if rem:
  38.             self.stock = self.df()
  39.             self.instock = self.datalen
  40.             return result + self._keeprest(rem)
  41.         self.instock = 0
  42.         return result
  43.     #
  44.     #
  45.     #
  46.     def _keeprest(self, nbytes):
  47.         startval = self.datalen - self.instock
  48.         self.instock = self.instock - self.datalen
  49.         return self.stock[startval:startval+nbytes]
  50.  
  51. def engine(g):
  52.     reportnl('engine(' + `g` + ')')
  53.     return Engine(g)
  54.