[Tutor] Self, Scopes and my unbelievable muddleheadedness.
John Fouhy
john at fouhy.net
Thu Oct 26 00:53:19 CEST 2006
On 26/10/06, doug shawhan <doug.shawhan at gmail.com> wrote:
> class Create:
> def freshDB(self, DBPATH, Fields):
> # ...
> def comparisonTable(self, DBPATH, Fields, columns, mode):
> # ...
> Now when I run freshDB from the other script:
>
> Fields = {"Common":"Inventory_Number, Stock_Number, Location, Year, Make,
> Model, Part_Type, Mileage, Description, Interchange_Data, Condition, Price",
> "EvilBay":"EvilBay_Title, EvilBay_Description",
> "HappyBase":"HappyBase_Description,
> HappyBase_Long_Description"}
>
> d = DBMod
>
> d.Create().freshDB(DBPATH, Fields)
>
> d.Create().comparisonTable(DBPATH, Fields, columns, "new")
What is the purpose of your Create class? It seems to me that you are
not actually doing anything object-oriented here. So maybe you're
confused about 'self' because you've got no object orientation?
I think you've got a couple of ways forward. You could abandon your
class, and just put the functions directly into a module; ie:
###### creategadfly.py #######
def freshDB(dbpath, fields):
# etc
def comparisonTable(dbpath, fields, columns, mode):
# etc
######
and then, in your code, you would do:
import creategadfly
creategadfly.freshDB(DBPATH, Fields)
creategadfly.comparisonTable(DBPATH, Fields, columns, "new")
Alternatively, you could restructure your code to make it OO.
Something like this:
######
class GadflyConnection(object):
def __init__(self, dbpath, dbname, fields):
self.createDB(dbpath, dbname, fields)
def createDB(self, dbpath, dbname, fields):
self.db = gadfly.gadfly()
self.db.startup(dbname, dbpath)
# etc
def comparisonTable(self, mode):
if mode == 'new':
# insert code to create 'query'
cursor = self.db.cursor()
cursor.execute(query)
cursor.close()
Maybe you would want a call to self.comparisonTable inside __init__?
Then to use:
gadflyDB = GadflyConnection(...)
I'm not sure what you want to do with your database, so I can't really
give you a bigger use example. But do you see what is going on? In
createDB(), you create a new gadfly connection, and then assign it to
'self.db'. Then, in comparisonTable, you can reference self.db to
access that connection.
Hope this helps.
--
John.
More information about the Tutor
mailing list