简体   繁体   English

是否有一种 pythonic 的方式来根据 function 中的 kwargs 来控制流量?

[英]Is there a pythonic way to control the flow depending on kwargs in a function?

i have register_users list that have list of user objects and every user object have password, username, email property i want to create a login function that get a ** kwargs from input.我有register_users列表,其中包含用户对象列表,每个用户 object 都有密码,用户名,email属性我想创建一个登录 function从输入中获取 ** kwarg

the kwargs input can be given to the function in 3 different ways.可以通过 3 种不同的方式将kwargs输入提供给 function。

  1. kwargs may have 3 keys username, password and email . kwargs 可能有 3 个密钥username、password 和 email

  2. kwargs may have 2 keys username or password . kwargs 可能有 2 个密钥username 或 password

  3. kwargs may have email or password . kwargs 可能有email 或密码

in all the above we must check whether there is a user object in the register_user list whose information is the same as the given input or not.综上所述,我们必须检查register_user列表中是否有一个用户 object 的信息与给定的输入相同。

And the question I have is What is the best way to determine how kwargs input is given?and how to do this with minimum use of if, ifelse ?or a pythonic way to do this.我的问题是确定如何给出 kwargs 输入的最佳方法是什么?以及如何在最少使用if、ifelse或 python 方式的情况下做到这一点。

class User:
    def __init__(self, username, password, email):
        self.username = username
        self.email = email
        self.password = password


user1 = User("user1", "user1@test.com", "test12345")
user2 = User("user2", "user2@test.com", "test12345")

register_users = [user1, user2]

def login(**kwargs):
    pass # determine how kwargs is given to function and then check informations

login({"username": "user1", "email": "user1@test.com", "password": "test12345"})
login({"username": "user1", "password": "test12345"})
login({"email": "user1@test.com", "password": "test12345"})

Logically, you always need the password, and either the username or email.从逻辑上讲,您总是需要密码,以及用户名或 email。 A pythonic way is to retrieve these fields from kwargs and take advantage of this to validate the input parameter. pythonic 的方法是从kwargs中检索这些字段并利用它来验证输入参数。 From there you can use getattr to retrieve either the username either the email from the user.从那里您可以使用getattr从用户那里检索用户名 email 。



def login(**kwargs):
     try:
         password = kwargs["password"]
         if "email" in kwargs:
             auth_field, auth_value = "email", kwargs["email"]
         else:
             auth_field, auth_value = "username", kwargs["username"]

     except KeyError:
         raise ValueError("Login requires password and either username or email")

     found_user = None
     for user in registered_users:
         if user.password == password and getattr(user, auth_field) == auth_value:
             found_user = user
             break

I assume that this is just for Stackoverflow, but in a real system you probably won't want to store your users in a list, and actual password checking will be slightly more complicated我假设这仅适用于 Stackoverflow,但在实际系统中,您可能不希望将用户存储在列表中,实际密码检查会稍微复杂一些


EDITED to answer question in comment if we want to check either one of username/email, either both if they are both present如果我们要检查用户名/电子邮件中的任何一个,则编辑以回答评论中的问题,或者两者都存在


def login(**kwargs):
     try:
         password = kwargs["password"]
         auth_fields = {}
         if "email" in kwargs:
             auth_fields["email"] = kwargs["email"]
         if "username" in kwargs:
             auth_fields["username"] = kwargs["username"]

     except KeyError:
         raise ValueError("Login requires password and either username or email")

     found_user = None
     for user in registered_users:
         if user.password == password and all([getattr(user, field) == value for field, value in auth_fields.items()):
             found_user = user
             break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM