Advertisement
homer512

py lock

Apr 5th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import threading
  2. from time import sleep
  3. from random import randint
  4. import string
  5.  
  6. class Bouncer (threading.Thread):
  7.     def __init__ (self):
  8.         threading.Thread.__init__ (self)
  9.         self.max = 59 #Max width of the screen
  10.         self.sleeptime = 50 #MS delay. Sets wave speed
  11.         self.sleeplock = threading.Lock ()
  12.         self.chars = string.ascii_letters + string.digits + string.punctuation
  13.  
  14.     def rand_char (self):
  15.         return self.chars [randint (0, len (self.chars) - 1)]
  16.  
  17.     def set_rate (self, nvalue):
  18.         try:
  19.             nvalue = int (nvalue)
  20.         except ValueError as e:
  21.             nvalue = 2000
  22.             print ("Incorrect input type:", e)
  23.         with self.sleeplock:
  24.             self.sleeptime = nvalue
  25.  
  26.     def wave (self):
  27.         pad = 0
  28.         dir = 'r'
  29.         padchar = ' '
  30.         while True:
  31.             if dir == 'l':
  32.                 pad += -1
  33.             elif dir =='r':
  34.                 pad += 1
  35.  
  36.             if randint (0, 1000) == 0:
  37.                 padchar = '!'
  38.  
  39.             yield (padchar * pad)
  40.  
  41.             if pad > self.max:
  42.                 dir = 'l'
  43.             elif pad < 0:
  44.                 dir = 'r'
  45.             padchar = ' '
  46.  
  47.     def run (self):
  48.         for p in self.wave ():
  49.             print (p + self.rand_char ())
  50.             with self.sleeplock:
  51.                 sleeptime = self.sleeptime
  52.             sleep (sleeptime / 1000)
  53.  
  54. wave = Bouncer ()
  55. wave.start ()
  56.  
  57. while True:
  58.     wave.set_rate (input (""))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement