home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / ratelimited.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  3.4 KB  |  110 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import division
  5. import sys
  6. import time
  7. if sys.platform == 'win32':
  8.     default_timer = time.clock
  9. else:
  10.     default_timer = time.time
  11. time = time.time
  12.  
  13. class RateMonitor(object):
  14.     time_threshold = 0.5
  15.     
  16.     def __init__(self, processor):
  17.         self.f_process = processor
  18.         self.bytecounts = []
  19.         self.written = 0
  20.         self._bps = 0
  21.  
  22.     
  23.     def handle_data(self, data):
  24.         self._process(len(data))
  25.         self.f_process(data)
  26.  
  27.     
  28.     def _process(self, num_bytes):
  29.         if num_bytes > 0:
  30.             self._add_byte_data(time(), self.written + num_bytes)
  31.         
  32.  
  33.     
  34.     def bps(self):
  35.         now = time()
  36.         self._add_byte_data(now, self.written)
  37.         if len(self.bytecounts) <= 1:
  38.             self._add_byte_data(now, self.written)
  39.             self._bps = 0
  40.             return self._bps
  41.         (oldest, lowest) = self.bytecounts[0]
  42.         (newest, highest) = self.bytecounts[-1]
  43.         time_diff = newest - oldest
  44.         byte_diff = highest - lowest
  45.         if byte_diff and time_diff > self.time_threshold:
  46.             self._bps = byte_diff / time_diff
  47.         else:
  48.             self._bps = 0
  49.         return self._bps
  50.  
  51.     bps = property(bps)
  52.     
  53.     def _add_byte_data(self, tstamp, bytecount):
  54.         self.written = bytecount
  55.         tstamp = tstamp
  56.         bytecounts = self.bytecounts
  57.         if not bytecounts:
  58.             bytecounts.append((tstamp, bytecount))
  59.         
  60.         (oldtime, oldcount) = bytecounts[-1]
  61.         if oldcount == bytecount:
  62.             bytecounts[-1] = (tstamp, bytecount)
  63.         elif tstamp > oldtime:
  64.             bytecounts.append((tstamp, bytecount))
  65.         elif tstamp == oldtime:
  66.             bytecounts[-1] = (tstamp, bytecount)
  67.         
  68.         now = time()
  69.         while bytecounts and now - bytecounts[0][0] > self.window:
  70.             bytecounts.pop(0)
  71.  
  72.  
  73.  
  74. class RateLimiter(RateMonitor):
  75.     
  76.     def __init__(self, processor, limit, window = 1):
  77.         RateMonitor.__init__(self, processor)
  78.         self.limit = limit
  79.         self.window = window
  80.         self._called_too_fast = False
  81.  
  82.     
  83.     def handle_data(self, data):
  84.         self._process(len(data))
  85.         if self.bps > self.limit:
  86.             if not self._called_too_fast:
  87.                 self.too_fast(data)
  88.                 self._called_too_fast = True
  89.             
  90.             return False
  91.         self.f_process(data)
  92.         self._called_too_fast = False
  93.         return True
  94.  
  95.  
  96. if __name__ == '__main__':
  97.     
  98.     class TooFastPrinter(RateLimiter):
  99.         
  100.         def too_fast(self, data):
  101.             print 'was sending %d bytes too fast!' % len(data), self._bps
  102.  
  103.  
  104.     rl = TooFastPrinter((lambda d: pass), 20480, 1)
  105.     for i in xrange(2048):
  106.         rl.write('a')
  107.     
  108.     print len(rl.bytecounts), rl.bps, rl.written
  109.  
  110.