【问题标题】:Is importing like this legal or not recommended?像这样进口是合法的还是不推荐的?
【发布时间】:2011-12-17 13:31:23
【问题描述】:

这样导入是否合法:

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.
        """

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

【问题讨论】:

    标签: python webapp2


    【解决方案1】:
    from webapp2_extras.appengine.auth.models import User as webapp2.User
    

    语法无效,python 不允许您在 as webapp2.User 中有 .,您应该改用 as webapp2_User

    【讨论】:

      【解决方案2】:

      这既违法也不推荐。

      如果您希望您的 User 基类用于不同的应用程序,您应该继承它并覆盖该应用程序中所需的部分。不要以这种方式导入。

      【讨论】:

      • 感谢您的回答超出了我的要求。基类来自 webapp2 API,我正在学习如何使用它,因为它似乎具有内置的散列和加盐密码,这是不必实现密码散列和加盐的一大优势。
      猜你喜欢
      • 2013-05-26
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2015-04-21
      相关资源
      最近更新 更多