Thanks for both versions, Kirby! I'll take the applause wherever/however I can get it. ;)

I'm not into COM/Windows much, but the basic example is a neat way to illustrate threading... I'll have to remember to steal it, maybe for the classes I teach to our 8th graders. ;)

Speaking of 8th graders, these days I'm also teaching online Python courses for middle school kids through Northwestern's Gifted Learrning Links program - an intro to Python using Hello World! and (starting in January) an intermediate Python class, which will do more with OOP concepts and GUI's. The link  is here (the intermediate course isn't up yet, but should be soon) - http://www.ctd.northwestern.edu/gll/courses/enrichment/winter2011/#Technology

Cheers,
Vern

On Sat, Dec 11, 2010 at 12:12 AM, kirby urner <kirby.urner@gmail.com> wrote:


Now if I could just turn that into a COM object (seriously, I'm trying to figure
out how to call movingballs2.py, a Vpython script, from Visual FoxPro).



OK, I turned it into a COM object, pretty mickey mouse, yet instructive (at least to me):

VFP = Visual FoxPro and in the middle you'll see how I'm in the interactive shell of that language, and creating this Python object.

Even though control returns to the host language, the threads spawn and do their business, taking their time to fill a randomly named text file in some pre-specified directory.

My thanks to Mark Hammond the one and only for swinging by on comp.lang.python -- more acknowledgment in my blog:


Here's the COM version (pycomsupport dependency not included for brevity)

import threading
from random import choice, randint
import pycomsupport
import os
import time

applause = ["Bravo!\n","Clap Clap\n","Yay!\n","whistle\n","shreek\n"]
laugh = ["chuckle\n", "guffaw\n", "hoot\n", "howl\n", "ROFL\n"]
boo = ["Boo!\n", "Nada mas!\n", "hissss\n"]

class MPSS_Audience ( object ):

   _public_methods_ = [ 'applause_track', 'laugh_track', 'boo_track' ]
   _reg_progid_ = "LoadOpt.Feedback"

   # Use pythoncom.CreateGuid()" to make a new clsid
   _reg_clsid_ = '{BFE032DC-0F31-47CA-A714-7D6AADA41F5C}'

   def __init__(self, ident = None):
       self.logger = ident
       fp = os.path.normpath("C:\Users\Kirby\Documents\Visual FoxPro
Projects\mpss")
       fn = pycomsupport.randomstring(8)+".txt"
       self.output = os.path.join(fp, fn)

   def _noise_threads(self, noise_type):
       hnd = open(self.output, 'w')

       for i in range(10):
           AudiencePerson(hnd, choice(noise_type), randint(0,5)).start()

   def applause_track(self):
       self._noise_threads(applause)

   def laugh_track(self):
       self._noise_threads(laugh)

   def boo_track(self):
       self._noise_threads(boo)


class AudiencePerson( threading.Thread ):

   def __init__(self, output, an, interval):
       self.output = output
       self.audience_noise = an

       self.interval = interval
       super(AudiencePerson, self).__init__()

   def run(self):
       for i in range(5):
           self.output.write(self.getName() + ": " + self.audience_noise)

           time.sleep(self.interval)

def testme():
   loObj = MPSS_Audience(1)
   loObj.laugh_track()

if __name__ == "__main__":
   print "Registering COM server..."
   import win32com.server.register
   win32com.server.register.UseCommandLine(MPSS_Audience)
   testme()


On the VFP side:

loCircus = CREATEOBJECT("Loadopt.Feedback", "some param")
loCircus.applause_track()

control returns immediately, even though the text file
takes quite awhile to fill, thanks to the several second
delays between thread writes.  The final file:

Thread-1: whistle
Thread-2: shreek
Thread-3: shreek
Thread-4: whistle
Thread-5: Yay!
Thread-6: whistle
Thread-7: Yay!
Thread-7: Yay!
Thread-7: Yay!
Thread-7: Yay!
Thread-7: Yay!
Thread-8: Clap Clap
Thread-9: Clap Clap
Thread-9: Clap Clap
Thread-9: Clap Clap
Thread-9: Clap Clap
Thread-9: Clap Clap
Thread-10: Clap Clap
Thread-2: shreek
Thread-8: Clap Clap
Thread-2: shreek
Thread-8: Clap Clap
Thread-2: shreek
Thread-4: whistle
Thread-5: Yay!
Thread-6: whistle
Thread-8: Clap Clap
Thread-1: whistle
Thread-2: shreek
Thread-3: shreek
Thread-8: Clap Clap
Thread-10: Clap Clap
Thread-4: whistle
Thread-5: Yay!
Thread-6: whistle
Thread-1: whistle
Thread-3: shreek
Thread-4: whistle
Thread-5: Yay!
Thread-6: whistle
Thread-10: Clap Clap
Thread-1: whistle
Thread-4: whistle
Thread-3: shreek
Thread-5: Yay!
Thread-6: whistle
Thread-10: Clap Clap
Thread-1: whistle
Thread-3: shreek
Thread-10: Clap Clap

Kirby




--
Vern Ceder
vceder@gmail.com, vceder@dogsinmotion.com
The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW