Where do I put custom class files?

564 views
Skip to first unread message

monotasker

unread,
Sep 25, 2011, 3:28:45 PM9/25/11
to web...@googlegroups.com
Where should I put files with custom python classes to take care of business logic (and get it out of my controllers)? Do I have to set these up as a module and import them into th controller or will the classes automatically be available to any controller?

Thanks,

Ian

Anthony

unread,
Sep 25, 2011, 3:47:37 PM9/25/11
to web...@googlegroups.com
Modules have to be imported -- only classes/objects defined in model files are available in all controllers (and views). Note, by default, the web2py globals (i.e., request, response, session, validators, HTML helpers) will not be available in your modules, so you either have to pass them in as arguments to classes/functions, or use the new thread local 'current' object. For some examples of the latter, see http://martin.tecnodoc.com.ar/default/post/2011/09/12/7_optimize-your-web2py-app-using-the-new-import-method.

Anthony

monotasker

unread,
Sep 25, 2011, 3:55:56 PM9/25/11
to web...@googlegroups.com
Thanks. A couple of points of clarification:
1. Are the global web2py objects (session etc.) available to classes defined in a model file?
2. You mentioned the option of writing modules to import. What's the best location for these? In the 'static' directory?

Anthony

unread,
Sep 25, 2011, 8:07:26 PM9/25/11
to web...@googlegroups.com
On Sunday, September 25, 2011 3:55:56 PM UTC-4, monotasker wrote:
Thanks. A couple of points of clarification:
1. Are the global web2py objects (session etc.) available to classes defined in a model file?

Yes, if you define a class in a model, all the web2py globals would be available. One thing to note, because models are executed by the framework, make sure your classes do not include a __del__ method, as that can cause a memory leak.
 
2. You mentioned the option of writing modules to import. What's the best location for these? In the 'static' directory?

You should put modules in /myapp/modules. If you have /myapp/modules/mymodule.py, then all you have to do to import it is 'import mymodule.py', and the web2py custom importer will look in the application's 'modules' folder before checking the rest of sys.path.

Anthony

pbreit

unread,
Sep 25, 2011, 8:08:53 PM9/25/11
to web...@googlegroups.com
I thought it was just "import mymodule"?

And can you do "from mymodule import myfunc"?

Anthony

unread,
Sep 25, 2011, 8:13:07 PM9/25/11
to web...@googlegroups.com
On Sunday, September 25, 2011 8:08:53 PM UTC-4, pbreit wrote:
I thought it was just "import mymodule"?

Yes, of course it is -- lack of proofreading.
 

And can you do "from mymodule import myfunc"?

As far as I know.

Anthony 

monotasker

unread,
Sep 25, 2011, 9:32:24 PM9/25/11
to web...@googlegroups.com
This is great. Thanks again Anthony.

I'm surprised that this approach doesn't seem to be well documented in the (generally outstanding) web2py book. Is it not encouraged as 'the web2py way' or is it taken for granted that people with Python knowledge can figure it out? (Or have I just missed it in there?)

Ian

Anthony

unread,
Sep 25, 2011, 10:36:39 PM9/25/11
to web...@googlegroups.com
Both the custom importer and the 'current' object are relatively new (added since book's last major revision), so they're not documented yet (except in this mailing list). Prior to that, it was of course possible to use modules, but not quite as easy. The book will be revised soon, so these things will be documented.

Anthony

Vineet

unread,
Sep 26, 2011, 2:11:24 AM9/26/11
to web2py-users
I use a custom 3rd party class (named 'dabo') in web2py.
Is there any catch if I keep the class in /web2py/site-packages ?

Earlier I had kept the same in /myapp/modules folder.
But it could not be imported in model files.
So I shifted it to /web2py/site-packages.
With this, I can import the class 'dabo' in model files.

---Vineet
> > Ian- Hide quoted text -
>
> - Show quoted text -

Anthony

unread,
Sep 26, 2011, 9:45:04 AM9/26/11
to web...@googlegroups.com
On Monday, September 26, 2011 2:11:24 AM UTC-4, Vineet wrote:
I use a custom 3rd party class (named 'dabo') in web2py.
Is there any catch if I keep the class in /web2py/site-packages ?
 
site-packages is fine. The advantage of /app/modules is that you can have different versions of the same module in different apps. Also, if the module is specific to a given app, it makes more sense to keep it with the app. But for third party modules, site-packages makes sense, particularly if multiple apps need to access it.
 

Earlier I had kept the same in /myapp/modules folder.
But it could not be imported in model files.
 
You should be able to import modules in model files. Must have been a bug, either in your code or in web2py.
 
Anthony

Vineet

unread,
Sep 26, 2011, 10:28:33 AM9/26/11
to web2py-users
I will retry putting the class in modules folder.

Thanks,
Vineet
Reply all
Reply to author
Forward
0 new messages