I am building what essentially could be viewed as a glorified database wrapper as a Python package, where I'm having several classes and functions spread out into different modules. My current problem is how to share the database connections with the different modules.
An example class in user.py:
class User(object):
def __init__(self, user_name, db_con):
self.user_name = user_name
self._db_con = db_con
Which results in my __init__.py looking like this:
from .user import User as _User
def _connect_dbs():
econ = _mdb.connect(host='db.com',
user='user', passwd=pass,
db='db', charset='utf8')
return econ
_db = _connect_dbs()
def User(user_name):
return _User(user_name, _db)
Is this a good solution, or could I implement it in another manner? While this avoids global variables, it does result in some code I would rather not have to write for quite a few functions.
1 Answer 1
If you are creating a database wrapper, why not make a lib out of the wrapper and stick it into a lib
directory that other modules/classes can import? So, your Users
class would be in a directory structure like this:
project/
account.py
lib/
database.py
user.py
zombie.py
Your database.py
would look something like:
import pymysql
class Database:
@staticmethod
def connect_dbs():
econ = _mdb.connect(host='db.com',
user='user', passwd=pass,
db='db', charset='utf8')
return econ
Then, in the user.py, you'd simply do an import like:
from .lib.database import Database as MyDatabase
Then, to get the connection, something along the lines of:
my_connection = MyDatabase.connect_dbs()
EDIT: As an example, see this repl.it:
class Database:
connection = None
def __init__(self):
print "Instantiating!"
def connect_dbs(self):
if self.connection is not None:
return self.connection
self.connection = 1
return self.connection
class User:
db = None
def __init__(self):
self.db = Database()
def save_user(self):
print self.db.connect_dbs()
U = User()
for i in range(30):
U.save_user() # Only instantiates the connection once, then reuses it 30x
-
\$\begingroup\$ I like this solution, but wouldn't this create a new connection instance every time connect_dbs is called? My solution was based solely on trying to avoid this. \$\endgroup\$user47380– user473802014年10月03日 19:27:26 +00:00Commented Oct 3, 2014 at 19:27
-
\$\begingroup\$ Unfortunately, yes, it would create a new connection instance every time (unless you were to instantiate MyDatabase to a User property, then in
class Database > connect_dbs
, instead of returning econ, you would store that in self.econ or whatever). \$\endgroup\$jsanc623– jsanc6232014年10月03日 20:12:37 +00:00Commented Oct 3, 2014 at 20:12 -
\$\begingroup\$ @JimmyC I've updated my answer to reflect my previous comment \$\endgroup\$jsanc623– jsanc6232014年10月03日 20:19:45 +00:00Commented Oct 3, 2014 at 20:19