2015-10-20 55 views
0

我有一個需要查詢的常量變量文件,我不知道如何去做。 我有一個數據庫查詢返回用戶名,我需要在常量變量的文件中找到匹配的用戶名。Python - 從常量文件獲取屬性

文件看起來是這樣的:

SALES_MANAGER_01 = {"user_name": "BO01", "password": "password", "attend_password": "BO001", 
        "csm_password": "SM001", "employee_num": "BOSM001"} 

只是有一堆用戶只需像上面那樣的。 我的函數如下:

@attr("user_test") 
def test_get_user_for_login(self): 
    application_code = 'BO' 
    user_from_view = self.select_user_for_login(application_code=application_code) 
    users = [d['USER'] for d in user_from_view] 
    user_with_ent = choice(users) 
    user_wo_ent = user_with_ent[-4:] 
    password = "" 
    global_users = dir(gum) 
    for item in global_users: 
     if user_wo_ent not in item.__getattr__("user_name"): 
      user_with_ent = choice(users) 
      user_wo_ent = user_with_ent[-4:] 
     else: 
      password = item.__getattr__("password") 
    print(user_wo_ent, password) 

global_users = dir(gum)是我的常量的文件。所以我知道我做錯了什麼,因爲我得到一個屬性錯誤AttributeError: 'str' object has no attribute '__getattr__',我只是不知道如何去解決它。

回答

0

如果您想比較每個item到您的匹配條件,您應該反轉循環。此外,你有一本字典,所以用它來做一些繁重的工作。

您需要添加一些進口

import re 
from ast import literal_eval 

我已經改變了dir(gum)位是這個功能。

def get_global_users(filename): 
    gusers = {} # create a global users dict 
    p_key = re.compile(ur'\b\w*\b') # regex to get first part, e.g.. SALES_MANAGER_01 
    p_value = re.compile(ur'\{.*\}') # regex to grab everything in {} 
    with (open(filename)) as f: # open the file and work through it 
     for line in f: # for each line 
      gum_key = p_key.match(line) # pull out the key 
      gum_value = p_value.search(line) # pull out the value 
      ''' Here is the real action. update a dictionary 
      with the match of gum_key and with match of gum_value''' 
      gusers[gum_key.group()] = literal_eval(gum_value.group()) 
    return(gusers) # return the dictionary 

您現有代碼的底部被替換爲此。

global_users = get_global_users(gum) # assign return to global_users 
for key, value in global_users.iteritems(): # walk through all key, value pairs 
    if value['user_name'] != user_wo_ent: 
     user_with_ent = choice(users) 
     user_wo_ent = user_with_ent[-4:] 
    else: 
     password = value['password'] 
+0

謝謝,現在我得到一個'TypeError:字符串索引必須是整數',因爲'dir(gum)'行只返回常量名稱而不是值。 – DarthOpto

+0

謝謝@ShawnMehan這看起來好像會起作用。我可以不通過'get_global_users'放置一個.py文件嗎?我現在收到這個錯誤: 'TypeError:invalid file: DarthOpto

+0

DarthOpto,你的問題不斷變形。您需要提出一個新問題或編輯這個問題。如果我已回答您的原始問題,請將其標記爲已接受。 –

0

所以有一個非常簡單的答案是得到常數文件,然後分析了它像這樣的dir

global_users = dir(gum) 
for item in global_users: 
    o = gum.__dict__[item] 
    if type(o) is not dict: 
     continue 
    if gum.__dict__[item].get("user_name") == user_wo_ent: 
     print(user_wo_ent, o.get("password")) 
    else: 
     print("User was not in global_user_mappings") 
0

我能夠找到通過執行以下答案:

def get_user_for_login(application_code='BO'): 
user_from_view = BaseServiceTest().select_user_for_login(application_code=application_code) 
users = [d['USER'] for d in user_from_view] 
user_with_ent = choice(users) 
user_wo_ent = user_with_ent[4:] 
global_users = dir(gum) 
user_dict = {'user_name': '', 'password': ''} 
for item in global_users: 
    o = gum.__dict__[item] 
    if type(o) is not dict: 
     continue 
    if user_wo_ent == o.get("user_name"): 
     user_dict['user_name'] = user_wo_ent 
     user_dict['password'] = o.get("password") 
return user_dict