home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / selectpoll.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  2.9 KB  |  142 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. from select import select, error
  5. from time import sleep
  6. from types import IntType
  7. from bisect import bisect
  8. POLLIN = 1
  9. POLLOUT = 2
  10. POLLERR = 8
  11. POLLHUP = 16
  12.  
  13. class poll:
  14.     
  15.     def __init__(self):
  16.         self.rlist = []
  17.         self.wlist = []
  18.  
  19.     
  20.     def register(self, f, t):
  21.         if type(f) != IntType:
  22.             f = f.fileno()
  23.         
  24.         if t & POLLIN != 0:
  25.             insert(self.rlist, f)
  26.         else:
  27.             remove(self.rlist, f)
  28.         if t & POLLOUT != 0:
  29.             insert(self.wlist, f)
  30.         else:
  31.             remove(self.wlist, f)
  32.  
  33.     
  34.     def unregister(self, f):
  35.         if type(f) != IntType:
  36.             f = f.fileno()
  37.         
  38.         remove(self.rlist, f)
  39.         remove(self.wlist, f)
  40.  
  41.     
  42.     def poll(self, timeout = None):
  43.         if self.rlist != [] or self.wlist != []:
  44.             (r, w, e) = select(self.rlist, self.wlist, [], timeout)
  45.         else:
  46.             sleep(timeout)
  47.             return []
  48.         result = []
  49.         for s in r:
  50.             result.append((s, POLLIN))
  51.         
  52.         for s in w:
  53.             result.append((s, POLLOUT))
  54.         
  55.         return result
  56.  
  57.  
  58.  
  59. def remove(list, item):
  60.     i = bisect(list, item)
  61.     if i > 0 and list[i - 1] == item:
  62.         del list[i - 1]
  63.     
  64.  
  65.  
  66. def insert(list, item):
  67.     i = bisect(list, item)
  68.     if i == 0 or list[i - 1] != item:
  69.         list.insert(i, item)
  70.     
  71.  
  72.  
  73. def test_remove():
  74.     x = [
  75.         2,
  76.         4,
  77.         6]
  78.     remove(x, 2)
  79.     x = [
  80.         2,
  81.         4,
  82.         6]
  83.     remove(x, 4)
  84.     x = [
  85.         2,
  86.         4,
  87.         6]
  88.     remove(x, 6)
  89.     x = [
  90.         2,
  91.         4,
  92.         6]
  93.     remove(x, 5)
  94.     x = [
  95.         2,
  96.         4,
  97.         6]
  98.     remove(x, 1)
  99.     x = [
  100.         2,
  101.         4,
  102.         6]
  103.     remove(x, 7)
  104.     x = [
  105.         2,
  106.         4,
  107.         6]
  108.     remove(x, 5)
  109.     x = []
  110.     remove(x, 3)
  111.  
  112.  
  113. def test_insert():
  114.     x = [
  115.         2,
  116.         4]
  117.     insert(x, 1)
  118.     x = [
  119.         2,
  120.         4]
  121.     insert(x, 3)
  122.     x = [
  123.         2,
  124.         4]
  125.     insert(x, 5)
  126.     x = [
  127.         2,
  128.         4]
  129.     insert(x, 2)
  130.     x = [
  131.         2,
  132.         4]
  133.     insert(x, 4)
  134.     x = [
  135.         2,
  136.         3,
  137.         4]
  138.     insert(x, 3)
  139.     x = []
  140.     insert(x, 3)
  141.  
  142.