LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2012, 04:07 PM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
python: compare old and new in a loop


Without much formal programming, a bit of gleaning, trial, and error I've managed to mash together a daemon that is quite useful to me (BTW, just yesterday this involved a cron job and 2 shell scripts :-)). Anyhow, this has two functional problems for me:

1. How can I avoid 'the addrs are different' on first run without using a counter?
2. My logic is off but how can I store value of former loop in myipaddr_then in order
to do a compare with myipaddr_now ?

For initial testing, I can change b.txt at any time. The host, my.example.com is my server.



Code:
import time, os, sys
from daemon import runner

class App:
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
    def run(self):

        myipaddr_then = 0

        while True:

         # myipaddr_now = os.popen('curl -s http://icanhazip.com').read()
         myipaddr_now = os.popen('curl -s http://my.example.com/b.txt').read()
         myipaddr_now = myipaddr_now.rstrip()

         if myipaddr_now == myipaddr_then:
          # print "ok"
          return
         else:
          print "the addrs are different"

         time.sleep(10)

         myipaddr_then = myipaddr_now

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
 
Old 03-23-2012, 04:53 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by ezekieldas
1. How can I avoid 'the addrs are different' on first run without using a counter?
You need to use a special value (or "flag") to indicate an initial state. You cannot avoid it. Whether it be a counter or something else. For your particular code, a good should-never-be-used-except-for-initialization Python value would be None. For instance, in your class initialization:
Code:
class App:
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
        self.myipaddr_then = None
Then you utilize it in your "run" method like so:
Code:
    def run(self):

        while True:

         # myipaddr_now = os.popen('curl -s http://icanhazip.com').read()
         myipaddr_now = os.popen('curl -s http://my.example.com/b.txt').read()
         myipaddr_now = myipaddr_now.rstrip()

         if self.myipaddr_then != None and myipaddr_now != myipaddr_then:
          print "the addrs are different"
         else:
          # print "ok"
          pass

         time.sleep(10)

         self.myipaddr_then = myipaddr_now
The above code should also address your second question. It eliminated the overwrite-with-0 at the beginning of the loop.

EDIT:
There was a slight problem with my original post. I had to rearrange the conditional. I changed "return" to "pass" assuming you meant to continue the loop regardless if the addresses are the same. If I recall, pass is an empty statement, which would allow you avoid a syntax error for an empty else clause.

Last edited by Dark_Helmet; 03-23-2012 at 05:00 PM.
 
1 members found this post helpful.
Old 03-23-2012, 09:43 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Dark_Helmet -- thanks for your help. This got me to completion of the project.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python loop JAPANBOY Programming 6 07-02-2011 10:13 AM
How do I take a list from a file and use it in a for loop in python... trist007 Programming 3 05-11-2009 09:00 PM
Python, breaking out of infinite loop edM Programming 7 05-05-2009 11:27 PM
python: can I see which iteration I'm on in a loop? BrianK Programming 2 08-27-2008 09:01 PM
Can you compare function return's in a 'while' loop? Tarts Programming 11 08-06-2003 07:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration