How to use functions defined in models, in modules?

37 views
Skip to first unread message

mfarees...@gmail.com

unread,
Apr 18, 2016, 6:06:26 AM4/18/16
to web2py-users
There is a function in models/utils.py called int_or_None. I'd like to be able to use this functions in modules. Is there a way to do this? If not, where would be the best place to keep this int_or_None function in order to make it accessible to the entire application? Thanks.

Leonel Câmara

unread,
Apr 18, 2016, 6:13:37 AM4/18/16
to web2py-users
This could be done, but it's a bad idea, just put the function itself in a module too instead of defining it in the models.

mfarees...@gmail.com

unread,
Apr 18, 2016, 6:17:26 AM4/18/16
to web2py-users
I want to use this function in multiple module files. Copying it over to each one of those files does not seem like a good idea. Is there some other approach to tackle this cleanly? 

Anthony

unread,
Apr 18, 2016, 8:28:36 AM4/18/16
to web2py-users
On Monday, April 18, 2016 at 6:17:26 AM UTC-4, mfarees...@gmail.com wrote:
I want to use this function in multiple module files. Copying it over to each one of those files does not seem like a good idea. Is there some other approach to tackle this cleanly? 

No, just put it in one module file and import it into any others where you need it -- that is the way Python works.

Anthony

mfarees...@gmail.com

unread,
Apr 18, 2016, 9:07:35 AM4/18/16
to web2py-users
I tried that but it did not work. I added the function to modules/common.py. Then I used import common and called the function using common.int_or_None(x). After doing this I ran my pytests but they failed on the import statement. This is the error I got:
'thread._local' object has no attribute 'auth'

Anthony

unread,
Apr 18, 2016, 9:13:55 AM4/18/16
to web2py-users
Sounds like a problem in your code. We need to see the code and the traceback.
Reply all
Reply to author
Forward
0 new messages