Home > Software design >  Access multiple return values in Python within multiple functions
Access multiple return values in Python within multiple functions

Time:09-15

I have below code. In the first function am creating database connection. And in last function am inserting the data into a table. Since am not committing the records, data is not getting committed in the database. If I return both cursor and connection as, return cur, conn, how do I access them in another function

import cx_Oracle

class transformation():

    def oracle_conn(self):
        con = cx_Oracle.connect('user/password@localhost:1521/xe')
        cur = con.cursor()
        return cur

    def extract(self):
        data = self.oracle_conn().execute('select * from employees').fetchall()
        return [(row) for row in data]

    def load(self):
        values = self.expression()
        query = 'INSERT INTO hr.employees_python (EMPLOYEE_ID, NAME, EMAIL, ' \
                'PHONE_NUMBER, HIRE_DATE, JOB_ID,  SALARY, MONTH, ' \
                'DAY, YEAR) values (:0, :1, :2, :3, :4, :5, :6, :7, :8, :9)'
        self.oracle_conn().executemany(query, values)
        

a  = transformation()
a.load()

Any suggestion will be helpful.

CodePudding user response:

Make cur and conn attributes of the class

import cx_Oracle

# class names should be capitalized
# no need for parenthesis if you are not inheriting
class Transformation:
    # You might want a constructor to pass parameters 
    def __init__(self):
        self.con = None
        self.cur = None

    def oracle_conn(self):
        # now cur and con are attributes of the class, 
        # so you don't actually need to return them
        self.con = cx_Oracle.connect('user/password@localhost:1521/xe')
        self.cur = self.con.cursor()

    def extract(self):
        # you can access cur and con here by doing
        # self.cur
        # self.con

        # just call oracle_conn() and then you can access self.con
        # but # you might not want to call oracle_con everytime.
        self.oracle_conn()
        data = self.con.execute('select * from employees').fetchall()
        return [(row) for row in data]

    def load(self):
        # you can access cur and con here by doing
        # self.cur
        # self.con
 
        # here you are using self.expression() which is not defined
        values = self.expression()
        query = 'INSERT INTO hr.employees_python (EMPLOYEE_ID, NAME, EMAIL, ' \
                'PHONE_NUMBER, HIRE_DATE, JOB_ID,  SALARY, MONTH, ' \
                'DAY, YEAR) values (:0, :1, :2, :3, :4, :5, :6, :7, :8, :9)'
        
        # you might not want to call oracle_con everytime.
        oracle_conn()
        self.con.executemany(query, values)
        

# a is not a very good name
a  = Transformation()
# you can also access them here doing 
# a.con
# a.cur

CodePudding user response:

You can simply do so by writing

n,m = oracle_conn()

Alternatively you can return them in a list format and then access them via the indices. So in oracle_conn:

return [cur, con]

and whereever you want to use it:

stuff = oracle_conn()
n = stuff[0]
m = stuff[1]

Edit: However, this is the way if you only want to access it in one other function. Otherwise I'd suggest the same as Sembei Norimaki.

  • Related