I think so. Based on the way web2py applications should not modify
the sys.path, this kind of import should be encouraged.
Web2py utilities like this should be collected under one object in the
exec environment (eg. utils ).
my_apps_helpers = utils.import('helpers',request=request)
Robin
On Feb 8, 5:48 pm, mdipierro <[email protected]> wrote:
> should this be in web2py? Pros/cons?
>
> Massimo
>
> On Feb 8, 5:38 pm, Robin B <[email protected]> wrote:
>
> > Here is another version of mod_import() that does not use string
> > splitting:
>
> > def mod_import(name):
> > import sys
> > mod = sys.modules.get(name,None)
> > if mod is None:
> > __import__(name)
> > mod = sys.modules[name]
> > return mod
>
> > Robin
>
> > On Jan 8, 1:07 pm, "Yarko Tymciurak" <[email protected]> wrote:
>
> > > Ooooh! starting to look yummy... :-)
>
> > > On Thu, Jan 8, 2009 at 1:04 PM, Robin B <[email protected]> wrote:
>
> > > > > def mod_import(name,request=None):
> > > > > if request: name='applications.%s.modules.%s' %
> > > > > (request.application, name)
> > > > > mod = __import__(name)
> > > > > for part in name.split('.')[1:]:
> > > > > mod = getattr(mod,part)
> > > > > return mod
>
> > > > Nice, I like how t2 collects helpers under a single object without
> > > > polluting the global namespace. What if these helpers could be
> > > > collected under a single object like t2?
>
> > > > # in gluon/helpers.py or something
> > > > def mod_import(name):
> > > > mod = __import__(name)
> > > > for part in name.split('.')[1:]:
> > > > mod = getattr(mod,part)
> > > > return mod
>
> > > > class Helpers(...):
> > > > def __init__(self,request):
> > > > self.request = request
>
> > > > def import(self,name):
> > > > return mod_import('applications.%s.modules.%s'%
> > > > (self.request.application,name))
>
> > > > # in models/0.py
> > > > T2 = helpers.import('t2').T2
>
> > > > > On Jan 8, 12:23 pm, Robin B <[email protected]> wrote:
>
> > > > > > def mod_import(name):
> > > > > > mod = __import__(name)
> > > > > > for part in name.split('.')[1:]:
> > > > > > mod = getattr(mod,part)
> > > > > > return mod
>
> > > > > > T2 = mod_import('applications.%s.modules.t2'%request.application).T2
> > > > > > t2=T2(request,response,session,cache,T,db)
> > > > > > ...
>
> > > > > > Benefits: does not modify sys.path and does not resort to exec'ing.
>
> > > > > > Robin
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---