[Zope] Questions about DocumentTemplate.

Stefan Franke sfranke@cdc-group.com
Tue, 23 Mar 1999 13:40:24 +0100


	Mark Hammond wrote:

	>The solution I came up with was:
	>  
	>  class DotWrapper:
	>  	def __init__(self, ob):
	>  		self._obj_ = ob
	>  	def __getattr__(self, attr):
	>  		bits =  string.split(attr, '.') 
	>  		ob = getattr(self._obj_, bits[0])
	>  		for bit in bits[1:]:
	>  			ob = getattr(ob, bit)
	>  		return ob

	Small caveat: Since whitespaces could occur in an nested
attrbute
	access ("a.  b  .x") , it's better to string them first. An
alternative 
	approach for DotWrapper's __getattr__ could be (not tested):

	def __getattr__(self, attr):
	    bits = map (string.strip, string.split(attr, '.'))
	    return reduce (getattr, bits, self._obj)

	Stefan