是否合法导入或不推荐?

时间:2011-12-17 13:31:23

标签: python webapp2

进行这样的导入是否合法:

from webapp2_extras.appengine.auth.models import User as webapp2.User

我想将该对象称为webapp2.User,即使它在技术上不是。这是因为还有其他一个名为User的对象,所以我想用这个模型命名用户,比如webapp2_user不同于fbuser(facebook用户通过“登录facebook”)和google用户。它似乎是一个很好的课程,因为它承认连接你的谷歌或Facebook其他帐户与此模型:

class User(object):

    def get_id(self):
        """Returns this user's unique ID, which can be an integer or string."""

    @classmethod
    def get_by_auth_token(cls, user_id, token):
        """Returns a user object based on a user ID and token.

        :param user_id:
            The user_id of the requesting user.
        :param token:
            The token string to be verified.
        :returns:
            A tuple ``(User, timestamp)``, with a user object and
            the token timestamp, or ``(None, None)`` if both were not found.
        """

    @classmethod
    def get_by_auth_password(cls, auth_id, password):
        """Returns a user object, validating password.

        :param auth_id:
            Authentication id.
        :param password:
            Password to be checked.
        :returns:
            A user object, if found and password matches.
        :raises:
            ``auth.InvalidAuthIdError`` or ``auth.InvalidPasswordError``.
        """

    @classmethod
    def create_auth_token(cls, user_id):
        """Creates a new authorization token for a given user ID.

        :param user_id:
            User unique ID.
        :returns:
            A string with the authorization token.
        """

    @classmethod
    def delete_auth_token(cls, user_id, token):
        """Deletes a given authorization token.

        :param user_id:
            User unique ID.
        :param token:
            A string with the authorization token.
        """

感谢您对此有任何回答或评论

2 个答案:

答案 0 :(得分:2)

from webapp2_extras.appengine.auth.models import User as webapp2.User

语法无效,python不允许您在.中使用as webapp2.User,而应使用as webapp2_User

答案 1 :(得分:1)

这既违法又不推荐。

如果您希望在不同的应用程序中使用User基类,则应该继承它并覆盖该应用程序中所需的部分。不要这样导入。