这样的进口合法还是不推荐?
像这样进行导入是否合法:
from webapp2_extras.appengine.auth.models import User as webapp2.User
我想将该对象称为 webapp2.User,即使从技术上讲它不是。这是因为还有其他名为 User 的对象,所以我可能会将具有此模型的 Users 命名为 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.
"""
感谢您对此的任何回答或评论
Is it legal to make an import like this:
from webapp2_extras.appengine.auth.models import User as webapp2.User
I'd like to refer to the object as webapp2.User even though it technically is not. This is because there are other object named User so I'd proabbly name Users with this model something like webapp2_user as distinct from fbuser (facebook user who comes via "login with facebook") and google users. It seems a good class since it admits connecting your google or facebook other account with this model:
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.
"""
Thank you for any answer or comment about this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是无效语法,python 不允许您在
as webapp2.User
中使用.
,您应该使用as webapp2_User
代替。is invalid syntax, python doesn't allow you to have a
.
inas webapp2.User
you should useas webapp2_User
instead.这既是非法的,也是不推荐的。
如果您希望您的
User
基类在不同的应用程序中使用,您应该继承它并覆盖该应用程序中所需的部分。不要以这种方式导入。It's both illegal and not recommended.
If you'd like your
User
base class to be used in different applications, you should inherit it and override needed parts in THAT app. Do not do importing this way.