469,290 Members | 1,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

problem with writing a simple module

ello there. i am having a problem getting a module to work right.

i wrote a class that is going to be used in a few different scripts in
the same directory.

it looks like this:

#!/usr/bin/python

import MySQLdb

class DbConnector(object):
"""
Database Connection object.
class receives the db argument to specify the database.
"""

def __init__(self, db='test_db', host="10.10.10.16",user="me",
passwd="mypass"):
self.host = host
self.user = user
self.passwd = passwd
self.db = db
# Unpack Other Database Arguments Here
self.CreateConnection()

def createConnection(self):
self.connection = MySQLdb.connect(self.host, self.user, self.passwd,
self.db)

def killConnection(self):
self.connection.close()

def getMany(self, sql_statement):
cursor = self.connection.cursor()
try:
cursor.execute(sql_statement)
result = cursor.fetchall()
self.connection.close()
return result
except:
self.connection.close()

the file is saved as DbConnector.py and made executable.
then i get this in idle
import DbConnector
x = DbConnector()
then it tells me that the module object is not callable.

this works though import DbConnector
x = DbConnector
x.db = 'other_db'
results = x.getOne("SELECT * FROM `stuff`")


it tells me that the module has no attribute getOne.

i am really not trying for an attribute, but a method.

anyone know what i am doing wrong?

thanks

May 22 '06 #1
8 963
ne*****@xit.net wrote:
ello there. i am having a problem getting a module to work right.

i wrote a class that is going to be used in a few different scripts in
the same directory.

it looks like this:

#!/usr/bin/python

import MySQLdb

class DbConnector(object):
"""
Database Connection object.
class receives the db argument to specify the database.
"""

def __init__(self, db='test_db', host="10.10.10.16",user="me",
passwd="mypass"):
self.host = host
self.user = user
self.passwd = passwd
self.db = db
# Unpack Other Database Arguments Here
self.CreateConnection()

def createConnection(self):
self.connection = MySQLdb.connect(self.host, self.user, self.passwd,
self.db)

def killConnection(self):
self.connection.close()

def getMany(self, sql_statement):
cursor = self.connection.cursor()
try:
cursor.execute(sql_statement)
result = cursor.fetchall()
self.connection.close()
return result
except:
self.connection.close()

the file is saved as DbConnector.py and made executable.
then i get this in idle
import DbConnector
x = DbConnector()
then it tells me that the module object is not callable.

this works though import DbConnector
x = DbConnector
x.db = 'other_db'
results = x.getOne("SELECT * FROM `stuff`")


it tells me that the module has no attribute getOne.

i am really not trying for an attribute, but a method.

anyone know what i am doing wrong?

thanks

You can do either:

from DbConnector import *
x=DbConnector()

or (preferred method these days)

import DbConnector
x=DbConnector.DbConnector()

When you zay x=DbConnector all you are doing is setting a
variable (pointer) x that points to the class DBconnector,
which is basically an alias. x in fact would not have
a getOne method as it hasn't been instantiated yet.

-Larry Bates
May 22 '06 #2
ok, cool, and thanks very much. That worked.
thanks for the info too.
i am still new at the OO thing, just tired of doing a copy and paste
over and over again.

thanks again

May 22 '06 #3
ne*****@xit.net wrote:
ok, cool, and thanks very much. That worked.
thanks for the info too.
i am still new at the OO thing, just tired of doing a copy and paste
over and over again.

thanks again

Glad that helped. The OO "stuff" does require a new
set of understanding, but once you get it you can write
reusable classes and productivity really begins to take
off.

If you haven't yet picked one up, get a copy of the
Python Cookbook (really good) and if you do Windows
get a copy of Python Programming on Win32.

-Larry
May 22 '06 #4
yeah, i have thought of picking that one up. That one, or nutshell.
i got programming python, which was way over my head, then learning
python, which has helped me a great deal.

thanks

May 22 '06 #5
ne*****@xit.net wrote:
ello there. i am having a problem getting a module to work right.

i wrote a class that is going to be used in a few different scripts in
the same directory.

it looks like this:

#!/usr/bin/python
This is not needed for a module.

(snip code) the file is saved as DbConnector.py
The most common convention is to name modules all_lower, and keep
CamelCaseNames for classes.
and made executable.
This is not needed for a module.

You may want to make "scripts" (ie: modules that has to be runned as
main program" executables and then add the shebang. Other modules (the
ones that are meant to be used via import) don't need this.
then i get this in idle

import DbConnector
x = DbConnector()

then it tells me that the module object is not callable.


cf Larry's answer. You may also want to browse the very recent (may 19)
thread named "noob import question".

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 22 '06 #6
ok, thanks everyone. The funny thing about the name conventions is, i
was just two days ago looking around on the web for the best way to go
about consistancy in variable names, class names, etc..

i have the module working now. thanks to you guys. And i think i
understand the container name | object name paradigm.

i did not get the suggestion to change all my database queries so the
parameter substitution is performed by the .execute*() methods..

that one went over me head.

thanks for all the help, guys

May 23 '06 #7
ok, what i posted above had the getOne method, the whole class has a
function for getMany, update, and Insert.

none of this will be used by an end user, it kinda runs in the
background. But, if you have a good link to the docs on the API, i
would like to see it. Still kinda new at this.

thanks

May 23 '06 #8
cool, thanks, i was running on some thinner examples i found on the
net.

May 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.