Why can't i iterate over functions and print on view

45 views
Skip to first unread message

Maurice Waka

unread,
Jun 1, 2016, 12:48:41 PM6/1/16
to web2py-users
I have part of the code here as follows:
r_lst = [foo(), foo1(), foo2(), foo3()]
class Filters(object):
    @staticmethod
    def Search_item():
        it = iter(r_lst)
        while True:
            try:
                for i in range(len(r_lst)):
                    it.next
                except stopIteration:
                    break
c = Filters.Search_item()
p = Filters
List_func = c
def Do_print():
    for stuff in dir(p):
        if 'Filter' in stuff:
            if List_func:
                return List_func
result = Do_print()
On view I have this:

code.....

{{=XML(''.join(list(result)))}}{{break}}

N/B
When I do this on Idle, it works well by printing out the result such as
def foo():
    print 'Hello World'...###Hello world is stored in a sqlite DB .I use 'return' instead of print function when using web2py
when I run this code on web2py, I just get a blank page. Where could i be going wrong?
Regards

Leonel Câmara

unread,
Jun 1, 2016, 1:10:25 PM6/1/16
to web2py-users
What are you trying to do exactly? That code is an unreadable mess.

The reason you're getting a blank page is that you only return if 'Filter' in stuff, since "stuff" is just one of the things you get when you run dir on class Filters (you have p=Filters and then you do dir(p)), and since that class has nothing with a "Filter" in its name on its __dict__ that is never true.

Are you trying to program by coincidence or something? 

Anthony

unread,
Jun 1, 2016, 1:41:44 PM6/1/16
to web2py-users
No idea what you're trying to do here, but note that web2py executes a view only if the controller function returns a dictionary. You haven't shown all of your code, but it isn't clear you are returning a dictionary (with "result" as one of the keys).

Anthony

Maurice Waka

unread,
Jun 1, 2016, 2:33:47 PM6/1/16
to web...@googlegroups.com

Thanks Anthony.
Am trying to import modules to the controller then run the functions imported from a list (r_lst). From here the several functions will print out different messages to view depending on user input.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/JeJEbqVpxKo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maurice Waka

unread,
Jun 1, 2016, 2:35:18 PM6/1/16
to web...@googlegroups.com

Hi.  Am not a professional python programmer so please bear with my crude code.

--

Maurice Waka

unread,
Jun 1, 2016, 2:39:30 PM6/1/16
to web...@googlegroups.com

I can see your point though.  And the input from Anthony. Let me work on the code again and try return a dict. The code is very long that's why I only took  a snippet

On Jun 1, 2016 8:10 PM, "Leonel Câmara" <leonel...@gmail.com> wrote:

--

Dave S

unread,
Jun 1, 2016, 3:16:56 PM6/1/16
to web2py-users


On Wednesday, June 1, 2016 at 11:33:47 AM UTC-7, Maurice Waka wrote:

Thanks Anthony.
Am trying to import modules to the controller then run the functions imported from a list (r_lst). From here the several functions will print out different messages to view depending on user input.


I'm not sure that I understand.  Do you mean the user input results in a request to
//mydomain.com/myapplication/mycontroller/myfunction?arg1&arg2   
and that you're supposed to do different things according to the values of arg1 and arg2?  If that's the case, myfunction() should probably just use regular if-statements.  The branches of the if-statement can invoke imported functions in the normal python way

import datetime
def myfunction():
   
if "snooze" in request.args[0]:
      result
= "Please rest until %s" % (request.now.timedelta(minutes=10).isoformat())
   
return dict(result = result)


If you want the print actions to happen without sending an [additional] request to the server, that's a different story.  Note that you normally can't run Python code in the browser, so returning Python to the browser doesn't have much value.  You need to have Javascript functions in place of the Python functions.  Python does run in the view, but that happens on the server, and can't respond to user input until that input is turned into a request.

If you mean something else, then you'll have to try again to explain what you want to do.

Dave
/dps



Anthony

unread,
Jun 1, 2016, 8:11:19 PM6/1/16
to web2py-users
On Wednesday, June 1, 2016 at 2:35:18 PM UTC-4, Maurice Waka wrote:

Hi.  Am not a professional python programmer so please bear with my crude code.


It's not that the code is crude so much as it is insanely complicated if all you want to do is call a list of functions and get a list of their results. Unless there's something much more sophisticated going on in the rest of the code, I don't know how you would have stumbled upon this particular implementation.

Anthony
Reply all
Reply to author
Forward
0 new messages