[Tutor] Making a function run every second.

Mic o0MB0o at hotmail.se
Tue Nov 29 17:01:50 CET 2011


Mic wrote:

>> I want a function to run every second , how do I do that?
>>
>> Say that the function look like this:
>>
>> def hi():
>>     print("hi")


>For a script that uses tkinter there's the after() method.
>Example:

>root = Tk()

>def hi():
    >print("ho")

>def hi_reschedule():
   > hi()
    ># make tkinter call it again after 1000 milliseconds
    >root.after(1000, hi_reschedule)
>hi_reschedule() # call it manually the first time


>http://infohost.nmt.edu/tcc/help/pubs/tkinter/universal.html

Nice!

Thank you!




------------------------------

Message: 3
Date: Tue, 29 Nov 2011 10:33:41 -0500
From: Dave Angel <d at davea.name>
To: bodsda at googlemail.com
Cc: Tutor - python List <tutor at python.org>, Mic <o0MB0o at hotmail.se>
Subject: Re: [Tutor] Making a function run every second.
Message-ID: <4ED4FB55.1000201 at davea.name>
Content-Type: text/plain; charset=windows-1252; format=flowed

(You put your response in the wrong place;  it belongs after the part
you're quoting.)
he
On 11/29/2011 10:19 AM, bodsda at googlemail.com wrote:
> You won't get it exactly on because the time it takes to call the function 
> will affect your trigger time.
>
> I would use something like an infinite loop with a 1 second sleep after 
> the function call
>
> Bodsda
> Sent from my BlackBerry? wireless device
>
> -----Original Message-----
> From: "Mic"<o0MB0o at hotmail.se>
> Sender: tutor-bounces+bodsda=googlemail.com at python.org
> Date: Tue, 29 Nov 2011 15:54:59
> To:<tutor at python.org>
> Subject: [Tutor] Making a function run every second.
>
> Hi
>
> I want a function to run every second , how do I do that?
>
> Say that the function look like this:
>
> def hi():
>      print("hi")
>
>
> Thanks!
>
>
> Mic
Without a clearer spec, there are too many possible answers.  As Bobsda
says, you can't get it exactly one second apart.  But you also have the
problem of whether a drift is okay.  For example, if you have a global
you're incrementing each time that function runs, and you want it to
represent the total time the program has been running, then a simple
sleep() is totally wrong.

What I was concerned about is that perhaps this is for one of the
tkinter programs Mic is writing.  For an event-driven program, sleep()
calls of even a second are unaccepable.  And if you literally write a
while loop like that, your whole program would stop responding.

So Mik:
Tell us more about the real requirements.  Is drift acceptable, is this
a console program or some gui environment, are there any other hidden
assumptions?

-- 

DaveA



------------------------------

Message: 4
Date: Tue, 29 Nov 2011 16:35:22 +0100
From: "Mic" <o0MB0o at hotmail.se>
To: <d at davea.name>
Cc: tutor at python.org
Subject: Re: [Tutor] How to handle try and except in this case
Message-ID: <COL124-DS245E361A018AE1AAB47ABEB7B30 at phx.gbl>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=response



-----Ursprungligt meddelande----- 
From: Dave Angel
Sent: Tuesday, November 29, 2011 4:25 PM
To: Mic
Cc: tutor at python.org
Subject: Re: [Tutor] How to handle try and except in this case

>(You top-posted again.  Try to put your remarks AFTER the part you're
>quoting, so that the message is self-descriptive)

I did? I thought I posted this:

"""

>Could you explain what's unclear about it?  Andreas couldn't get more
>specific, since you didn't say how these 10 names are provided.


Yes, it was probably my fault. Sorry about that. I have come up with a new
way of working around
a problem in my main program so I only need two files to be tested if they
exist instead of say 10 files. """


Is that top posting?

>The os.path.exists(filename) returns a boolean, and doesn't tie up
>resources, so you are welcome to use it in an if expression.

>if not (os.path.exists(file1) and os.path.exists(file2)):
      >print "hi"


Thank you! What do you mean with that it "doesn't tie up resources"?


Mic


------------------------------

Message: 5
Date: Mon, 28 Nov 2011 09:47:06 -0500
From: Max gmail <maxskywalker1 at gmail.com>
To: surya k <suryak at live.com>
Cc: Python Tutor <tutor at python.org>
Subject: Re: [Tutor] why doesn't python show error
Message-ID: <B88E173D-D050-4647-9413-C9F7E54CEF9A at gmail.com>
Content-Type: text/plain; charset=iso-8859-1

In some cases, it is a useful fact that Python only shows error messages 
when they are encountered.  For example, I can test a program while keeping 
away from an area that still doesn't work, rather than having to make it 
work flawlessly before my first test.

Python *can* generate executables with py2exe, though if you use Python 3 
you'll need to learn to convert your code to Python 2.  Or, as Blender does, 
you could include Python in the download of your program, so that the user 
installs both your program and Python.

On Nov 28, 2011, at 4:53 AM, surya k wrote:

>
> 1. Why doesn't python show error(description given below) at the beginning 
> when we use functions which aren't present in the standard modules...
>
> Example:
>
> TheString = raw_input('enter a string')lengthofStr = strlen(TheString)Look 
> closely, I used a wrong function to find length of the string. [ strlen( ) 
> belongs to C ].When I run the program, it didn't show any error but when 
> entered input, it then showed up!.Why python doesn't show error at the 
> beginning just like C does?2. Why doesn't python create executable file 
> (.exe ) when we run the code.. If this doesn't do, how can I share my 
> program.. does everyone need to have python to check others code and know 
> what it does?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



------------------------------

Message: 6
Date: Tue, 29 Nov 2011 10:48:00 -0500
From: Dave Angel <d at davea.name>
To: Mic <o0MB0o at hotmail.se>
Cc: tutor at python.org
Subject: Re: [Tutor] How to handle try and except in this case
Message-ID: <4ED4FEB0.60708 at davea.name>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 11/29/2011 10:35 AM, Mic wrote:
>
>
> -----Ursprungligt meddelande----- From: Dave Angel Sent: Tuesday,
> November 29, 2011 4:25 PM To: Mic Cc: tutor at python.org Subject: Re:
> [Tutor] How to handle try and except in this case
>> (You top-posted again. Try to put your remarks AFTER the part you're
>> quoting, so that the message is self-descriptive)
>
>   <SNIP  - a perfectly reasonable message>
>
> Is that top posting?
>

No, you're absolutely right.  I don't know what I was looking at.  Maybe
I had your message scrolled when I was reading it.  I am sorry.

>> The os.path.exists(filename) returns a boolean, and doesn't tie up
>> resources, so you are welcome to use it in an if expression.
>
>> if not (os.path.exists(file1) and os.path.exists(file2)):
>  >print "hi"
>
>
> Thank you! What do you mean with that it "doesn't tie up resources"?

If you do multiple opens in the same expression, you can't easily*
capture all the file objects, especially in the case of an exception.
So there may be files left open that will get closed an indeterminate
amount of time in the future.  That sort of thing is probably acceptable
if the program is short-lived, but even then you can run into
file-sharing conflicts.  Note that current CPython versions are pretty
good about closing files if you don't save the object, but other
versions might not get around to it for a while.

>
>
> Mic
>

* (you can partially solve this if you put the open inside a list
comprehension or equivalent, but exceptions still rear their ugly heads)

-- 

DaveA


------------------------------

Message: 7
Date: Tue, 29 Nov 2011 16:52:47 +0100
From: "Mic" <o0MB0o at hotmail.se>
To: <d at davea.name>, <bodsda at googlemail.com>
Cc: Tutor - python List <tutor at python.org>
Subject: Re: [Tutor] Making a function run every second.
Message-ID: <COL124-DS25EEF909CC9C089BFE0401B7B30 at phx.gbl>
Content-Type: text/plain; format=flowed; charset="Windows-1252";
reply-type=response



-----Ursprungligt meddelande----- 
From: Dave Angel
Sent: Tuesday, November 29, 2011 4:33 PM
To: bodsda at googlemail.com
Cc: Mic ; Tutor - python List
Subject: Re: [Tutor] Making a function run every second.

>(You put your response in the wrong place;  it belongs after the part
you're quoting.)

Okay, thanks.


On 11/29/2011 10:19 AM, bodsda at googlemail.com wrote:
>> You won't get it exactly on because the time it takes to call the
>> function will affect your trigger time.
>>
>> I would use something like an infinite loop with a 1 second sleep after
>> the function call

>> Bodsda
> Sent from my BlackBerry? wireless device
>
>>> -----Original Message-----
>>> From: "Mic"<o0MB0o at hotmail.se>
>>> Sender: tutor-bounces+bodsda=googlemail.com at python.org
>>> Date: Tue, 29 Nov 2011 15:54:59
>>> To:<tutor at python.org>
>>> Subject: [Tutor] Making a function run every second.

>>> Hi

>>> I want a function to run every second , how do I do that?
>
>>> Say that the function look like this:
>
>>> def hi():
>      print("hi")

>Without a clearer spec, there are too many possible answers.  As Bobsda
>says, you can't get it exactly one second apart.  But you also have the
>problem of whether a drift is okay.  For example, if you have a global
>you're incrementing each time that function runs, and you want it to
>represent the total time the program has been running, then a simple
>sleep() is totally wrong.

Okay, I undestand. Hmm, what is a drift?

>What I was concerned about is that perhaps this is for one of the tkinter
>programs Mic is writing.  For an event-driven program, sleep() calls of
>even a second are unaccepable.  And if you literally write a while loop
>like that, your whole program would stop responding.

Yes, this is one of my tkinter programs :) Why would the program stop
responding using a sleep()?

>So Mik:
>Tell us more about the real requirements.  Is drift acceptable, is this a
>console program or some gui environment, are there any other hidden
>assumptions?

Okay, this function approximately runs every second and check what the time
is.
(Well, every 30 second or every minute would be okay if that makes it
easier)
If the time is 15:00 it is supposed to remove a file. That should be all
requirements!

I have figured out how to make the function check what time it is and how to
remove the file when the time is 15:00, but I don't know how to make it run
every
second or something like that.


Thanks!

Mic



------------------------------

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 93, Issue 173
************************************** 



More information about the Tutor mailing list