[SQLObject] Re: Importing classes from seperate files
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Scott L. <sle...@in...> - 2003-12-04 02:23:43
|
Brad Bollenbach wrote: > Le dimanche, 30 nov 2003, =E0 22:37 Canada/Eastern, Scott LeFevre a =E9= crit : > > I'm new to Python and have been frustrated by a problem specific > to SQLObject. I want to create my classes in seperate > files/modules and import them into the application that I'm > writing. If I do this and try to use any of the SQLObject based > classes that I've created, I always end up with an AttributeError > > For example, if I try to create a new object with the new method, > I'll get something like. > Traceback (most recent call last): > File "test01.py", line 13, in ? > r =3D ProcReport.new() > File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", > line 883, in new > inst._SO_finishCreate(id) > File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", > line 904, in _SO_finishCreate > id =3D self._connection.queryInsertID(self._table, self._idName= , > AttributeError: 'NoneType' object has no attribute 'queryInsertID' > > Now if I copy the class code in the same file as the applicate > code everything works. I've tried importing my class files via > 'import classfile' and 'from classfile import *' without any succes= s. > > I realize this is a name space issue, but I thought that the 'from > ... import' statement would resolve this. Can anyone shed some > light on this for me? Can I have my classes in a seperate file or > do I have to put everything in a single file for SQLObject? > > > It's not a namespace issue. python is telling you that a NoneType=20 > object doesn't have a method called queryInsertID, and if you notice=20 > in the error message, the object on which that method is called is=20 > self._connection. > > In other words, your connection is a NoneType, which it obviously=20 > shouldn't be. > > In other words, you didn't define __connection__ (or a per class=20 > _connection) in your module full of SQLObject-derived classes. One of=20 > the most recent posts to this list referred to precisely the same=20 > issue (with precisely the same error message.) > Thank you for your help. Since I'm new to Python, I don't have a good=20 grasp of how global variables work and how to use them properly. I've=20 tried the following: Created a ProcReport class based on SQLObject and saved it in a seperate = file. I then imported this file into my test app. Please see below. _ProcReport.py --------------------------------------------------- from SQLObject import * class ProcReport(SQLObject): #db columns/fields sourceorg =3D StringCol(length=3D10, default=3DNone) mrn =3D StringCol(length=3D10, default=3DNone) lastname =3D StringCol(length=3D10, default=3DNone) firstname =3D StringCol(length=3D10, default=3DNone) middlename =3D StringCol(length=3D10, default=3DNone) dateofbirth =3D DateTimeCol(default=3DNone) gender =3D StringCol(length=3D1, default=3DNone) orderingmd =3D StringCol(length=3D20, default=3DNone) procdesc =3D StringCol(length=3D20, default=3DNone) srvdatetime =3D DateTimeCol(default=3DNone) readingmd =3D StringCol(length=3D20, default=3DNone) reporttext =3D StringCol(default=3DNone) reporthtml =3D StringCol(default=3DNone) reportsource =3D StringCol(default=3DNone) ------------------------------------- test01.py ----------------------------------- from SQLObject import * __connection__ =3D MySQLConnection(host=3D'localhost', user=3D'xxxxx', = passwd=3D'zzzz', db=3D'ReportDR') from _ProcReport import ProcReport ProcReport.createTable() --------------------------------- When I run the test01.py app I get the AttributeError: 'NoneType' as I=20 listed in a previous post. My best guess is the __connection__ variable = isn't available when the class is declared. I've tested this by=20 inserting the following code into both files: for k,v in globals().items(): print k,'=3D',v the __connection__ variable doesn't print from the _ProcReport file but=20 does from the test01 file. I hope this is clear. I'm stumped as to why this doesn't work. Could=20 someone please share with me a clean way of accomplishing this? Or, a=20 good example of how to use global variables? Thank you for you help!! --=20 Scott LeFevre, Consultant Solutio Informatio |