I wrote a small function that will strip out either a list of namespaces (if you pass in ['http://ns1','http://ns2'] as nss) or all namespaces (if you pass in ['*'] as nss). Enjoy:
def stripns(x,nss):
a = {}
for t in x.attrib:
a[t] = x.attrib[t]
for dns in nss:
ns = '{'+dns+'}'
lenns = len(ns)
if ns=='{*}':
x.tag = x.tag[x.tag.find('}')+1:]
elif x.tag.find(ns)==0:
x.tag = x.tag[lenns:]
for i in x.attrib:
if ns=='{*}':
del a[i]
a[i[i.find('}')+1:]] = x.attrib[i]
elif i.find(ns)==0:
del a[i]
a[i[lenns:]] = x.attrib[i]
x.attrib = a
for i in x.findall('*'):
stripns(i,nss)
I ran into a problem compiling aggdraw on my slice. It turns out the problem was that I was in a 64-bit environment. It was complaining that a specific cast from one type to another was going to lose precision. There's a solution up here: http://timeless-scripts.blogspot.com/2007/08/patch-to-aggdraw-for-64-bit-machines.html
And I re-posted it here: http://www.pocketuniverse.ca/archive/2008/december/11/pil-and-aggdraw/
Thanks for the sample. I'll look into this as soon as I find the time.
Update: Your sample code is setting a class attribute (Myfile.isread) instead of an instance attribute (self.isread). Since class attributes are shared between all instances of a class, the second Myfile instance will always return an empty string -- which causes the second parser to raise a "ParseError: no element found" exception. If I fix that, both parsers successfully parse that XML snippet.
It is just an oberservation.
This is something we get as a result from a webservice:
--------------------------------------------------------
from elementsoap.ElementSOAP import namespace_parse
import elementtree.ElementTree as ET
Would you welcome the use of the XPath expression parser from pdis (http://sourceforge.net/projects/pdis-xpath/), but with a generator-oriented engine ? I gave it a try with a prototype and liked it.
What does "not as reliable" mean here? The reason ElementSoap uses a parser wrapper is that the standard parser doesn't preserve namespace prefixes, which are needed to resolve things like XSI types, fault codes, and other places where SOAP puts prefixes in parsed content.
we work with ElementSoap and found out that namespace_parse does not work as reliable as ET.parse. ET.parse seems to handle namespaces much better than namespace_parse does ;-)
I would suggest the following changes to the call function in ElementSOAP.py:
ET 1.3 has suffered from something of a scope creep (wrt. XPath and C14N), but the main reason for the delay is that I'm in the process of changing jobs. No exact ETA, in other words, but it's definitely moving in the right direction.
i'll try to contact you this way since i couldn't find an email on your site.
aggdraw. the curveto() doesn't work. it draws straight lines instead of curves.
the last post by tal leming regarding this is from march 2006. have you made progress in this field? i even tried the dev svn version.
i can't find any other way to draw PS outlines into an image, since the Path() of renderPM of reportlab isn't implemented yet, and drawing a PDF and imagemagicking it into an image is the least thing i would like to do.
i'll buy you any refreshment you like, if you help me on this.
thanks and best regards,
yanone
Hi, exemaker works =)
but can you someone tell me how things works
i must to have exemaker.exe and exemaker.py in Python25 folder where is python.exe? because in another place it show "Cannot find python.exe"
and second... i thought that when i created for example hello.exe that will work anywhere is it placed... if i executed from (c:\temp) , so hello.exe doesn't work and needs python.exe
thanks for help
Question... I have a prototype of a (better than ET's but still incomplete) Xpath engine, based on some pdis (http://sourceforge.net/projects/pdis-xpath/) code. I especially appreciated the XPath expression parser. I re-wrote the Xpath findall equivalent in pdis so that it yields the elements instead of creating lists.
Is that something you may want to see in ET, or do you want to keep ET XPath engine simpler?
It doesn't support file paths. It does support package names, but it doesn't really do what you expect: __import__('X.Y.Z') will indeed import the X, X.Y, and X.Y.Z modules and install them in sys.modules, but the function returns the X package module instead of the Z module.
Very clear, useful code and structure. As a beginner with python this module examples and many others have made my learning curve very much easier.
Thanks very much
I wrote a small function that will strip out either a list of namespaces (if you pass in ['http://ns1','http://ns2'] as nss) or all namespaces (if you pass in ['*'] as nss). Enjoy:
Thank you, nice explanation.
I ran into a problem compiling aggdraw on my slice. It turns out the problem was that I was in a 64-bit environment. It was complaining that a specific cast from one type to another was going to lose precision. There's a solution up here: http://timeless-scripts.blogspot.com/2007/08/patch-to-aggdraw-for-64-bit-machines.html
And I re-posted it here: http://www.pocketuniverse.ca/archive/2008/december/11/pil-and-aggdraw/
Thanks!
Congratulations :-)
Liked it! Read "Dive Into Python"-- great book-- but needed to catch up on more recent Python stuff.
You forgot to pass 'cls' to type.__init__.
http://code.google.com/p/benjhayden/source/browse/trunk/plugins.py
Opps, you are right. I have originally used it in the running system. And cutted some XML away. I will check again.
Thanks for the sample. I'll look into this as soon as I find the time.
Update: Your sample code is setting a class attribute (Myfile.isread) instead of an instance attribute (self.isread). Since class attributes are shared between all instances of a class, the second Myfile instance will always return an empty string -- which causes the second parser to raise a "ParseError: no element found" exception. If I fix that, both parsers successfully parse that XML snippet.
It is just an oberservation.
This is something we get as a result from a webservice:
--------------------------------------------------------
from elementsoap.ElementSOAP import namespace_parse
import elementtree.ElementTree as ET
xmltext = """<?xml version="1.0" encoding="ISO-8859-1" ?><ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"><ns0:Body><ns1:getMasterDataResponse xmlns:ns1="XeriAPI"><resultList command="getMasterData" version="1.0">
<user>
<title value="Jan"/>
</user>
<doctype>
<title value="Richtlinie T"/>
<title value="Formular"/>
</doctype>
<process>
<title value="Controlling"/>
</process>
<department>
<title value="Integral Development GmbH"/>
</department>
<category>
<title value="Beschaffung / Purchase"/>
</category>
<responsibleUnit>
<title value="Support"/>
</responsibleUnit>
<location>
<title value="Stockholm"/>
<title value="Paris"/>
<title value="Hamburg"/>
</location>
</resultList></ns1:getMasterDataResponse></ns0:Body></ns0:Envelope>
"""
class Myfile:
isread = False
def read(self, *args, **kw):
if Myfile.isread:
return ""
Myfile.isread = True
return xmltext
try:
res = ET.parse(Myfile())
print "ET.parse worked:", res
except:
print "ET.parse error!"
try:
res = namespace_parse(Myfile())
print "namespace_parse worked:", res
except:
print "namespace_parse error!"
--------------------------------------------------------
Would you welcome the use of the XPath expression parser from pdis (http://sourceforge.net/projects/pdis-xpath/), but with a generator-oriented engine ? I gave it a try with a prototype and liked it.
What does "not as reliable" mean here? The reason ElementSoap uses a parser wrapper is that the standard parser doesn't preserve namespace prefixes, which are needed to resolve things like XSI types, fault codes, and other places where SOAP puts prefixes in parsed content.
Hello Fredrik,
we work with ElementSoap and found out that namespace_parse does not work as reliable as ET.parse. ET.parse seems to handle namespaces much better than namespace_parse does ;-)
I would suggest the following changes to the call function in ElementSOAP.py:
------- snippet -----------
# *** changed - param 'parser' added
def call(self, action, request, header=None, parser=None):
...
# call the server
try:
response = self.__client.do_request(
ET.tostring(envelope),
extra_headers=[("SOAPAction", action)],
parser=parser # *** changed
)
except HTTPError, v:
if v[0] == 500:
# might be a SOAP fault
response = ET.parse(v[3]) # *** changed
...
------- snippet -----------
Thanks a lot!
Marcus
ET 1.3 has suffered from something of a scope creep (wrt. XPath and C14N), but the main reason for the delay is that I'm in the process of changing jobs. No exact ETA, in other words, but it's definitely moving in the right direction.
I've posted a workaround to the image-sig mailing list. It's probably not the best way to do it, but it'll hopefully get you going.
I like the path improvements in ET 1.3 a lot. Any idea when it will move beyond alpha?
hi fredrik,
i'll try to contact you this way since i couldn't find an email on your site.
aggdraw. the curveto() doesn't work. it draws straight lines instead of curves.
the last post by tal leming regarding this is from march 2006. have you made progress in this field? i even tried the dev svn version.
i can't find any other way to draw PS outlines into an image, since the Path() of renderPM of reportlab isn't implemented yet, and drawing a PDF and imagemagicking it into an image is the least thing i would like to do.
i'll buy you any refreshment you like, if you help me on this.
thanks and best regards,
yanone
The second example explained just what I've been trying for an hour now - getting Python/Readline auto-completion based on my own word-list.
Thanks!
PS: Next time I'll consult effbot first. ;)
Hi, exemaker works =)
but can you someone tell me how things works
i must to have exemaker.exe and exemaker.py in Python25 folder where is python.exe? because in another place it show "Cannot find python.exe"
and second... i thought that when i created for example hello.exe that will work anywhere is it placed... if i executed from (c:\temp) , so hello.exe doesn't work and needs python.exe
thanks for help
Question... I have a prototype of a (better than ET's but still incomplete) Xpath engine, based on some pdis (http://sourceforge.net/projects/pdis-xpath/) code. I especially appreciated the XPath expression parser. I re-wrote the Xpath findall equivalent in pdis so that it yields the elements instead of creating lists.
Is that something you may want to see in ET, or do you want to keep ET XPath engine simpler?
It doesn't support file paths. It does support package names, but it doesn't really do what you expect: __import__('X.Y.Z') will indeed import the X, X.Y, and X.Y.Z modules and install them in sys.modules, but the function returns the X package module instead of the Z module.
Also see: http://effbot.org/zone/import-string.htm
Interesting article. BTW, the line `raise SyntaxError("unknown operator)' is lacking a closing double-quote.
xml-scanner-example-4.py doesn't need the `pos = 0'.
The article points out trailing garbage is initially ignored, but doesn't point out that commas are optional in separating tuple members, e.g.
simple_eval("(1 2 3 4711.0)")
Very clear, useful code and structure. As a beginner with python this module examples and many others have made my learning curve very much easier.
Thanks very much