February 10th, 2014, 01:24 PM
Dynamically call methods
1. I want to execute a sql in oracle, mysql, postgre databases.
2. So I created three different classes named mysql.py, oracle.py, postgre.py
3. I want to write a method it should accept one parameter. The parameter should be one of the above(mysql or oracle or postgre)
4. Based on the object it should call the corresponding methods.
The main aim of this approach is reusability of the code.
How to do it? Please give some brief explanation on this.
I believe that we need to use some design patterns here.
Please give sample codes and links also.
February 11th, 2014, 07:59 AM
Yes there's a design pattern for this, I'm sure it's one of the originals in the gang of 4 book. Maybe a dictionary is sufficient. Its key is the parameter you mention, the value is a tuple of db specific information.
chameleon = dict(
mysql = (mysql_connection, mysql_database),
sqlite3 = (sqllite3_connection, sqlite3_database),
(connection, db,) = chameleon['sqlite3']
[/code] are essential for python code and Makefiles!