1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Rep Power

    Thumbs up 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.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    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]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo