2014-07-25 88 views
1

我正在研究一個掃描程序,它檢查文件傳輸的進度,該掃描程序在IP地址的醃製列表中進行操作。當一個文件完成時,我希望IP地址從IP的清單列表中移除並移動到單獨的清單中,「完成」。使用IP地址作爲參數的函數調用

開始:

servers = {"10.10.10.1": "", "10.10.10.2": "", "10.10.10.3": ""} 
skeys = servers.keys() 
complete = [] 

def donewith(server): 
    if server in skeys: 
     complete.append("{0}".format(server)) 
     servers.pop("{0}".format(server)) 
     logging.info('Moved {0} to Complete list.'.format(server)) 
    else: 
     logging.error('Unable to move \'{0}\' to Complete list.'.format(server)) 

期望的結果:

donewith(10.10.10.1) 

servers = {"10.10.10.2": "", "10.10.10.3": ""} 
complete = ["10.10.10.1"] 

這是我真正得到。

donewith(10.10.10.1) 
File "<stdin>", line 1 
donewith(10.10.10.1) 
       ^
SyntaxError: invalid syntax 

或者調用帶引號的功能產生TypeError requiring an integer.不太知道如何解決這個問題,因爲它似乎是這樣一個簡單的問題

制定的報價方案:

def check(server): 
    #print "Checking {0}".format(server) 
    logging.info('Fetching {0}.'.format(server)) 
    response = urllib2.urlopen("http://"+server+"/avicapture.html") 
    tall = response.read() # puts the data into a string 
    html = tall.rstrip() 
    match = re.search('.*In Progress \((.*)%\).*', html) 
    if match: 
     temp = match.group(1) 
     results = temp 
     servers[server] = temp 
     if int(temp) >= 98 and int(temp) <= 99: 
      abort(server) 
      alertmail(temp, server) 
      donewith(server) 
      logging.info('{0} completed.'.format(server)) 
     return str(temp) 
    else: 
     logging.error('Unable to find progress for file on {0}.'.format(server)) 
     return None 

這函數調用donewith()函數,如果該函數具有如下引號,則該函數不起作用:donewith("server")

實施例:

def check(server): 
    donewith("server") 

def donewith(server) 
    do_things. 

check(server) 

從而造成..

check(10.10.10.3) 
      ^
SyntaxError: invalid syntax 
與第三組數字零

始終...

+1

你要通過值字符串僅作爲您的報價提。你會提供「TypeError:需要一個整數」的回溯嗎? – Nilesh

+0

請提供從tracback中提供的2-3行全部回溯。 – Nilesh

回答

0

只是爲了跟進。在其他答案中忽略了一個函數不能使用一個IP地址作爲arg調用另一個函數,因爲它使得它不是一個字符串。即使在func中調用func(str(ip))也不起作用。

我採取了不同的策略,並決定針對條目的索引,而不是條目名稱本身。

解決方案:

servers = {"10.10.10.1": "", "10.10.10.2": "", "10.10.10.3": ""} 
skeys = servers.keys() # Creates list of dictionary keys (IP addresses) 
["10.10.10.1", "10.10.10.2", "10.10.10.3"] 

for i, j in enumerate(skeys): # Find the index 
    if j == server: 
     skeys.pop(i) # Pop the index. 

servers = servers.fromkeys(skeys, "") # Recreates server dict from skeys. 
1

的關鍵是一個字符串。你應該這樣做:

donewith('10.10.10.1') 
0

試試這個

In [6]: def donewith(server): 
...:   if server in skeys: 
...:     complete.append(server) 
...:     servers.pop(server) 
...:     logging.info('Moved {0} to Complete list.'.format(server)) 
...:   else: 
...:     logging.error('Unable to move \'{0}\' to Complete list.'.format(server)) 
...: 

In [7]: donewith("10.10.10.1") 

In [8]: 

In [8]: servers 
Out[8]: {'10.10.10.2': '', '10.10.10.3': ''} 
+0

我知道這種方法的工作原理,但問題是donewith(服務器)函數位於另一個函數,其中放置單引號或雙引號不起作用的調用。 – oorahduc

1

你期望的行爲是不可能的,因爲它是,因爲10.10.10.1不是一個字符串,如Python看到點.,它試圖解析它作爲浮點數,但浮點數只能有一個小數點,這就是爲什麼異常指向第二個點之後的數字。

爲了使它工作,你需要通過參數作爲字符串:

donewith("10.10.10.1") 

servers = {"10.10.10.2": "", "10.10.10.3": ""} 
complete = ["10.10.10.1"] 

不僅在donewith,但在check太:

def check(server): # here server is variable name, not a string 
    donewith(server) 

def donewith(server) 
    do_things. 

check(server) # server = "10.10.10.1" 
# or 
check("10.10.10.1") 

,因爲它是蟒蛇是如何工作的,你需要在引號中聲明字符串,否則將被視爲數字(第一個字符是數字)或變量名稱。

相關問題