# L-34 MCS 275 Wed 9 Apr 2008 : conversation.py

# This script uses threads to simulate a conversation.
# Guests arrive at a party, say a number of sentences,
# and then leave.

from randomwords import RandomSentence
from threading import *
import time
import random

class Guest(Thread):
   """
   Simulation of a conversation at a party.
   """
   def __init__(self,t,n):
      """
      Initializes guest with name t,
      and the total number of sentences.
      """
      Thread.__init__(self,name=t)
      print 'guest ' + t + ' is born'
      self.n = n

   def run(self):
      """
      Guests arrive between 1 and 5 time units.
      After sleeping between 1 and 6 time units,
      they utter a random sentence.
      """
      s = self.getName()
      time.sleep(random.randint(1,5))
      print 'guest ' + s + ' arrives'
      for i in range(0,self.n):
         time.sleep(random.randint(1,6))
         print s + ' says ' + RandomSentence(10,10)
      time.sleep(random.randint(1,5))
      print 'guest ' + s + ' leaves'
     
def main():
   """
   Defines the number of guests
   and starts the simulation.
   """
   n = input('give #guests : ')
   G = []
   k = ord('A')
   for i in range(0,n):
      G.append(Guest(chr(k+i),n))
   print 'starting the conversation'
   for g in G: g.start()
   print 'conversation has started'

if __name__ == "__main__": main()
