gpt4 book ai didi

Python 类在实例化时缺少参数

转载 作者:行者123 更新时间:2023-12-01 01:03:45 25 4
gpt4 key购买 nike

我有此类用户,它根据用户名从 sqlite 数据库文件中提取值。我已经仔细检查过,并且我的光标执行检索了正确的元组,但是在 User 类的实例化中似乎出现了错误。

这是我的文件

用户.py

import sqlite3


class User:
# _id becuase id is a reserved keyword
def __init__(self, _id, username, password):
self.id = _id
self.username = username
self.password = password

# added this since self wasn't used and we called User instantiation
@classmethod
def find_by_username(cls, username):
connection = sqlite3.connect("data.db")
cursor = connection.cursor()

query = "SELECT * FROM users WHERE username=?"
# execute has to be in the form of a tuple
result = cursor.execute(query, (username,))
# gets the first result in the set , and if empty returns None
row = result.fetchone()
if row:
# this expands into row[0],row[1],row[2]
user = cls(*row)
else:
user = None

connection.close()
return user

@classmethod
def find_by_id(cls, _id):
connection = sqlite3.connect("data.db")
cursor = connection.cursor()

query = "SELECT * FROM users WHERE id=?"

row = cursor.execute(query, (_id,))
if row:
user = cls(*row)
else:
user = None

connection.close()
return user

安全.py

from werkzeug.security import safe_str_cmp
from user import User


def authenticate(username, password):
user = User.find_by_username(username)
if user and safe_str_cmp(user.password, password):
return user


def identity(payload):
user_id = payload["identity"]
return User.find_by_id(user_id)

我已经验证 User.py 中的数据库调用返回正确的元组(id、“用户名”、“密码”)。这是我得到的堆栈跟踪:

 Traceback (most recent call last):
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_jwt/__init__.py", line 176, in decorator
_jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_jwt/__init__.py", line 162, in _jwt_required
_request_ctx_stack.top.current_identity = identity = _jwt.identity_callback(payload)
File "not_importtant_path/code/security.py", line 13, in identity
return User.find_by_id(user_id)
File "not_importtant_path/code/user.py", line 40, in find_by_id
user = cls(*row)
TypeError: __init__() missing 2 required positional arguments: 'username' and 'password'

最佳答案

您需要在 find_by_id 类方法中的 cursor.execute() 之后执行 fetchone。您在 find_by_username 类方法中正确执行了此操作。

关于Python 类在实例化时缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com