0

A user input module that iterates though imported modules as shown. How can i make it to iterate though all imported modules since it only iterates though foo only

from packageA import foo # has open_book() function
from packageB import bar# has read_music() function
from packageC import loader# has loader() function
import re
import sys
import difflib

def search():
    name = input('Please enter your query: ').lower().split()

    while True:    
        for name in ["open_book","read_music","loader"  ]:  #help code here          
            getattr(foo, "open_book")()
            getattr(bar, "read_music")()
    else:        
        print ('Try again')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3346746
  • 327
  • 3
  • 14
  • What do you expect to happen? Your code also accesses `bar.read_music()` here. Do you want to call the 3 names on all 3 modules? – Martijn Pieters Feb 24 '14 at 13:12
  • If your 3 modules have 3 distinct functions, why not just use `foo.open_book()`, `bar.read_music()` and `loader.loader()`? E.g. just reference the methods directly? Why does it have to happen dynamically? – Martijn Pieters Feb 24 '14 at 13:13
  • @Martijn, yes to call all the three different modules. using name (user input) it is to iterate through the three and output as programmed in the snippets – user3346746 Feb 24 '14 at 13:14
  • That doesn't answer my question, not really. It is entirely unclear what you are trying to do here. – Martijn Pieters Feb 24 '14 at 13:16
  • i dont have the foo, bar or loader module snippets here but with user input e.g. 'open book', it will iterate through all the three searching for the one that will output the answer – user3346746 Feb 24 '14 at 13:19
  • So the user will enter `open_book` or `read_music` or `loader` and you want to invoke the right function for any of those 3? – Martijn Pieters Feb 24 '14 at 13:20

1 Answers1

1

If you want to let a user enter any function name that might be available on those 3 modules, store all 3 modules in a list or tuple and loop over that, then test if the function is available:

modules = (foo, bar, loader)

def search():
    name = input('Please enter your query: ').lower()

    for module in modules:
        function = getattr(module, name, None)
        if function is not None:
            function()
            break

    else:        
        print ('Try again')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Hi thanks for the input. What i mean is that the user input does not necessarily call the module name but inputs a string that contains a keyword that iterates though the modules. – user3346746 Feb 25 '14 at 06:34
  • That makes no sense whatsoever. Can you update your question with some examples of user input and expected behaviour / output? – Martijn Pieters Feb 25 '14 at 07:27
  • user input will be like, 'please open book', this iterates over the modules to the module that picks a key word and opens the book. e.g. in the foo module, if open book in name(user input)..code to open the book. I am working on each module to be able to pick specific books for example but all i need is to fix this problem – user3346746 Feb 25 '14 at 07:40
  • Update your question with that information. Natural language processing is a *broad* topic, though. – Martijn Pieters Feb 25 '14 at 08:05