1

Am still new to web2py and python, in my web2py app, i created this code that works well in python shell.

python modules: The methods work in such a way that a user inputs an equation query to get an answer. If it is an addition, method1 works it out, the same to other methods being invoked to performing different codes e.g.

def method1():# to do additions
    name = input('Please Enter equation here: ').lower()
    if '1 + 1':
        answer = code
        return answer

def method2():# to do subtractions
    name = input('Please Enter equation here: ').lower()
    if '1 - 1':
        answer = code
        return answer

In the controller, I imported the methods as follows though there are many more methods than these shown

from applications ...... import method1
from applications ...... import method2
from applications ...... import method3
from applications ...... import method4

method1 = method1
method1 = method2
method1 = method3
method1 = method4

G0 = [method1, method2, method3, method4]

def Foo():
    code..
    for (func) in G0:
        return func()

The problem is that only method1 which is at position[0] in the list is invoked and not other methods. I want to randomly call any method when a user inputs any query.

user3346746
  • 327
  • 3
  • 14

4 Answers4

2

you're looking for yield.

G0 = [method1, ..., method4]

def foo():
    for method in G0:
        yield method()

method_results = foo()
type(method_results) # <class 'generator'>
for result in method_results:
    print(result)
## OUTPUT will be each result, one per line

Though I think the deeper problem is:

method1 = method1
method1 = method2 # method1 = ... huh?
method1 = method3 # help I'm trapped in an
method1 = method4 # overwrite factory....
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
1

Only method1 is called because you're returning from inside the loop, so the loop exits as well. What do you want to return? A list of all return values perhaps?

def Foo():
    ret_list = []
    for (func) in G0:
        ret_list.append(func())
    return ret_list
Carl
  • 905
  • 5
  • 9
1

If you want to call methods randomly use random.choice:

def foo1():
    print "hello"

def foo2():
     print "world"

def foo3():
     print "goodbye"

def foo4():
     print "world"
GO = [foo1, foo2, foo3, foo4]

import random

def Foo():
    func = random.choice(GO)
    return func()
In [30]: Foo()
world

In [31]: Foo()
goodbye

In [32]: Foo()
hello

In [33]: Foo()
goodbye
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Are you trying to run all of those methods in G0? If so, the issue is that you're using return func(). The return keyword will exit Foo() as soon as it's called, which is why only one function gets called. You should just use func() or result = func() if you want to store the value.

If you only want to run one of those methods, you need to ditch the for loop and just use return G0[x](), where x is the list index of the function you want to call.

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42