python - Create an import statement using variable "interpolation" -


I have a long list of files that I need to import. I would only need 1 of them and they all have the same interface (selecting payment gateway for a payment process)

Suppose I have a dictionary that represents the names of all the gateway files Does. Gateway = {'1': 'Authorized', '2': 'Pessemal', '3': 'BrainPrements',' 4 ':'

Etc. ',}

I know the key to this dictionary based on information from a database. Therefore, if I get a payment processing request with a gateway value of 1, then I know that it should be controlled by Authorize.net. A2 has to be processed by simple, etc.

I want to be able to create an import statement which is created with my information instead of the awesome list of elif statements.

The payment_gateway for the purposes of this example is defined #here elsewhere. Gateway_file = gateways.gate (payment_gateway) import_str = "gateway_interface% s import process"% gateway_file gogo (import_str)

where gogo is actually imported The reason for the import statement is made.

Simple

  process = __import __ ('gateway_infaces'. + Gateway_file, list = ['foo']) .process   

Edit : 'Foo' in the list may not be long aslist is not an emptylist

Also had to edit because my first post was not described as expected in __ import __ .

If you have Python 2.7 import importlib process = importlib.import_module ('Gateway_interface.' + Gateway_file) .process

WAAAAY will be cool Use package_tools (like pkg_resources import iter_entry_points )

Can solve the finding of the right function even if they are not in weird packages under the gateway_iffire if they all have a place Su and that Overkill so you do not have Sitil-point ... yeah just __ import __

Comments