Yes and no.
the mechanism of importing modules (in python and in any other
language) allows to get stuff out of the module (functions, variables)
but not in the module (request, response, cache, etc). So you have to
pass them. You do not want to pass them to every function so here is
the suggested trick:
#in module mymodule.py
class MyModule:
def __init__(self,request,response,session,cache,T,db):
(self.request,self.response,self.session,self.cache,self.T,self.db)=
(request,response,session,cache,T,db)
def my_function(self,a):
# use self.request, self.response, etc. etc as needed
return a
#in models of controllers
mymodule=MyModule(request,response,session,cache,T,db)
print mymodule.my_function('4')
I know this is somewhat ugly but there is no other way that I know of.
On Jul 13, 1:12 am, Dan <[email protected]> wrote:
> Maybe I'm doing something silly...
>
> I intend to run my code on GAE when I deploy it. For development and
> testing, I use SQLite to work out most of the bugs and then I test on
> GAE to find any GAE-specific bugs/issues. So I have some conditional
> import statements in the web2py controller files, which detect when
> the code is running on GAE and import two special GAE modules so that
> my code will work properly. These are the lines that are at the top of
> my controller, and they work properly for me:
>
> # for caching datastore requests
> if request.env.web2py_runtime_gae: # if running on Google App Engine
> from gluon.contrib.gae_memcache import MemcacheClient
> cache.ram=cache.disk=MemcacheClient(request)
> #else: # we will use the normal web2py caching method's cache.ram
>
> # for task queue support. we use this for offline processing for
> insertion and aggregation
> if request.env.web2py_runtime_gae: # if running on Google App Engine
> from google.appengine.api.labs import taskqueue
> import urllib
>
> However, my code is getting more complex and I have some routines that
> access the datastore that I would like to use in a few different
> controller files. So I would like to put the re-usable code into a
> custom module and then import that module into the controller files.
> The problem I'm running into is that I'm getting errors when using
> these conditional import statements at the top of my custom module,
> because the request object is not defined. Do I need to put the import
> statements INSIDE each function in my custom module, and pass the
> request object to each function? Or is there a better way?
>
> Maybe this is an easier way to ask this question: Is it possible to
> use "@cache" function decorators in a custom module - in both the
> typical web2py (running on SQLite) and GAE environments? If so, how
> would I import the necessary libraries to get this to work?
>
> Dan
>
> On Jul 12, 7:24 pm, mdipierro <[email protected]> wrote:
>
> > You can determine if it running on gae from
>
> > request.env.web2py_runtime_gae
>
> > You do not need any conditional import, perhaps I do not understand
> > the question.
>
> > On Jul 12, 8:40 pm, Dan <[email protected]> wrote:
>
> > > Sorry, I'm afraid I'm still not getting it.
>
> > > I can see how it would be possible to pass the request object to each
> > > function within my custom module, and then decide if it's appropriate
> > > to import the GAE-specific routines. This conditional import would
> > > need to happen in every function within my custom module, correct?
>
> > > But it would be nicer to avoid duplicating that import -- if it's
> > > possible to do the conditional import at the top of a module. It seems
> > > to me that a conditional import statement at the top of a custom
> > > module cannot successfully reference the request object -- because it
> > > is not contained in a function, so I don't know how to pass the
> > > request object to the module - is that possible? Or is there another
> > > way to determine if it's running on GAE?
>
> > > Dan
>
> > > On Jul 12, 8:42 am, mdipierro <[email protected]> wrote:
>
> > > > You can one one import statement at the top of the module.
>
> > > > As far as global objects, it depends. I suggest defining a class, pass
> > > > the globals to the contructor, store them, so that all methods have
> > > > access to them as needed.
>
> > > > On Jul 12, 9:42 am, Dan <[email protected]> wrote:
>
> > > > > ok, that makes sense. And would I need to put import statements
> > > > > inside each function in my custom module? Or can I do something to get
> > > > > that module-level reference to request/response/session/etc working?
>
> > > > > On Jul 11, 10:21 pm, mdipierro <[email protected]> wrote:
>
> > > > > > This is not a bad idea but there some rules to follow.
>
> > > > > > request, response, session, cache, T are global objects in web2py so
> > > > > > if you import a function from a module, you have to pass those 5
> > > > > > objects to the function (not all of them, only those you need). You
> > > > > > probably want to pass your database connection too.
>
> > > > > > All the other web2py keywords can be imported from your modules as
> > > > > > needed.
>
> > > > > > Massimo
>
> > > > > > On Jul 11, 5:48 pm, Dan <[email protected]> wrote:
>
> > > > > > > Hello-
> > > > > > > I want to re-use some data access routines in a few different
> > > > > > > controller files, so I would like to put them in a separate file
> > > > > > > in
> > > > > > > the /modules directory, and then import that file into the
> > > > > > > controller
> > > > > > > files that need them. However, I'm running into this error
> > > > > > > because I'm
> > > > > > > trying to use some web2py stuff in my module, like caching - and
> > > > > > > this
> > > > > > > is a conditional import that depends on accessing the request
> > > > > > > variable.
>
> > > > > > > in /modules/my_module.py:
> > > > > > > if request.env.web2py_runtime_gae: # if running on Google App
> > > > > > > Engine
> > > > > > > from gluon.contrib.gae_memcache import MemcacheClient
> > > > > > > cache.ram=cache.disk=MemcacheClient(request)
>
> > > > > > > error:
> > > > > > > name 'request' is not defined
>
> > > > > > > Another way to put it is that I'm building an abstraction layer
> > > > > > > on top
> > > > > > > of web2py's DAL so that I can reuse my code. Is this possible? Or
> > > > > > > maybe a bad idea?
>
> > > > > > > Dan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---