Python - 从常量文件中获取属性

时间:2015-10-20 16:44:53

标签: python python-3.x

我有一个需要查询的常量变量文件,我不知道如何去做。 我有一个返回用户名的数据库查询,我需要在常量变量文件中找到匹配的用户名。

该文件如下所示:

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__',我只是不确定如何解决它。

3 个答案:

答案 0 :(得分: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']

答案 1 :(得分: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")

答案 2 :(得分: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