python ignorance shining through...
Hi all, I've got an external method that does an SQL lookup and retrieves (essentially) two fields, tagclass and content. What I'm doing at the moment is; if tagclass == 'modulecontributor': template_str = self.RENDER.modulecontributor.read_raw() template = DocumentTemplate.HTML(template_str) theresult = template(self, content=content) return theresult How can I do something like; try: template_str = self.RENDER.(the contents of tagclass).read_raw() template = DocumentTemplate.HTML(template_str) theresult = template(self, content=content) return theresult except KeyError: return "no renderer for %s" % tagclass any pointers gratefully received.. tone ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
Hi tone If I understand correctly you want to get the contents of an object but you only have the name and not a reference to it, correct? If that is the case then try this: try: template_str = eval("self.RENDER.%s.read_raw()" % tagclass) template = DocumentTemplate.HTML(template_str) theresult = template(self, content=content) return theresult except KeyError: return "no renderer for %s" % tagclass The eval will take a string and evaluate it in the current context, this should use the contents of tagclass to create a callable reference to that tagclass object. HTH Phil phil@philh.org ----- Original Message ----- From: Tony McDonald <tony.mcdonald@ncl.ac.uk> To: Zope List <zope@zope.org> Sent: Tuesday, September 14, 1999 12:33 PM Subject: [Zope] python ignorance shining through...
Hi all, I've got an external method that does an SQL lookup and retrieves (essentially) two fields, tagclass and content. What I'm doing at the moment is;
if tagclass == 'modulecontributor': template_str = self.RENDER.modulecontributor.read_raw() template = DocumentTemplate.HTML(template_str) theresult = template(self, content=content) return theresult
How can I do something like;
try: template_str = self.RENDER.(the contents of tagclass).read_raw() template = DocumentTemplate.HTML(template_str) theresult = template(self, content=content) return theresult except KeyError: return "no renderer for %s" % tagclass
any pointers gratefully received.. tone
------ Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
_______________________________________________ Zope maillist - Zope@zope.org http://www.zope.org/mailman/listinfo/zope
(To receive general Zope announcements, see: http://www.zope.org/mailman/listinfo/zope-announce
For developer-specific issues, zope-dev@zope.org - http://www.zope.org/mailman/listinfo/zope-dev )
At 1:09 pm +0100 14/9/99, Phil Harris wrote:
Hi tone
If I understand correctly you want to get the contents of an object but you only have the name and not a reference to it, correct?
corrrrect!
If that is the case then try this:
try: template_str = eval("self.RENDER.%s.read_raw()" % tagclass) template = DocumentTemplate.HTML(template_str) theresult = template(self, content=content) return theresult except KeyError: return "no renderer for %s" % tagclass
The eval will take a string and evaluate it in the current context, this should use the contents of tagclass to create a callable reference to that tagclass object.
sweeeet! (I actually changed the last bit to except: pass as I had some additional processing to do...). Nice one Phil, one I owe yer mate! :) Tone. ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
Tony McDonald wrote: [snippage]
template_str = eval("self.RENDER.%s.read_raw()" % tagclass)
[bobbit] This will certainly work, but here's a non-eval solution (I really dislike exec-ing and eval-ing): template_str = getattr(self.RENDER, tagclass).read_raw()
Yep, that's neat! See ya Phil ----- Original Message ----- From: Evan Simpson <evan@4-am.com> To: Tony McDonald <tony.mcdonald@ncl.ac.uk> Cc: Phil Harris <phil@philh.org>; Zope List <zope@zope.org> Sent: Tuesday, September 14, 1999 2:59 PM Subject: Re: [Zope] python ignorance shining through...
Tony McDonald wrote: [snippage]
template_str = eval("self.RENDER.%s.read_raw()" % tagclass)
[bobbit]
This will certainly work, but here's a non-eval solution (I really dislike exec-ing and eval-ing):
template_str = getattr(self.RENDER, tagclass).read_raw()
At 8:59 am -0500 14/9/99, Evan Simpson wrote:
Tony McDonald wrote: [snippage]
template_str = eval("self.RENDER.%s.read_raw()" % tagclass)
[bobbit]
This will certainly work, but here's a non-eval solution (I really dislike exec-ing and eval-ing):
template_str = getattr(self.RENDER, tagclass).read_raw()
wow. Tres Cool. A multitude of solutions! :) cheers Evan and Phil. Tone
participants (3)
-
Evan Simpson -
Phil Harris -
Tony McDonald