PythonCard timer/thread tutorial

S

Sponge Nebson

Hello all,

This is my first post. Nice to meet you all! Could one of you walk me
through this code?

def myThread(*argtuple):
"""
A little thread we've added
"""
print "myThread: entered"
q = argtuple[0]
print "myThread: starting loop"
x = 10
while True:
time.sleep(10) # time unit is seconds
print "myThread x=%d" % x
q.put(str(x)) # stick something on message queue
wx.WakeUpIdle() # triggers 'idle' handlers
x += 10

It is from David McNab and Alex Tweedly's tutorial on timers and
threads, which can be found here:

http://pythoncard.sourceforge.net/timers-threads.html

Among my questions are:
""" A little thread we've added""" seems to be an isolated string. It
does not seem to be doing anything there, almost like a comment. Why
is it there?

What is argtuple for? how does it work?

What is the queue for?

Thanks!

-Ben
 
M

Mike Driscoll

Hello all,

This is my first post. Nice to meet you all! Could one of you walk me
through this code?

   def myThread(*argtuple):
        """
        A little thread we've added
        """
        print "myThread: entered"
        q = argtuple[0]
        print "myThread: starting loop"
        x = 10
        while True:
            time.sleep(10) # time unit is seconds
            print "myThread x=%d" % x
            q.put(str(x)) # stick something on message queue
            wx.WakeUpIdle() # triggers 'idle' handlers
            x += 10

It is from David McNab and Alex Tweedly's tutorial on timers and
threads, which can be found here:

 http://pythoncard.sourceforge.net/timers-threads.html

Among my questions are:
""" A little thread we've added""" seems to be an isolated string. It
does not seem to be doing anything there, almost like a comment. Why
is it there?


That's what some people call a doc string. It is like a comment in
that it helps the user know what the function is for. Notice the
triple quotes. If you were to type "help(myFunction)", it would grab
the functions doc string and display it. You can read up on them here:

http://diveintopython.org/getting_to_know_python/documenting_functions.html

What is argtuple for? how does it work?

This allows the programmer to pass an arbitrarily long argument list
to the function. It took a little digging, but I found it in the docs
(scroll down towards the bottom):

http://docs.python.org/tutorial/controlflow.html#SECTION006600000000000000000

What is the queue for?

Thanks!

-Ben

I haven't messed with queues as yet, but they are one way of dealing
with multiple threads in GUI programming. The idea is to stick
something in the queue for the GUI thread (or potentially some other
thread) to pick up when it's not busy. So one thread sticks something
on the queue and another thread checks the queue periodically to see
if there's something there and if there is, it picks it up. At least,
that's my understanding. You can read up on various methods of messing
with threads in wxPython here:

http://wiki.wxpython.org/LongRunningTasks

And here are the queue docs (for 2.6...): http://docs.python.org/library/queue.html

I recommend learning how to use Google effectively. I found about half
the links above using it. You might also find joining the wxPython
mailing list beneficial. I learn a lot there just by reading and
posting to it: http://wxpython.org/maillist.php

- Mike
 
G

Gabriel Genellina

This is my first post. Nice to meet you all! Could one of you walk me
through this code?
It is from David McNab and Alex Tweedly's tutorial on timers and
threads, which can be found here:

Mike Driscoll has already answered your questions very well; I'd say that
before going into those topics, you would benefit from reading the Python
tutorial http://docs.python.org/tutorial/ or the "Dive into Python" book
http://www.diveintopython.org/
 
S

Steven D'Aprano

That's what some people call a doc string.

Other people call it "Maurice". *wink*

A doc[umentation] string is the accepted name for it. Anytime a function,
class or module starts immediately with a loan string, that string is
stored by the Python compiler in an attribute __doc__. E.g.:
.... "This is a doc string."
.... return "Norwegian Blue"
....'This is a doc string.'

It is like a comment in that
it helps the user know what the function is for. Notice the triple
quotes.

Triple quotes are a red herring. Any string literal will do.
 
M

Mike Driscoll

That's what some people call a doc string.

Other people call it "Maurice". *wink*

A doc[umentation] string is the accepted name for it. Anytime a function,
class or module starts immediately with a loan string, that string is
stored by the Python compiler in an attribute __doc__. E.g.:

...     "This is a doc string."
...     return "Norwegian Blue"
...>>> parrot.__doc__

'This is a doc string.'
It is like a comment in that
it helps the user know what the function is for. Notice the triple
quotes.

Triple quotes are a red herring. Any string literal will do.

Thanks for clearing that up. I suppose that's more of a recommended
style choice than a requirement. I'm pretty sure some of the Python
books I've read have implied that the triple quotes matter...oh well.
Live and learn. Merry Christmas!

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top