我有一个需要查询的常量变量文件,我不知道如何去做。 我有一个数据库查询返回用户名,我需要在常量变量的文件中找到匹配的用户名。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__'
,我只是不知道如何去解决它。
谢谢,现在我得到一个'TypeError:字符串索引必须是整数',因为'dir(gum)'行只返回常量名称而不是值。 – DarthOpto
谢谢@ShawnMehan这看起来好像会起作用。我可以不通过'get_global_users'放置一个.py文件吗?我现在收到这个错误: 'TypeError:invalid file: –
DarthOpto
DarthOpto,你的问题不断变形。您需要提出一个新问题或编辑这个问题。如果我已回答您的原始问题,请将其标记为已接受。 –