[Edu-sig] Python in the news...

kirby urner kirby.urner at gmail.com
Sat Dec 11 06:12:06 CET 2010


>
>
>
> 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:

http://mybizmo.blogspot.com/2010/12/office-work.html

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20101210/b06e980f/attachment.html>


More information about the Edu-sig mailing list