OO issues in python

John Landahl john at landahl.org
Fri Dec 19 22:40:55 EST 2003


Ali El Dada <eldada at mdstud.chalmers.se> wrote in message news:<brph4c$er0$1 at eol.dd.chalmers.se>...

> so do you advise me to take the second solution, creating a class called 
> Global that any other class can access?? as in: (not tested)
> 
> class Global:
> 	user_home = os.environ['HOME']
> 	my_app_data = 'C:\\Program Files\\MyApp'
> 
> class AnyClass:
> 	self.user_home = Global.user_home

Take a look at PEAK, the Python Enterprise Application Kit
(http://peak.telecommunity.com/).  It has advanced configuration and
binding capabilities that make this sort of thing incredibly simple
(and which provide far more power and flexibility), without needing to
resort to singletons like your Global class.

Using PEAK your AnyClass might look like this:

  from peak.api import binding, PropertyName
  class AnyClass(binding.Component):
      user_home = binding.Obtain(PropertyName('environ.HOME'))
      app_data  = binding.Obtain(PropertyName('myapp.data'))

binding.Obtain allows you to locate and "bind to" an object that's
been created elsewhere in the program.  Objects are located through
"configuration keys" -- in this case dotted "property names" are used,
though there are other choices which provide even more flexibility. 
Properties provide a simple way to organize data hierarchically (e.g.
'myapp.foo.bar.whatever') and which have a direct tie-in to external
.ini config files.  "environ.*" provides a simple way to lookup
environment variables, and "myapp.data" could be specified in a config
file like so:

  [myapp]
  data = 'C:\\Program Files\\MyApp'

There's a great introductory tutorial on the PEAK DevCenter wiki:

  http://peak.telecommunity.com/DevCenter/IntroToPeak

It walks you through creating simple applications with these features,
and moves on to more advanced PEAK features that you might also find
useful.




More information about the Python-list mailing list