so I am trying to use a variable brand which is selected by the user. The variable is then to be used to call a given module in Python. currently on line 7 you can see 'apple.solutions()'. However, I essentially want to be able to use something on the lines 'brand.solutions()' - although I know this will not work as it requires the attribute. I am looking for a solution to select the module based on the variable brands. I would appreciate any solutions or advice. Thanks,
Main program:
import apple, android, windows
brands = ["apple", "android", "windows"]
brand = None
def Main():
query = input("Enter your query: ").lower()
brand = selector(brands, query, "brand", "brands")
solutions = apple.solutions()
print(solutions)
Apple.py Module File (same directory as main program):
def solutions():
solutions = ["screen", "battery", "speaker"]
return solutions
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import apple, android, windows
brands = ["apple", "android", "windows"]
def selector(brands, query):
if query in brands:
exec("import %s as brand" % query)
else:
brand = None
return brand
def Main():
query = raw_input("Enter your query: ").lower()
brand = selector(brands, query)
solutions = brand.solutions()
print(solutions)
if __name__ == '__main__':
Main()
I have a simple way by using the exec
function to dynamically import models