I have a long list of possible files I need to import. I will only ever need 1 of them and they all have the same interface. (Choosing a payment gateway to process a payment)
Suppose I have a dictionary that represents the name of all the gateway files.
i.e.
gateways = {
'1' : 'authorize',
'2' : 'paysimple',
'3' : 'braintreepayments',
'4' : 'etc',
}
I know the keys to this dictionary based on information from a database. So, if I receive a payment process request with a gateway value of 1 I know it needs to be handled by Authorize.net. A 2 is to be processed by Pay Simple. Etc.
I'd like to be able to create an import statement that is built with the information I know rather than a horrible list of elif
statements.
Consider the simple method below:
# For the purposes of this example assume payment_gateway is defined
# elsewhere and represents the key to the dictionary
gateway_file = gateways.get(payment_gateway)
import_str = "from gateway_interface.%s import process" % gateway_file
gogo(import_str)
Where gogo
is a way to cause the import statement to actually import.
Is such a thing possible?