[PYTHON DB-SIG] recent postings and table classes

Hirendra Hindocha hiren@dnaco.net
Tue, 29 Oct 1996 02:00:25 -0500


--=====================_846590425==_
Content-Type: text/plain; charset="us-ascii"

Hmm, I don't have access to the recent postings on the db-sig (i.e not
from home) but the general talk has been about wrapper classes to
table and a row. 
I have been working on displaying data from a RDBMS using the odbc module.
The original idea was to provide base wrapper classes around the table
and a row from other classes will derive to provide app specific functionality.
This instances could then be passed off to the HTMLgen.Table class which
expects a list of lists to format and display. 
The idea was to retrieve a row just when needed (didn't quite work out
because of string.join inability to handle an instance instead of a
sequence or a list)

and here is some preliminary work done that I've attached. 
suggestions are most welcome. 
Hiren 
--------------------------------cut here ----------------------------

--=====================_846590425==_
Content-Type: text/plain; charset="us-ascii"
Content-Disposition: attachment; filename="Database.py"

# $Id: Database.py,v 1.1 1996/10/28 03:33:27 hiren Exp hiren $
# $Author: hiren $
# vim:ts=2:sw=2

import dbi
import odbc
import time
class RTable:
	def __init__(self,dsn):
		try:
			s = odbc.odbc(dsn)
			self.cur = s.cursor()
		except NameError,e:
			print 'error ', e, 'undefined'
	def execute(sql):
		self.sql = sql
		try:
			self.cur.execute(self.sql)
		except NameError,e:
			print 'error ', e, 'undefined'
	def __len__(self):
		pass
	def __setitem__(self,key,val):
		pass
	def __getitem__(self,index):
		rec = self.cur.fetchone()
		if not rec:
			raise IndexError, "index too large"
		return rec
class RRec:
	def __init__(self,rec,description):
		self.record = []
		if not rec: return 
		i = 0
		for field in rec:
			if description[i][1] == 'DATE':
				#print description[i][1],field,field.value
				if field != None and field != 'N/A':
					local = time.localtime(field.value)
					s = str(local[1]) + '/' \
						+ str(local[2]) + '/' +str(local[0])
					setattr(self,description[i][0],s)
					self.record.append(s)
				else:
					self.record.append('N/A')
			elif description[i][1] == 'RAW':
					dummy = 'RAW Not Implemented'
					setattr(self,description[i][0],dummy)
					self.record.append( dummy)
			else:
					setattr(self,description[i][0],field)
					self.record.append(field)
			i = i+1
		self.data = self.record
	def dump(self):
		for i in self:
			print i,
		print
	def __len__(self):
		return len(self.record)
	def __setitem__(self,key,val):
		self.record[key] = val
	def __getitem__(self,index):
		return self.record[index]


if __name__ == '__main__':
	print 'script as main'
	#sqlstmt = 'select * from shippers'
	#sqlstmt = 'select * from orders'
	#sqlstmt = 'select * from categories'
#	sqlstmt = '\
#	SELECT Employees.EmployeeID, Employees.LastName,\
#	Employees.FirstName, Employees.Title, Employees.TitleOfCourtesy,\
#	Employees.BirthDate, Employees.HireDate, Employees.Address,\
#	Employees.City, Employees.Region, Employees.PostalCode,\
#	Employees.Country,\
#	Employees.HomePhone, Employees.Extension, Employees.Notes,\
#	Employees.ReportsTo\
#	FROM Employees'
	sqlstmt = '\
	SELECT EmployeeID, LastName,\
	FirstName, Title, TitleOfCourtesy,\
	City, Region, PostalCode,\
	Country,\
	HireDate, Address,\
	HomePhone, Extension, Notes,\
	ReportsTo\
	FROM Employees'

	dsn='nwin/admin/'
	s = RTable(dsn,sqlstmt)
	#print s.cur.description
	for rec in s:
		b = RRec(rec,s.cur.description)
		for field in b:
			print field,
		print

--=====================_846590425==_
Content-Type: text/plain; charset="us-ascii"


--=====================_846590425==_--


=================
DB-SIG  - SIG on Tabular Databases in Python

send messages to: db-sig@python.org
administrivia to: db-sig-request@python.org
=================