#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    Searching each line a few lines ahead


    I have been working on a small script that helps me connect to folding@home's stats and get some data on a cetain team

    right now i am just messing around with it, so i know the code isn't at its greatests, there will be lots more funcionality later. I am just looking for a better way of handling this:

    Code:
    #!/usr/local/bin/python2.3
    
    import urllib2
    import re
    import sys
    
    rankCPU = re.compile("[0-9]+")
    rankScore = re.compile(r"[0-9]+.+[0-9]+")
    teamRank = re.compile(r"[0-9]+ of [0-9]+")
    #http://vspx27.stanford.edu/cgi-bin/main.py?qtype=teampage&teamnum=41053
    #http://tycho.usno.navy.mil/cgi-bin/timer.pl
    print "GeneralGurus' folding@home Stats reporter"
    print "--------------------------------------"
    print "\n"
    print "looking up Stats on Team 41053"
    print "\n"
    
    
    work = 0
    cpus = 0
    total = 0
    rank = 0
    for line in urllib2.urlopen('http://vspx27.stanford.edu/cgi-bin/main.py?qtype=teampage&teamnum=41053'):
        if cpus == 1:
           searchCPU = rankCPU.search(line)
           print "Active CPUs: " + searchCPU.group()
           cpus = 0
        elif "Active CPUs within 50 days" in line:
             cpus = 1
    
        if total == 1:
           total = 2
        elif total == 2:
             searchGS = rankScore.search(line)
             print "Grand Score: " + searchGS.group()
             total = 0
        elif "Grand Score" in line:
             total = 1
    
        if work == 1:
           work = 2
        elif work == 2:
             searchWU = rankScore.search(line)
             print "Work Units: " + searchWU.group()
             work = 0
        elif 'Work Unit Count' in line:
           work = 1
    
        if rank == 1:
           searchTeamRank = teamRank.search(line)
           print "Team Rank: " + searchTeamRank.group()
           rank = 0
        elif "Team Rank" in line:
             rank = 1
    
    
    
    
    
    
    
    
    blah = raw_input("Press enter(return) to exit")
    Well in that, what i am doing is just looking for the closes uniqu marker and telling python to go a few lines ahead. Well i wish there was a way i could say go this meny lines ahead of where you are and do this to it, then come back to the next line of where you left. I am not sure if this is even possible, if not then is there a way to make my current code a little bit simpler than that or maybe more optimized?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    What about this?
    Code:
    lines = urllib2.urlopen('http://vspx27.stanford.edu/...')
    
    for line_num in range(len(lines)):
      if cpus == 1:
        searchCPU = rankCPU.search(lines[line_num])
        # three lines ahead
        lines[line_num + 3]
        # .... do something
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Sweet, i never thought of doing that, thanks. That works perfect.

IMN logo majestic logo threadwatch logo seochat tools logo