[web2py] Re: read once definitions - development vs. production
You can move code to modules if you don't want it all executed on every request. In that case, you'll either have to use the 'current' object or pass global objects (such as 'db') to your module functions and methods. See http://web2py.com/book/default/chapter/04#Accessing-the-API-from-Python-modules. Anthony On Thursday, December 8, 2011 5:30:12 PM UTC-5, Constantine Vasil wrote: > > I used the PyDev+Eclipse+ GAE debugger extensively (after making it > working) > to see how web2py is working internally. > > Bottom line web2py is excellent for rapid development - while it is still > running, > any change in the model in 0.py (say) is available immediately after > browser refresh > because 0.py is read every time on browser refresh. > > This is perfect for development but for production kills the speed. > Actually after 0.py > is tweaked and reaches maturity I need to put it at read 'once mode'. > > This is much more important with GAE because for GAE you update all your > project > on the cloud, not just 0.py if changed, so this feature is useless for GAE > in production > environment and slows down the execution. > > In 0.py I have GAE models which are already stable and I have to be able > to put them > in a read once place. The question is how they would be available globally > too? > In ideal scenario if I have a class: > > class MyClass(db_google.Model) > name = dg_google.string(...) > def profile(self, id) > > > I would want to access the model like (the simplest possible way): > > myclass = MYClass.profile(id) > name = myclass.name > > > Is this possible in web2py? > > Regards, > --Constantine > > > > > > > >
[web2py] Re: read once definitions - development vs. production
BTW, do you have a large number of model definitions? Have you measured the performance hit? It might not be that big. Another option is conditional models: http://web2py.com/book/default/chapter/04#Workflow. You can define models that execute only for a particular controller and even a particular function within a controller. Anthony On Thursday, December 8, 2011 6:05:18 PM UTC-5, Anthony wrote: > > You can move code to modules if you don't want it all executed on every > request. In that case, you'll either have to use the 'current' object or > pass global objects (such as 'db') to your module functions and methods. > See > http://web2py.com/book/default/chapter/04#Accessing-the-API-from-Python-modules > . > > Anthony > > On Thursday, December 8, 2011 5:30:12 PM UTC-5, Constantine Vasil wrote: >> >> I used the PyDev+Eclipse+ GAE debugger extensively (after making it >> working) >> to see how web2py is working internally. >> >> Bottom line web2py is excellent for rapid development - while it is still >> running, >> any change in the model in 0.py (say) is available immediately after >> browser refresh >> because 0.py is read every time on browser refresh. >> >> This is perfect for development but for production kills the speed. >> Actually after 0.py >> is tweaked and reaches maturity I need to put it at read 'once mode'. >> >> This is much more important with GAE because for GAE you update all your >> project >> on the cloud, not just 0.py if changed, so this feature is useless for >> GAE in production >> environment and slows down the execution. >> >> In 0.py I have GAE models which are already stable and I have to be able >> to put them >> in a read once place. The question is how they would be available >> globally too? >> In ideal scenario if I have a class: >> >> class MyClass(db_google.Model) >> name = dg_google.string(...) >> def profile(self, id) >> >> >> I would want to access the model like (the simplest possible way): >> >> myclass = MYClass.profile(id) >> name = myclass.name >> >> >> Is this possible in web2py? >> >> Regards, >> --Constantine >> >> >> >> >> >> >> >>
[web2py] Re: read once definitions - development vs. production
I tried to move to modules but got the following issues: This is not possible - e.g. to acces the class directly without a prefix - when I have a lot of code if I cannot do that it is a lot of work adding a prefix. myclass = MyClass.profile(id) name = myclass.name Also if I have several related class definitions in a module - how to access them? There is a possibility to put every class in a separate module but it is not practical - my classes are interconnected. Also they use Google db.Model so the class definition is like: class MyClass(db_google.Model) I import Google imports successfully but not sure if every Google Model feature is working. Bottom line - I cannot just copy/paste my working and tested class definitions in a module(s) without a significant code rewrite, all is already tested and I don't want to tweak it in order to be accessible from various parts of the code - it just have to be available globally and used like: myclass = MyClass.profile(id) name = myclass.name Otherwise it is not practical - it needs a lot of work.
[web2py] Re: read once definitions - development vs. production
They are a lot of model definitions - it is a big project which I am porting to web2py. Django templates were easy to translate. GAE working with web2py - took me a lot of time be I did it. Making the PyDev+Ecilpse+GAE SDK environment to work with debugging - very hard but it is done. Now the final thing is to extract maximum performance - if CPU cycles a wasted reading over and over the same code - this is not good. My pure GAE class definitions - well I counted 10,000 lines - no -- this cannot be put in 0.py and read every single time a user hits refresh or changes a view. What will happen if I put them in routes.py? I know it is read once but do they will be accessible globally? Something like: myclass = MyClass.profile(id) name = myclass.name Without a code modification - these are another 25,000 lines of code? It has to be practical - this porting.
[web2py] Re: read once definitions - development vs. production
On Thursday, December 8, 2011 6:22:31 PM UTC-5, Constantine Vasil wrote: > > I tried to move to modules but got the following issues: > > This is not possible - e.g. to acces the class directly without a prefix - > when I have a lot of code if I cannot do that it is a lot of work adding > a prefix. > Are you saying: from mymodule import MyClass myclass = MyClass.profile(id) doesn't work?
[web2py] Re: read once definitions - development vs. production
OK let put it in reverse - how you would do this (GAE)/ in default.py myaccount = MyAccount.get_profile(user_id) Please note that 'in module' root = MyAccountRoot.get_profile() does not works - it says 'not available' or something. in default.py to import custom import you do that: from main import Main current.app.myapp = Main() current.app.myapp.common_services() So if it even works I have to change all my code to add current.app.myapp. before every call. Which is a lot of work. Regards, --Constantine = in module = from google.appengine.ext import db as google_db class MyAccountRoot(google_db.Model): global_counter = google_db.IntegerProperty(required=True, default=0) @classmethod def get_profile(self): root = Root.get_profile_del_cache() profile = MyAccountRoot(key_name='user_id', parent=root.key()) class MyAccount(google_db.Model): @classmethod def get_profile(self, user_id): root = MyAccountRoot.get_profile() profile = MyAccount.get_by_key_name(user_id, parent=root.key())
[web2py] Re: read once definitions - development vs. production
On Thursday, December 8, 2011 9:29:42 PM UTC-5, Constantine Vasil wrote: > > > OK let put it in reverse - how you would do this (GAE)/ > > in default.py > myaccount = MyAccount.get_profile(user_id) > What happens when you do: from module_that_defines_MyAccount import MyAccount myaccount = MyAccount.get_profile(user_id) Anthony
[web2py] Re: read once definitions - development vs. production
@Constantine Vasil I had a similat task earlier. I am using a third party library named 'DABO'. Placed that in 'site-packages' folder within 'web2py' (source) & imported that in models & controllers as required. (Before that, I had tried placing it in 'modules' folder, but could not import in controller/model, don't know why). After the first import, While importing it next time, there is no overhead (since python will not again import it, but read from .pyc file). Excuse me if I have not understood your question properely, but this's my take on your question, based on what I did for my similar problem. --- Vineet On Dec 9, 10:08 am, Anthony wrote: > On Thursday, December 8, 2011 9:29:42 PM UTC-5, Constantine Vasil wrote: > > > OK let put it in reverse - how you would do this (GAE)/ > > > in default.py > > myaccount = MyAccount.get_profile(user_id) > > What happens when you do: > > from module_that_defines_MyAccount import MyAccount > myaccount = MyAccount.get_profile(user_id) > > Anthony
[web2py] Re: read once definitions - development vs. production
Hi Vineet, Thank you! Absolutely - you understood my question properly. It seems that solution is very simple, I am so overwhelmed with this porting that sometimes I oversee the simplest solutions ;) So if I put it under site-packages like this: site-packages/models site-packages/definitions I can import them from everywhere from site-packages.models import models from site-packages.models import definitions and in /models I have: class MyClass(db_google.Model) name = dg_google.string(...) def profile(self, id) Then I can the model like this: myclass = MYClass.profile(id) name = myclass.name Is that right?
[web2py] Re: read once definitions - development vs. production
You should be able to do the same from the application's /modules folder. Use site-packages if it's a module that needs to be accessed by multiple applications (or possibly by other Python programs). Anthony On Saturday, December 10, 2011 7:09:32 PM UTC-5, Constantine Vasil wrote: > > Hi Vineet, > > Thank you! Absolutely - you understood my question properly. > It seems that solution is very simple, I am so overwhelmed > with this porting that sometimes I oversee the simplest > solutions ;) > > So if I put it under site-packages like this: > site-packages/models > site-packages/definitions > > I can import them from everywhere > from site-packages.models import models > from site-packages.models import definitions > > and in /models I have: > > class MyClass(db_google.Model) > name = dg_google.string(...) > def profile(self, id) > > > Then I can the model like this: > > myclass = MYClass.profile(id) > name = myclass.name > > Is that right? > >
[web2py] Re: read once definitions - development vs. production
I just put my classes in site-packages/models.py and the 'from models import *' made the classes available globally. So if I put them in /modules/models.py it would be the same?
[web2py] Re: read once definitions - development vs. production
As Anthony pointed out rightly, if you need that module for a specific app, then put it in modules folder. If that module is required to be imported in other apps as well, then put it in site-packages folder. HTH -- Vineet On Dec 11, 9:29 am, Constantine Vasil wrote: > I just put my classes in site-packages/models.py > > and the 'from models import *' made the classes available globally. > > So if I put them in /modules/models.py it would be the same?
[web2py] Re: read once definitions - development vs. production
On Saturday, December 10, 2011 11:29:49 PM UTC-5, Constantine Vasil wrote: > > I just put my classes in site-packages/models.py > > and the 'from models import *' made the classes available globally. > > So if I put them in /modules/models.py it would be the same? > It should work the same -- let us know if it doesn't. web2py includes a custom importer, so if you use a regular import statement, before searching the Python path, it will look in the application's /modules folder. See http://web2py.com/book/default/chapter/04#Third-Party-Modules and http://web2py.com/book/default/chapter/04#Accessing-the-API-from-Python-modules. Anthony
[web2py] Re: read once definitions - development vs. production
Thank you, I tested it and it works both ways.
[web2py] Re: read once definitions - development vs. production
Just to confirm: If I put a code in /modules folder is it imported every time on each function request? I want it imported on demand only when I need it.
[web2py] Re: read once definitions - development vs. production
No. code in models is executed at every request. Code in modules is importent only once and cached. It is executed when functions in the module are called (as in normal python). On Dec 11, 2:51 pm, Constantine Vasil wrote: > Just to confirm: > > If I put a code in /modules folder is it imported every time > on each function request? > > I want it imported on demand only when I need it.
[web2py] Re: read once definitions - development vs. production
Thank you , Massimo!