Matt W.
Matt W.

Reputation: 3722

replace a chained method with a variable in python

I have a function from the simple_salesforce package,

sf = Salesforce(username, pass, key)

In order to update an object in the salesforce database, you call sf by:

sf.bulk.object.update(data)

For example, account is the native customer account object in salesforce, so you would feed data for updating accounts like this:

sf.bulk.account.update(data)

I was wondering if there is a way in python to set that specific piece of the chain as an argument.

what I would like to do:

def update_sf(object, data):
    sf.bulk.object.update(data)

That way I could call:

update_sf('account', data)

The only other way I can think of doing this is to create a dictionary with the dozens of values for objects in the instance

{'account':sf.bulk.account.update(),
 'contact':sf.bulk.contact.update()}

Is there a way to do this?

Upvotes: 1

Views: 260

Answers (1)

Sohaib Farooqi
Sohaib Farooqi

Reputation: 5666

You can use builtin function getattr to fetch your desired entity:

>>> getattr(sf.bulk, object).update(data)

To also able to dynamically select operation(insert, update, delete) you can chain getattr

>>> getattr(getattr(sf.bulk, object),operation)

Upvotes: 2

Related Questions