Python loop function

This is the place for queries that don't fit in any of the other categories.

Python loop function

Postby spikeinc » Tue Aug 13, 2013 9:38 am

Hi,

I am new to python, more familiar with perl. I want to write a basic code that will loop through a function indefinitely every one hour.I tried to write the below code but it doesnt seem to work. Can someone help me?

Code: Select all
while var True :
def main():       
    connect("10.1.1.1")
     return 0
localtime = time.localtime(time.time())
sleeptime = (60 * ((19 - localtime) % 60));
print localtime
time.sleep 60

def connect(ip) :
#my function to connect


Basically I want the function connect() to be called every one hour. its kinda strange with python, am used to braces to begin and end a loop, but here its just a : :(
Last edited by stranac on Tue Aug 13, 2013 12:18 pm, edited 1 time in total.
Reason: Added code tags
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am

Re: Python loop function

Postby stranac » Tue Aug 13, 2013 12:27 pm

There are a few reasons it's not working.

Code: Select all
while var True :
is invalid syntax. I'm guessing you wanted
Code: Select all
while True:

And your indentation is totally fucked up.
Last edited by stranac on Wed Aug 14, 2013 11:27 am, edited 2 times in total.
Reason: "fucked up" is a correct description of the op's indentation, no reason to censor it
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1135
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python loop function

Postby spikeinc » Tue Aug 13, 2013 10:27 pm

This doesnt work either :( I know its an indent error but how do you actually go about it?
I miss braces :D

Code: Select all
while True:
     def main():
      tcp_connection_status("10.219.136.2")
      return 0
    localtime = time.localtime(time.time())
    sleeptime = (60 * ((19 - localtime) % 60));
    print localtime
    time.sleep 60


Code: Select all
C:\Users\aashraff\Documents\Scripts\SNMP Script\python>tcp.py
  File "C:\Users\aashraff\Documents\Scripts\SNMP Script\python\tcp.py", line 17
    localtime = time.localtime(time.time())
                                          ^
IndentationError: unindent does not match any outer indentation level
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am

Re: Python loop function

Postby Yoriz » Tue Aug 13, 2013 10:41 pm

What are you trying to do here?
which parts are suposed to be looping?
Why do you want to repeatedly define main inside a loop?
Code: Select all
time.sleep 60
should be
Code: Select all
time.sleep(60)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Python loop function

Postby micseydel » Tue Aug 13, 2013 10:57 pm

Languages that use braces typically use the same indentation as Python, just by convention rather than by syntax, so if Python's indentation instead of braces isn't intuitive to you, you were probably writing ugly code in that other language.

The indentation issue you have here, on top of the logical issues that Yoriz mentioned, is equivalent to having a close brace somewhere for no reason. "def main()" is indented legally, and then you indent the contents of it properly, and then when you dedent to exit the function you dedent too much.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1295
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python loop function

Postby spikeinc » Wed Aug 14, 2013 12:18 am

Thanks for the explanation Yoruz and micseydel!

Infact what I am trying to do is as below:
Code: Select all
while True
{
      call function tcp_status();
      sleeptime #now sleep for 60 minutes... this part runs the script For example, every hour at x:19 (like 10:19, 11:19, 12:19, 1:19...) irrespective of when I run it. Like if I run it at 10:02, the function should be called at 10:19.
      print time
      #loop back
}

Should I have the while loop inside the def main function? I only want to invoke the tcp_connection_status() function (with the return).
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am

Re: Python loop function

Postby micseydel » Wed Aug 14, 2013 1:42 am

I'm guessing from your other thread that you've figured it out reasonably well?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1295
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests