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.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  3.3 KB  |  207 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (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.     if not x == [
  80.         4,
  81.         6]:
  82.         raise AssertionError
  83.     x = [
  84.         2,
  85.         4,
  86.         6]
  87.     remove(x, 4)
  88.     if not x == [
  89.         2,
  90.         6]:
  91.         raise AssertionError
  92.     x = [
  93.         2,
  94.         4,
  95.         6]
  96.     remove(x, 6)
  97.     if not x == [
  98.         2,
  99.         4]:
  100.         raise AssertionError
  101.     x = [
  102.         2,
  103.         4,
  104.         6]
  105.     remove(x, 5)
  106.     if not x == [
  107.         2,
  108.         4,
  109.         6]:
  110.         raise AssertionError
  111.     x = [
  112.         2,
  113.         4,
  114.         6]
  115.     remove(x, 1)
  116.     if not x == [
  117.         2,
  118.         4,
  119.         6]:
  120.         raise AssertionError
  121.     x = [
  122.         2,
  123.         4,
  124.         6]
  125.     remove(x, 7)
  126.     if not x == [
  127.         2,
  128.         4,
  129.         6]:
  130.         raise AssertionError
  131.     x = [
  132.         2,
  133.         4,
  134.         6]
  135.     remove(x, 5)
  136.     if not x == [
  137.         2,
  138.         4,
  139.         6]:
  140.         raise AssertionError
  141.     x = []
  142.     remove(x, 3)
  143.     if not x == []:
  144.         raise AssertionError
  145.  
  146.  
  147. def test_insert():
  148.     x = [
  149.         2,
  150.         4]
  151.     insert(x, 1)
  152.     if not x == [
  153.         1,
  154.         2,
  155.         4]:
  156.         raise AssertionError
  157.     x = [
  158.         2,
  159.         4]
  160.     insert(x, 3)
  161.     if not x == [
  162.         2,
  163.         3,
  164.         4]:
  165.         raise AssertionError
  166.     x = [
  167.         2,
  168.         4]
  169.     insert(x, 5)
  170.     if not x == [
  171.         2,
  172.         4,
  173.         5]:
  174.         raise AssertionError
  175.     x = [
  176.         2,
  177.         4]
  178.     insert(x, 2)
  179.     if not x == [
  180.         2,
  181.         4]:
  182.         raise AssertionError
  183.     x = [
  184.         2,
  185.         4]
  186.     insert(x, 4)
  187.     if not x == [
  188.         2,
  189.         4]:
  190.         raise AssertionError
  191.     x = [
  192.         2,
  193.         3,
  194.         4]
  195.     insert(x, 3)
  196.     if not x == [
  197.         2,
  198.         3,
  199.         4]:
  200.         raise AssertionError
  201.     x = []
  202.     insert(x, 3)
  203.     if not x == [
  204.         3]:
  205.         raise AssertionError
  206.  
  207.