From 50310719970c6a669e876ac3d33562e899879409 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Mon, 13 Nov 2006 18:43:21 +0000 Subject: [PATCH] - rename Table.row to Table.classobj for clarity - add Table.dict() for returning table as a dict keyed on Row.primary_key --- PLC/Table.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/PLC/Table.py b/PLC/Table.py index 967bc6d3..95244636 100644 --- a/PLC/Table.py +++ b/PLC/Table.py @@ -123,14 +123,15 @@ class Table(list): Representation of row(s) in a database table. """ - def __init__(self, api, row, columns = None): + def __init__(self, api, classobj, columns = None): self.api = api - self.row = row + self.classobj = classobj + self.rows = {} if columns is None: - columns = row.fields + columns = classobj.fields else: - columns = filter(lambda x: x in row.fields, columns) + columns = filter(lambda x: x in classobj.fields, columns) if not columns: raise PLCInvalidArgument, "No valid return fields specified" @@ -151,4 +152,15 @@ class Table(list): """ for row in self.api.db.selectall(sql, params): - self.append(self.row(self.api, row)) + obj = self.classobj(self.api, row) + self.append(obj) + + def dict(self, key_field = None): + """ + Return ourself as a dict keyed on key_field. + """ + + if key_field is None: + key_field = self.classobj.primary_key + + return dict([(obj[key_field], obj) for obj in self]) -- 2.47.0