web2py function import not working well

69 views
Skip to first unread message

Maurice Waka

unread,
Jun 3, 2014, 5:50:48 AM6/3/14
to web...@googlegroups.com
from applications.Folders1.modules.myfolder1.main import main
from applications.Folders1.modules.myfolder1.list1 import GAMES0
code.....
if id == name3:
            result = location
            return result  

else:                
     while True:               
           class Filters(object):                        
                def Filter_cause(self):                            
                    for item in name3.split():
                        b = False
                        for c in FILTERTYPE:
                            b = b | (item.find(c) != -1)
                        if b:
                            for (func) in GAMES0:
                                func()
            o = Filters()
            for stuff in dir(o):                    
                if 'Filter' in stuff:
                    getattr(o, stuff)()

            result = Filters()
            return result

GAMES0 contains several python modules being imported. My loop works well up to the return location, section but on looping over the 'def class Filters', I get this message:

<applications.Folders1.modules.myfolder1.main.Filters object at 0x06D689F0>

How can I make it to print out my message from any of the modules

Derek

unread,
Jun 3, 2014, 3:15:23 PM6/3/14
to web...@googlegroups.com
Sorry, but it looks like it's doing what you are asking of it. Perhaps you should comment your code to make sure it's doing what you are thinking it's doing.
you are instantiating a new filters class, giving it the name 'o', then you get a list of the contents of that class, run them if possible, and then you are instantiating a new filters class, naming it 'result', kinda like 'o' was.... and then you are returning it. Why? What's the point of doing all that if 'o' is never going to be used? you might as well take that out since you are just returning a new class instance anyway.

Maurice Waka

unread,
Jun 3, 2014, 3:30:02 PM6/3/14
to web...@googlegroups.com

When I work with the same module on python shell, its perfectly printing out. I dont know why it does not print on web2py. Basically I imported it without editing from python shell. Anyway since am new to web2py and python, you could help out
> --
> 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/JsESNTvfrHQ/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.
>

--
Dr Maurice Waka, MBCHB.
Nairobi

Michael Beller

unread,
Jun 3, 2014, 3:51:26 PM6/3/14
to web...@googlegroups.com
Some of the code in the functions and classes not displayed in your original post may reference web2py globals and environment variables.

I think (I'm still learning web2py also) that running your code in the shell then has access to the web2py environment.  Modules do not have access automatically (unlike models and controllers and views).  You have to explicitly give your module functions and classes access to the web2py globals, environment, etc. either by importing them in the module or passing them as arguments.

Here's one thread that has some more information:

There's also a good slice on web2pyslices that gives an example.

Derek

unread,
Jun 4, 2014, 7:20:36 PM6/4/14
to web...@googlegroups.com
you running it from the shell gives you the REPL which you don't have when you run it through web2py. repl will print out if you just give it a class it will tell you what instance of class you have. if you want to print like it does in the repl, you need to 'print repr(x)' where x is the class or whatever you want.

Maurice Waka

unread,
Jun 5, 2014, 8:13:51 AM6/5/14
to web...@googlegroups.com
Thanks team!! That gives me an idea of how to program. Mush appreciated!

Maurice Waka

unread,
Jun 8, 2014, 4:11:59 AM6/8/14
to web...@googlegroups.com
I tried to work it out but I still have no breakthrough. Now I get this None  write response instead. Here is the code:

from applications.test.modules.Test1 import GAMES0
from applications.test.modules.Test1 import GAMES1
from applications.test.modules.Test1.cores import main
from gluon import *
def main():
    id = 0
    location = ""
    conn = sqlite3.connect("tools.db")
    c = conn.cursor()
    ........code

    name3 = ' '.join(name2)        
    
class Filters(object):
        def Filter_cause(self):
            for item in name3.split():
                b = False

                for c in FILTERCORE:

                    b = b | (item.find(c) != -1)
                if b:
                    for (func) in GAMES0:
                        func()

        def Filter_complications(self):

            for item in name3.split():
                b = False

                for c in FILTERCOMP:

                    b = b | (item.find(c) != -1)
                if b:

                    for (func) in GAMES1:

                        func()
    o = Filters()
    for stuff in dir(o):
        if 'Filter' in stuff:
            getattr(o, stuff)()

note that GAMES0 and GAMES1 contain lists of python modules that func() is calling to run. e.g. GAMES0 = [testcore1, testcore2, testcore3, testcore4]

import sys
import re

def testcore1():
    .....code
    name1 = ''.join(name).split()
    class FILTER_TESTCORE1(object):
        def test():
            for item in name1:break
            ...code
        result = test()
        return result

N/B with python shell. this works fine, but on web2py, i get the 'None' printout



On Thursday, June 5, 2014 3:13:51 PM UTC+3, Maurice Waka wrote:
Thanks team!! That gives me an idea of how to program. Much appreciated!


To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Maurice Waka

unread,
Jun 8, 2014, 4:12:22 AM6/8/14
to web...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages