宿題があります。私はできる限り進んでいますが、行き詰まっています。誰かが私を正しい方向に向けることができますか....各データ行を新しいオブジェクトにすることに固執しています。通常、行を繰り返すことができると思いますが、それは最後の行のみを返します
質問:
classFactory.pyソースコードを変更して、build_row関数によって返されるDataRowクラスが別のメソッドを持つようにします。
retrieve(self、curs、condition = None)
selfは(通常どおり)メソッドが呼び出されているインスタンスであり、cursは既存のデータベース接続上のデータベースカーソルであり、condition(存在する場合)は、受信したすべての行に当てはまる必要がある条件の文字列です。
取得メソッドはジェネレーターである必要があり、完全に使い果たされるまで結果セットの連続する行を生成します。各行は、DataRowタイプの新しいオブジェクトである必要があります。
これは私が持っているものです------テスト:
import unittest
from classFactory import build_row
class DBTest(unittest.TestCase):
def setUp(self):
C = build_row("user", "id name email")
self.c = C([1, "Steve Holden", "steve@holdenweb.com"])
def test_attributes(self):
self.assertEqual(self.c.id, 1)
self.assertEqual(self.c.name, "Steve Holden")
self.assertEqual(self.c.email, "steve@holdenweb.com")
def test_repr(self):
self.assertEqual(repr(self.c),
"user_record(1, 'Steve Holden', 'steve@holdenweb.com')")
if __name__ == "__main__":
unittest.main()
私がテストしているスクリプト
def build_row(table, cols):
"""Build a class that creates instances of specific rows"""
class DataRow:
"""Generic data row class, specialized by surrounding function"""
def __init__(self, data):
"""Uses data and column names to inject attributes"""
assert len(data)==len(self.cols)
for colname, dat in zip(self.cols, data):
setattr(self, colname, dat)
def __repr__(self):
return "{0}_record({1})".format(self.table, ", ".join([" {0!r}".format(getattr(self, c)) for c in self.cols]))
DataRow.table = table
DataRow.cols = cols.split()
return DataRow