473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1021
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rami A. Kishek | last post by:
Hi - this mysterious behavior with shelve is just about to kill me. I hope someone here can shed some light. First of all, I have this piece of code which uses shelve to save instances of some...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
158
by: jacob navia | last post by:
1: It is not possible to check EVERY malloc result within complex software. 2: The reasonable solution (use a garbage collector) is not possible for whatever reasons. 3: A solution like the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.