Maitaining a global valaue across all action classes

I have file in which i have so many action related classes. Each action class has certain logic of connecting to DB. While connecting to DB, getting connection objection is common across all actions.

Is there a way to maintain such common logic at common place?? Every action need to refer to common place and get the connection object. Could some one please educate me on this?

class ActionDepositStatus(Action): def name(self): return ‘action_deposit_status’

def run(self, dispatcher, tracker, domain):
        import cx_Oracle
        # Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
        conn_str = u'INSTALLER/'
        connection = cx_Oracle.connect(conn_str)
        cursor = connection.cursor()
        agency_id = tracker.get_slot('Jurisdiction')
        deposit_Status = tracker.get_slot('depositStatus')
        agencyids = {'federal': 9, 'flsui': 9}
        depositStatuses = {'pending': "0", 'failed': "F",'held':"1"}
        #cursor.execute(u'SELECT * FROM mt_control')
        cursor.execute(u"select  status, amount as amount from mt_it_history where A_I = :aid and STATUS = :status",{"agencyid":agencyids[agency_id],"status":depositStatuses[deposit_Status]})
        row = cursor.fetchone()
        depositstatus = row[0]
        response = """It is currently $$  {}  in {} status for {}. """.format(row[1],deposit_Status, agency_id )
        return [SlotSet('depositStatus',deposit_Status),SlotSet('Jurisdiction',agency_id)]

class UtilMetods(Action):

def areThereAnyErrors(self,procid):
    import cx_Oracle
    conn_str = u'INSTALLER/'
     connection = cx_Oracle.connect(conn_str)
    errormessagecursor = connection.cursor()
    errormessagecursor.execute(u"select  count(*) from MT_PROSS_MESGE where poc_id = :procid and msg_Tye =    :meype",{"procid": procid, "messageType": 'E'})
    counts = errormessagecursor.fetchone()
    errorCount = counts[0]
    print("error count is {}".format(errorCount))
    if errorCount == 0:
        return False
        return True

Maybe creating a file and importing it on your could resolve this?

1 Like

@Abir Dude please see if you can answer this

I would suggest of creating a DB connection and object retrieval method . You can keep it in your action file just the way you have did it .

In order to make the method more generic i would suggest you to use “**kwargs” in place of any hard coded arguments as the later one makes the method less flexible.

Another thing is try passing the tracker object to your method , it may sound pointless initially but it helps for future improvements .

There are multiple ways to do the same . It’s your requirement that matters

Hope this helps :slight_smile:

What if I have to retrieve from a small sized pandas dataframe? Then I should just declare it as a global variable, is it? @Abir

Global declaration is never a good idea(ideally) . As you said it’s a small data frame , why not pass the data frame to a method (coded to do your task). That way if you intent to do some permanent changes in the data frame , you would be able to attain it using “inplace” attribute of most of the pandas methods and along with that you will be able to retrieve data from it as well.

Okay and what if I’m not gonna make any changes in that dataframe ever?