hey bill, try this:

# in models/0.py:

def mod_import(name):
  mod = __import__(name)
  for part in name.split('.')[1:]:
    mod = getattr(mod,part)
  return mod

config = mod_import('applications.
%s.modules.config'%request.application)

# in modules/config.py

import datetime
boottime = datetime.datetime.now()
title= 'My Time: '

# in controllers/default.py

def index():
  return str(config.title)+str(config.boottime)


-----------

It should print out the same boottime on each request because the
module is run once when it is first imported, which makes modules good
for configuration or for storing values that do not need to be
recomputed on each request.

Robin



On Jan 27, 9:47 am, Timothy Farrell <tfarr...@swgen.com> wrote:
> OK, so I've looked at it.  I like it.  Achipa is right that you'll have
> to protect drivers[] and known_plugins{} (possibly others) during
> load-time.  I think you should just use a lock-type rather than cache.ram().
>
> Cool, while web2py already supports many large databases, this will
> certainly clean up the code and make implementing more dbs easier.  It
> will need a large amount of testing though.
>
> -tim
>
>
>
> billf wrote:
> > Tim
>
> > Thanks for your response. I'm still a Python newbie, especially with
> > some of the more esoteric stuff.
>
> > With the plugin demo I have code in sql.py outside any class that
> > appears to only run the once (when sql.py is imported I assume) and,
> > as you suggest, it loads details of the available db-plugins.  I will
> > try wrapping the code in a function outside a class and see if it runs
> > in the same way - on import - or does it need to be named anything in
> > particular?
>
> > BTW in the case of the db-plugins all the data and functions are
> > static or class - you don't have to create an instance of a plugin.  I
> > don't know yet whether that is an advantage/disadvantage or
> > immaterial.
>
> > On the more general point, in the original question I was thinking
> > about how I would want to configure an application with regard to
> > dynamically loading modules, plugins, configuration options, etc.  If,
> > for example, I had a "Configurator" class, where would I import it?
>
> > If I understand things correctly:
>
> > - it couldn't be in a controller file as there may be an interaction
> > with the model in db.py and it may not have been imported by the time
> > db.py is run (is that correct?)
>
> > - it could be in db.py but that seems conceptually wrong as it may
> > have no db element at all
>
> > - any other ideas?
>
> > Coming from Java, it takes a bit of effort to get to grips with the
> > elasticity of Python :-)
>
> > On Jan 27, 2:14 pm, Timothy Farrell <tfarr...@swgen.com> wrote:
>
> >> I'm not sure what you're getting at here, but if you consider Python is
> >> a very dynamic language.  Whenever a module is imported, it is
> >> essentially run in it's own namespace.  In the case of your plugin
> >> (driver) system (which I haven't looked at yet), you could have an
> >> SQLDBFactory function that runs at import-time, loading the available
> >> db-engine plugins, and producing a functional SQLDB class.
>
> >> Reimporting a module does not re-run the module unless you call
> >> reload(modname).  If you have code that you want to run once, put it in
> >> a module that is normally imported (such as sql.db)
>
> >> This is the area where Javascript has made me a better Python
> >> programmer.  =)
>
> >> -tim
>
> >> billf wrote:
>
> >>> Basically, is there any code that receives control when an application
> >>> first starts that allows some initialisation/configuration that
> >>> doesn't have to run after every request?
>
> >>> I believe code could be put in db.py but that is not ideal
> >>> conceptually - and would run on every request?
>
> >>> I can see that there are pros and cons to the idea of "on start" code
> >>> and would be interested in peoples' views.
>
> >> --
> >> Timothy Farrell <tfarr...@swgen.com>
> >> Computer Guy
> >> Statewide General Insurance Agency (www.swgen.com)
>
> --
> Timothy Farrell <tfarr...@swgen.com>
> Computer Guy
> Statewide General Insurance Agency (www.swgen.com)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to