From xml data to python code to run
Fredrik Lundh
fredrik at pythonware.com
Wed Aug 30 09:50:07 EDT 2006
ralfbrand50 at gmail.com wrote:
> Is it possible to run data that's from an xml file as python code, by
> this I mean the following.
>
> In a xml file there's the following data
>
> <code><![CDATA[self.wTree.signal_autoconnect(dic)]]></code>
>
> Now I want to extract the self.wTree.signal_autoconnect(dic)* from the
> xml file and make it possible that Python runs this, instead of having
> a python file were with the hardcoded self.wTre.. in it.
from elementtree import ElementTree as ET
text = """<doc><code><![CDATA[object.method('hello')]]></code></doc>"""
doc = ET.fromstring(text)
class Object:
def method(self, value):
print value
# populate the namespace
namespace = {
"object": Object()
}
# execute all code fragments
for code_elem in doc.findall(".//code"):
exec code_elem.text in namespace
</F>
More information about the Python-list
mailing list