jeudi 31 décembre 2020

OOP - Python - How to call function within class and avoid the error of missing arguments caused by self?

I am the beginner to the OOP practices.

I want to create a class which execute my query to database.

Here is my code:

class tools():
    def connect_db(self):
        cfg = ConfigParser()
        cfg.read('env.ini')
        sv_info = cfg['sv_info']

        db =  psycopg2.connect(
            host = db_connect['host'],
            user = db_connect['user'],
            password = db_connect['password'],
            database = db_connect['database'],
            port = db_connect['port']
        )
        return db


    def run_query(self, query):
        connection = self.connect_db
        do_the_query = connection.cursor()
        do_the_query.execute(query)
        do_the_query.fetchall()
        return do_the_query

    def find_locations(self,simple_query, latitude, longitude):
        return self.run_query(simple_query).format(latitude = latitude, longitude = longitude))

simple_query:

simple_query = """select * from a_function('{latitude}' , '{lontitude}')"""

My code to run it:

tools.find_locations(simple_query , '12.123456', '21.654321')

However, I keep receiving the error of:

TypeError: find_locations() missing 1 required positional argument: 'longitude'

According to my research, the argument self should not be taken to account (being ignored).

Anyway, would be nice if you guys can correct my code.

Aucun commentaire:

Enregistrer un commentaire