2015-10-20 57 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