我在Python中遇到了一个类似于';int';JetBrains PyCharm中的对象不可下标错误
这是我的密码我在Python中遇到了一个类似于';int';JetBrains PyCharm中的对象不可下标错误,python,database,jetbrains-ide,Python,Database,Jetbrains Ide,这是我的密码 def position_list(db,limit=10): """Return a list of positions ordered by date db is a database connection return at most limit positions (default 10) Returns a list of tuples (id, timestamp, owner, title, location, company, d
def position_list(db,limit=10):
"""Return a list of positions ordered by date
db is a database connection
return at most limit positions (default 10)
Returns a list of tuples (id, timestamp, owner, title, location, company, description)
"""
cursor = db.cursor()
sql = "Select id, timestamp, owner, title, description From positions order by timestamp desc limit 10"
cursor.execute(sql)
result =[]
for row in cursor:
result.append(row[0])
result.append(row[1])
result.append(row[2])
result.append(row[3])
result.append(row[4])
return result
这是我必须通过的测试:
def test_position_list(self):
"""Test that position_list returns positions"""
# first get all positions
positions = interface.position_list(self.db, limit=1000)
self.assertEqual(len(self.positions), len(positions))
# validate the returned values
self.assertEqual(1, positions[0][0])
self.assertEqual('2018-03-07 22:36:19', positions[0][1])
# can't check owner as it is randomly assigned
self.assertEqual('Staff Site Reliability Engineer ', positions[0][3])
def test_position_list_limit(self):
"""Test that position_list returns positions using the limit argument"""
# now look at the limit argument
positions = interface.position_list(self.db, limit=3)
self.assertEqual(3, len(positions))
positions = interface.position_list(self.db, limit=1)
self.assertEqual(1, len(positions))
# limit higher than number of positions
positions = interface.position_list(self.db, limit=100)
self.assertEqual(50, len(positions))
当我尝试运行单元测试时,我得到
Type error: 'int' object is not subscriptable.
This is due to the row[0] value of id which is an integer.
我该怎么解决这个问题?。还有人能告诉我如何通过
def test\u position\u list\u limit(self)
?您的position\u list
函数需要返回元组列表。返回单个列表将保证两个测试都失败
类似下面的代码应该可以工作
def position_list(db, limit=10):
cursor = db.cursor()
sql = "Select id, timestamp, owner, title, description From positions order by timestamp desc limit 10"
cursor.execute(sql)
return [row for row in cursor]