Best practices: When to use classes

183 views
Skip to first unread message

lyn2py

unread,
Dec 1, 2011, 1:05:24 AM12/1/11
to web2py-users
Hi,

I'm obviously super new at this. I appreciate any gentle pointers,
guidelines and best practices on when to use classes when writing code
with web2py (and/or python for that matter).

I ask this because:
- In Ruby on Rails, everything is put under a class of some kind
- I have briefly seen a few discussion posts on here that have used
classes in their code
- I want to learn from experienced python coders (like you!)

Thank you.

pbreit

unread,
Dec 1, 2011, 4:05:17 AM12/1/11
to web...@googlegroups.com
I wouldn't worry about classes at this point. You don't need to use them at all in Web2py.

Alan Etkin

unread,
Dec 1, 2011, 6:24:09 AM12/1/11
to web2py-users
Martin Mulone has made a great work documenting new web2py features
and presents some tricks on using class oriented web2py:
His work has not been translated to english yet (it is in spanish):

http://www.slideshare.net/martinpm/web2py-pensando-en-grande-9448110

lyn2py

unread,
Dec 1, 2011, 8:12:53 AM12/1/11
to web2py-users
Thanks Alan for sharing the link to Martin's slides.

I can't read Spanish so I don't understand most of it, but I briefly
caught the idea to use classes in modules (define tables).

pbreit

unread,
Dec 1, 2011, 6:23:45 PM12/1/11
to web...@googlegroups.com
I forgot that there are some parts of Web2py that do use classes such as Virtual Fields. For those, I would suggest just copy/pasting from the Book and then editing.

Bruno Rocha

unread,
Dec 1, 2011, 7:48:29 PM12/1/11
to web...@googlegroups.com
I have an opinion about that.

For me the /models folders should be renamed to /scripts since it has scripts that are executed in every request. Going on this approach I am only writing model-less (or script-less) apps.

I am now recommending to all of my students and clients to avoid the use of models and go to class based system in modules, now with thread locals object and custom_importer it is very easy to create an application without using any model file.

I encourage you to learn more about O.O in Python, then after that you can find a better way to create your web2py applications using modules and classes.


--

Nik Go

unread,
Dec 1, 2011, 7:56:41 PM12/1/11
to web...@googlegroups.com
Interesting.  Do you have any recommended links to know more about this topic?

Anthony

unread,
Dec 1, 2011, 8:01:01 PM12/1/11
to web...@googlegroups.com
On Thursday, December 1, 2011 7:48:29 PM UTC-5, rochacbruno wrote:
I have an opinion about that.

For me the /models folders should be renamed to /scripts since it has scripts that are executed in every request.

I suppose you could be strict about it and only include model related code in model files, in which case calling them "models" is appropriate. I'm not sure "scripts" is a better name -- the word "script" doesn't imply "run on every request" any more than "model" does.

Also, if you don't like having all models run on every request, another alternative is using conditional models (perhaps we could make that more flexible).

I am now recommending to all of my students and clients to avoid the use of models and go to class based system in modules

Using classes and putting things in modules seem to be two separate issues. You could define classes in models or use a non-class based approach in modules.

Also, isn't it sometimes useful to put code in a model file that you want to run on every (or nearly every) request without having to do an import?

Anthony

lyn2py

unread,
Dec 1, 2011, 8:17:54 PM12/1/11
to web2py-users
Just a thought.

Model files are run on every request - does that mean having a complex
database structure would slow the entire site down?

Bruno Rocha

unread,
Dec 1, 2011, 8:19:05 PM12/1/11
to web...@googlegroups.com


On Thu, Dec 1, 2011 at 11:01 PM, Anthony <abas...@gmail.com> wrote:
Also, isn't it sometimes useful to put code in a model file that you want to run on every (or nearly every) request without having to do an import?

Yes, some helping functions and objects i think is very useful to have in modules, but not Auth, Crud, Mail, db and defined tables. obviously I am talking about a large app. for small apps I see no problem on having [auth, crud, service, db, mail, etc..] loaded in every request even if it is only an ajax callback or another simple page.

But for large apps which uses ajax a lot, I think it is not needed to load all that objects/instances or define all tables in every request. So in that cases I am going to use /modules. I created a template app with basemodels and handlers. so I call then directly in controller only what I need and when I need to use.

For example, I dont need crud and auth for some counter functions that I use as ajax callbacks.

Example, in a system where I have 100+ tables in DB, so I need an simple ajax callback to return something related to only one of the tables, why I need to define every one of them? In this ajax callback I dont need auth, crud service or nothing more.. I only need access to one table to return a simple string.

in controller:

def myajaxcallback():
    from datamodel.myobject import MyClass
    myobject = MyClass('myparams')
    return myobject.counter()
   
in module:

from gluon import current
from basemodel import BaseModel # a place where I have code which uses class attributes to define the tables

class MyClass(BaseModel):
    def __init__(self, params):
        from mydb import DataBase # a custom subclass of DAL
        self.db = DataBase([theonlytableIneed])  # will return DAL instance with only the table I need defined
  
    def counter(self):
        arg = current.request.args(0)
        return self.db(self.db.theonlytableIneed.field == arg).select(cache=(cahe.ram, 300))

I already tried to use submodels but I find it very useful for small apps, but large apps need more class based system to be more reusable.

Bruno Rocha

unread,
Dec 1, 2011, 8:30:17 PM12/1/11
to web...@googlegroups.com


On Thu, Dec 1, 2011 at 11:17 PM, lyn2py <lyn...@gmail.com> wrote:
Model files are run on every request - does that mean having a complex
database structure would slow the entire site down?

Yes, if you everything you have in models will be executed even if you are just serving a simple contact form or an small string in ajax callback.

For solving this you can use submodels (subfolders in models which maps to the requested controller/function) or you can create your system in modules and import only what you need explicity in every controller/function you create.

I think submodels are for small apps because it does not permits the reuse of submodels for multiple controllers. I prefer to code in /modules

an example of the approach:  https://github.com/rochacbruno/Movuca (the sample app running http://labs.blouweb.com/movuca2/home/index)

Specially if the application are being created to run on Google App Engine, where CPU costs a lot.

Anthony

unread,
Dec 1, 2011, 10:29:17 PM12/1/11
to web...@googlegroups.com
Cool. Thanks for sharing.

Bruno Rocha

unread,
Dec 1, 2011, 11:43:39 PM12/1/11
to web...@googlegroups.com

Actually you dont need even an ajax callback.

Take this example:

## models/script.py ##

db.define_table("mytable", Field("picture", "upload"))
print "models executed!"

## controllers/default.py ##

def index():
    pictures = db(db.mytable).select()
    return dict(pictures=pictures)

def download():
    return response.download(request, db)

## views/default/index.html ##

{{for picture in pictures:}}
    <img src="{{=URL('default', 'download', args=picture.picture)}}">
{{pass}}


Let's say the table has 200 records and I want to show 200 thumbnails in the page.

Q: How many times you will see "models executed!" in terminal? and how many times the table will be defined? the Auth, Crud, Service etc will be instantiated?

A: 201 times. 1 for the first request and 200 for each image which calls the 'download' function.

Now imagine a system which huge model files, too many tables and calling long running funtions.

my tips:

1. Avoid models, use modules and import what you need in controller.

2. Avoid the download function, put uploadfolder='static' and build URL('static') when need an image. Or put your download function in a separate application which have access to the file system and database.

kenji4569

unread,
Dec 2, 2011, 5:53:56 AM12/2/11
to web2py-users
I think there are two types of web applications; one is for admin-
site, and the other is for front-site. For admin-site, CPU-usage would
be relatively not so critical, and rich man's programming (fat models
in web2py) could be allowed. Front-site, on the other hand, needs
optimization as far as possible, which put developers some works.

Kenji

Lazarof

unread,
Dec 2, 2011, 2:00:04 PM12/2/11
to web2py-users
Optimize your web2py app using the new import method

http://martin.tecnodoc.com.ar/default/post/2011/09/12/7_optimize-your-web2py-app-using-the-new-import-method

> > 1. Avoid models,usemodules and import what you need in controller.

pbreit

unread,
Dec 2, 2011, 2:46:36 PM12/2/11
to web...@googlegroups.com
I'm interested in this approach and these samples are very helpful but it seems like there is still a ways to go. Would it be possible to create some sort of welcome-like model-less bootstrap?

Richard Vézina

unread,
Dec 2, 2011, 2:57:12 PM12/2/11
to web...@googlegroups.com
Pretty much interrested too... I would start with menu.py since I use conditional group menu.

What do I have to do...

Richard

Bruno Rocha

unread,
Dec 2, 2011, 3:20:05 PM12/2/11
to web...@googlegroups.com


On Fri, Dec 2, 2011 at 5:46 PM, pbreit <pbreit...@gmail.com> wrote:
I'm interested in this approach and these samples are very helpful but it seems like there is still a ways to go. Would it be possible to create some sort of welcome-like model-less bootstrap?

Look in to my modules here: https://github.com/rochacbruno/Movuca/tree/master/modules

BaseModel, BaseHandler and custom DataBase and Access
Reply all
Reply to author
Forward
0 new messages