[Tutor] Namespace variables vs. instantiation keywords - best practices

Tim Johnson tim at johnsons-web.com
Thu Feb 3 20:19:51 CET 2011


FYI: Python 2.6.5 on Linux.
FYI: I am a web programmer of 24 years experience programming, 9
with python, but have never had the advantage of working with a
senior programmer as a mentor. I am investigating the best practices
of instantiating an object with a large amount of options. I would
also request that when the reader sees me using terminology that is
not pythonic that I be corrected. That will help me to better
communicate my practices and researching this topic via search
engines.

I'm redesigning a large system to an MVC architecture. 
To make a long story short, I and clients have chosen *not* to adapt
this system to an existing framework such as django, but to develop
and original framework with an eye towards integrating with
something like django in the future.

# Bootstrapping:
An executable script imports a 'site module' that sets up a
project-specific environment. 

# Managing content
I have developed a view (template) module for which most of the
heavy lifting is done. 

The interface to the class looks like this:
def __init__(self,**kw):
## Consider that all code examples are pseudo-code
"""
 tmpl.py
 Purpose - python templating features
 Author - Me - Me at mydomain.com
"""
## I will use two public module variables as an example:
## default variables in the module namespace
templatepath = "templates" ## default value
projectname = ""           ## must be intialized 

## Object variables in the class namespace
## Initialized from the public module variables
self.templatepath = templatepath 
self.prj = projectname

## Intialization loop. Defaults can be 'overridden'
    for k in kw:
        if k in self.__dict__:
            self.__dict__[k] = kw[k]
        else : ## raise exception

#instantiation code might look like this:
content = LoadView(prj="myproject",templatepath="views")

# or 
kws = {"prj":"myproject","templatepath":"views"}
content = LoadView(**kws)

# OR (project config file)
kws = load.config("myconfig","tmpl_kws")
kws.update({"prj":"myproject","templatepath":"views"})
kws = {"prj":"myproject","templatepath":"views"}

# OR - use the site module - example site module code follows:
## Import the module containing the `LoadView' class
import tmpl
## set the module namespace variables.
tmpl.projectname = MyProject    
tmpl.templatepath = TemplatePath
## Calling module settings follow.....

I've received some very helpful comments in the past by senior
members of this ML and perhaps this topic may help other as
well as myself
TIA
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com


More information about the Tutor mailing list