Re: how to get next month string?
Sounds like a job for dateutil (http://labix.org/python-dateutil). It's not a built in module (it's in the cheeseshop at least), but it looks like it pretty much does exactly what you want. If you really dont want to download anything, I suppose you could create something from datetime's timedeltas which do support arithmetic operations. -- http://mail.python.org/mailman/listinfo/python-list
Re: the one python book
On Aug 4, 6:23 am, "dhr" <[EMAIL PROTECTED]> wrote:
Assuming that you already know that a ton of great stuff is on the
web, I'd also recommend 'Programming Python' and 'Python Cookbook'.
'The Python Book' would have to be the Python book ('Programming
Python', cuz it has a python on the cover and covers the python
language, brilliant!) It's huge and is full of great scripts and,
most importantly, promotes good coding style. Hopefully they've
updated the index tho, since the 2nd edition has an utterly pathetic
index, tho you can still find what you want pretty quickly with just
the toc.
Python Cookbook is necessary because it covers so many little hidden
secrets of python. The section on dateutil is worth the price of the
book alone.
--
http://mail.python.org/mailman/listinfo/python-list
Adjusting the names of custom exceptions (since raising strings is deprecated)
Heyas So this probably highlights my lack of understanding of how naming works in python, but I'm currently using FailUnlessRaises in a unit test and raising exceptions with a string exception. It's working pretty well, except that I get the deprecation warning that raising a string exception is going to go away. So my question is, how do I mangle the name of my exception class enough that it doesnt stick the name of the module before the name of the exception? Namely I'd like to get the following *** Traceback (most recent call last): File "", line 1, in ? MyError: 'oops!' instead of *** Traceback (most recent call last): File "", line 1, in ? __main__.MyError: 'oops!' (or even test_thingie.MyError as is usually the case). Creating a class in a separate file and then doing *** from module import MyError raise MyError still gives *** Traceback (most recent call last): File "", line 1, in module.MyError Anyway, any help appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Adjusting the names of custom exceptions (since raising strings is deprecated)
On Aug 21, 1:32 am, James Stroud <[EMAIL PROTECTED]> wrote: > Silfheed wrote: > > Heyas > > > So this probably highlights my lack of understanding of how naming > > works in python, but I'm currently using FailUnlessRaises in a unit > > test and raising exceptions with a string exception. It's working > > pretty well, except that I get the deprecation warning that raising a > > string exception is going to go away. So my question is, how do I > > mangle the name of my exception class enough that it doesnt stick the > > name of the module before the name of the exception? > > > Namely I'd like to get the following > > > *** > > Traceback (most recent call last): > > File "", line 1, in ? > > MyError: 'oops!' > > > instead of > > > *** > > Traceback (most recent call last): > > File "", line 1, in ? > > __main__.MyError: 'oops!' > > > (or even test_thingie.MyError as is usually the case). > > > Creating a class in a separate file and then doing > > > *** > > from module import MyError > > raise MyError > > > still gives > > > *** > > Traceback (most recent call last): > > File "", line 1, in > > module.MyError > > > Anyway, any help appreciated. > > Would it be cheating to use metaclasses? > > # myModule.py > class ExampleType(type): >def __repr__(cls): > return cls.__name__ > > class ExampleError(Exception): >__metaclass__ = ExampleType >__name__ = 'ExampleError' >def __repr__(self): > return 'ExampleError' > > py> import myModule > py> raise myMo > myModule myModule.py myModule.pyc myModule.py~ > py> raise myModule.Ex > myModule.ExampleError myModule.ExampleType > py> raise myModule.ExampleError > > Traceback (most recent call last): >File "", line 1, in > ExampleError > > James It doesnt appear to work for me. Same exact code as you have but I still get: >>> raise myModule.ExampleError Traceback (most recent call last): File "", line 1, in myModule.ExampleError -- http://mail.python.org/mailman/listinfo/python-list
Re: Adjusting the names of custom exceptions (since raising strings is deprecated)
On Aug 21, 1:53 pm, Peter Otten <[EMAIL PROTECTED]> wrote:
> Silfheed wrote:
> > On Aug 21, 1:32 am, James Stroud <[EMAIL PROTECTED]> wrote:
> >> Silfheed wrote:
> >> > Heyas
>
> >> > So this probably highlights my lack of understanding of how naming
> >> > works in python, but I'm currently using FailUnlessRaises in a unit
> >> > test and raising exceptions with a string exception. It's working
> >> > pretty well, except that I get the deprecation warning that raising a
> >> > string exception is going to go away. So my question is, how do I
> >> > mangle the name of my exception class enough that it doesnt stick the
> >> > name of the module before the name of the exception?
>
> >> > Namely I'd like to get the following
>
> >> > ***
> >> > Traceback (most recent call last):
> >> > File "", line 1, in ?
> >> > MyError: 'oops!'
>
> >> > instead of
>
> >> > ***
> >> > Traceback (most recent call last):
> >> > File "", line 1, in ?
> >> > __main__.MyError: 'oops!'
>
> >> > (or even test_thingie.MyError as is usually the case).
>
> >> > Creating a class in a separate file and then doing
>
> >> > ***
> >> > from module import MyError
> >> > raise MyError
>
> >> > still gives
>
> >> > ***
> >> > Traceback (most recent call last):
> >> > File "", line 1, in
> >> > module.MyError
>
> >> > Anyway, any help appreciated.
>
> >> Would it be cheating to use metaclasses?
>
> >> # myModule.py
> >> class ExampleType(type):
> >>def __repr__(cls):
> >> return cls.__name__
>
> >> class ExampleError(Exception):
> >>__metaclass__ = ExampleType
> >>__name__ = 'ExampleError'
> >>def __repr__(self):
> >> return 'ExampleError'
>
> >> py> import myModule
> >> py> raise myMo
> >> myModule myModule.py myModule.pyc myModule.py~
> >> py> raise myModule.Ex
> >> myModule.ExampleError myModule.ExampleType
> >> py> raise myModule.ExampleError
> >>
> >> Traceback (most recent call last):
> >>File "", line 1, in
> >> ExampleError
>
> >> James
>
> > It doesnt appear to work for me.
> > Same exact code as you have but I still get:
>
> >>>> raise myModule.ExampleError
> > Traceback (most recent call last):
> > File "", line 1, in
> > myModule.ExampleError
>
> James tested his code in the ipython console which obviously uses a
> different routine to produce the traceback.
>
> Try
>
> >>> class MyError(Exception):
>
> ... __module__ = None
> ...>>> raise MyError("oops")
>
> Traceback (most recent call last):
> File "", line 1, in
> MyError: oops
>
> Peter
Ah ha! Thanks, that worked great!
--
http://mail.python.org/mailman/listinfo/python-list
Python automatic testing: mocking an imported module?
Heyas So we have the following situation: we have a testee.py that we want to automatically test out and verifiy that it is worthy of being deployed. We want our tester.py to test the code for testee.py without changing the code for testee.py. testee.py has a module in it that we want to mock in some tests and in others use the real module. /foo.py: (real module) class bar: def __init__(self): "I am real" /foo_fake/foo.py: (fake module) class bar: def ___init__(self): "I am a banana" /testee.py: import foo foo.bar() /tester.py: from foo_fake import foo foo.bar()# prints I am a banana testee.py # also prints I am a banana import foo foo.bar() # prints I am real testee.py # also prints I am real This isnt working as we would like, does anyone have any tips on how to get something like this working? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python automatic testing: mocking an imported module?
Wow, that works great! Thanks all!
On Mar 28, 12:02 am, Ben Finney <[EMAIL PROTECTED]>
wrote:
> "Silfheed" <[EMAIL PROTECTED]> writes:
> = foo.py =
> class Bar(object):
> def __init__(self):
> self.name = "bar"
> =
>
> = dostuff.py =
> import foo
>
> def get_bar():
> return foo.Bar()
> =
>
> = test_dostuff.py =
> import dostuff
>
> class Mock_foo_module(object):
> """ Mock object for foo module """
>
> class Bar(object):
> def __init__(self):
> self.name = "banana"
>
> def test_dostuff_get_bar_should_create_bar():
> """ The dostuff.get_bar function should create a new Bar """
> dostuff.foo = Mock_foo_module()
> test_bar = dostuff.get_bar()
> if not test_bar.name == "banana":
> raise AssertionError("test_bar.name should be banana")
> =
>
> This is simply using an instance of a class (Mock_foo_module) to be a
> mock 'module' object. That mock module has the required 'Bar'
> attribute, bound to a class; so the 'dostuff' module's 'foo' attribute
> can be replaced with our mock module object for the purpose of the
> test.
>
> The mock module's Bar class behaves in an idiomatic way (setting the
> 'name' attribute of its instance to a known value) that we use to
> check whether the 'dostuff' module actually used our mock module.
>
> This can be extended to mock any module interface you like, and then
> confirm that the module under test has actually used the module as
> expected.
>
> --
> \ "A politician is an animal which can sit on a fence and yet |
> `\ keep both ears to the ground." -- Henry L. Mencken |
> _o__) |
> Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list
PYTHONPATH, opensuse10.2, gtk not working
Heyas So I'm trying to do two things, install a little gnome taskbar applet (timer-applet) that was written in python and experiment with writing python gtk apps. I've installed (through yast) gtk, gtk2, gtk2-devel, python-gtk, python-gtk-devl, python-gtk-doc but I still cant get python to import gtk. I set PYTHONPATH = /usr/lib/python2.5/site- packages/ and that got importing pygtk to work, but the little app I downloaded wants to import gtk and I just cant figure out what I'm supposed to set PYTHONPATH to in order to get `import gtk` to work. Anyone know what I'm doing wrong or missing? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONPATH, opensuse10.2, gtk not working
On Oct 4, 7:39 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > Silfheed wrote: > > Heyas > > > So I'm trying to do two things, install a little gnome taskbar applet > > (timer-applet) that was written in python and experiment with writing > > python gtk apps. I've installed (through yast) gtk, gtk2, gtk2-devel, > > python-gtk, python-gtk-devl, python-gtk-doc but I still cant get > > python to import gtk. I set PYTHONPATH = /usr/lib/python2.5/site- > > packages/ and that got importing pygtk to work, but the little app I > > downloaded wants to import gtk and I just cant figure out what I'm > > supposed to set PYTHONPATH to in order to get `import gtk` to work. > > Anyone know what I'm doing wrong or missing? > > > Thanks > > set PYTHONPATH to /usr/lib/python2.5/lib/site-packages:gtk-root-dir > > By the way, do you really think that it's necessary to put site-packages > on the path? You might find it's already on there as Python starts up. > > You can tell by printing sys.path from Python. Yes, it actually requires adding the site-packages to the PYTHONPATH to get pygtk to work. So far I've managed to get gtk to give a different error when I added the site-packages/gtk-2.0/ directory (the only gtk-like thing in the site-packages) to the PYTHONPATH `ImportError: gobject/_gobject.so: undefined symbol: PyUnicodeUCS4_FromObject`. I'll have to check the path when I get home to see what it actually has within it and work from there. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONPATH, opensuse10.2, gtk not working
On Oct 4, 8:31 pm, Silfheed <[EMAIL PROTECTED]> wrote: > On Oct 4, 7:39 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > > > > > Silfheed wrote: > > > Heyas > > > > So I'm trying to do two things, install a little gnome taskbar applet > > > (timer-applet) that was written in python and experiment with writing > > > python gtk apps. I've installed (through yast) gtk, gtk2, gtk2-devel, > > > python-gtk, python-gtk-devl, python-gtk-doc but I still cant get > > > python to import gtk. I set PYTHONPATH = /usr/lib/python2.5/site- > > > packages/ and that got importing pygtk to work, but the little app I > > > downloaded wants to import gtk and I just cant figure out what I'm > > > supposed to set PYTHONPATH to in order to get `import gtk` to work. > > > Anyone know what I'm doing wrong or missing? > > > > Thanks > > > set PYTHONPATH to /usr/lib/python2.5/lib/site-packages:gtk-root-dir > > > By the way, do you really think that it's necessary to put site-packages > > on the path? You might find it's already on there as Python starts up. > > > You can tell by printing sys.path from Python. > > Yes, it actually requires adding the site-packages to the PYTHONPATH > to get pygtk to work. So far I've managed to get gtk to give a > different error when I added the site-packages/gtk-2.0/ directory (the > only gtk-like thing in the site-packages) to the PYTHONPATH > `ImportError: gobject/_gobject.so: undefined symbol: > PyUnicodeUCS4_FromObject`. I'll have to check the path when I get > home to see what it actually has within it and work from there. > > Thanks. So after checking sys.path and snooping around a bit more, it turns out that I have two python site-package directories... one in /usr/lib and one in /usr/local/lib.. One is automatically stuck into sys.path (but allowed root access only) while the other is not. I dunno why this is, but is there a way to get python to see both of these automatically (without PYTHONPATH?) Not that this solves my problem since after changing the permissions to world readable on the sys.path listed /usr/lib/python2.5/site-packages, it still wont let me import gtk. This is all after installing everything with yast (which says that I have gtk).. -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONPATH, opensuse10.2, gtk not working
On Oct 5, 1:04 pm, Silfheed <[EMAIL PROTECTED]> wrote: > On Oct 4, 8:31 pm, Silfheed <[EMAIL PROTECTED]> wrote: > > > > > On Oct 4, 7:39 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > > > > Silfheed wrote: > > > > Heyas > > > > > So I'm trying to do two things, install a little gnome taskbar applet > > > > (timer-applet) that was written in python and experiment with writing > > > > python gtk apps. I've installed (through yast) gtk, gtk2, gtk2-devel, > > > > python-gtk, python-gtk-devl, python-gtk-doc but I still cant get > > > > python to import gtk. I set PYTHONPATH = /usr/lib/python2.5/site- > > > > packages/ and that got importing pygtk to work, but the little app I > > > > downloaded wants to import gtk and I just cant figure out what I'm > > > > supposed to set PYTHONPATH to in order to get `import gtk` to work. > > > > Anyone know what I'm doing wrong or missing? > > > > > Thanks > > > > set PYTHONPATH to /usr/lib/python2.5/lib/site-packages:gtk-root-dir > > > > By the way, do you really think that it's necessary to put site-packages > > > on the path? You might find it's already on there as Python starts up. > > > > You can tell by printing sys.path from Python. > > > Yes, it actually requires adding the site-packages to the PYTHONPATH > > to get pygtk to work. So far I've managed to get gtk to give a > > different error when I added the site-packages/gtk-2.0/ directory (the > > only gtk-like thing in the site-packages) to the PYTHONPATH > > `ImportError: gobject/_gobject.so: undefined symbol: > > PyUnicodeUCS4_FromObject`. I'll have to check the path when I get > > home to see what it actually has within it and work from there. > > > Thanks. > > So after checking sys.path and snooping around a bit more, it turns > out that I have two python site-package directories... one in /usr/lib > and one in /usr/local/lib.. One is automatically stuck into sys.path > (but allowed root access only) while the other is not. > > I dunno why this is, but is there a way to get python to see both of > these automatically (without PYTHONPATH?) Not that this solves my > problem since after changing the permissions to world readable on the > sys.path listed /usr/lib/python2.5/site-packages, it still wont let me > import gtk. This is all after installing everything with yast (which > says that I have gtk).. Well.. I suppose a logical explanation would be two different installs of python.. sorry for the noise. -- http://mail.python.org/mailman/listinfo/python-list
Mocking `from foo import *` functions
So I'm in the current testing situation: sender.py: - def sendEmails(): return "I send emails" alerter.py: - from sender import * def DoStuffAndSendEmails(): doStuff() sendEmails() I'm trying to write a test fn that will test DoStuffAndSendEmails() (as well as it's kin) without actually sending any emails out. I could go through alter alerter so that it does `import sender` and then find and replace fn() with sender.fn() so I can just create a mock fn fakeSendEmails() and and do something like sender.sendEmails = fakeSendEmails, but I'd rather not. Anyone know how to test alerter.py with out altering the file? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Mocking `from foo import *` functions
Brilliant, I love it. Thanks a million, guys. -- http://mail.python.org/mailman/listinfo/python-list
PIL's ImageFile Parser and PNG's
Heyas
So I've been messing around with the PIL and PNG's and came across a
little problem with PNG's.
So just to clarify, I'm running with the standard ubuntu 8.04 python-
imaging package that installs zlib and all the other goodies that go
along to make the PIL work with PNG's nicely.
So this works fine:
from PIL import Image
x = Image.open('some.png')
x.show()
But if I try to get a bit fancier and do
from PIL import ImageFile
p = ImageFile.Parser()
p.feed(open('some.png','rb').read())
image = p.close()
image.show()
I get a traceback:
Traceback (most recent call last):
File "basic_img.py", line 9, in
image.show()
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1450, in
show
_showxv(self, title, command)
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 2089, in
_showxv
file = image._dump(format=format)
File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 477, in
_dump
self.load()
File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 189,
in load
s = read(self.decodermaxblock)
File "/usr/lib/python2.5/site-packages/PIL/PngImagePlugin.py", line
365, in load_read
return self.fp.read(bytes)
File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 300,
in read
data = self.data[pos:pos+bytes]
TypeError: 'NoneType' object is unsubscriptable
But if all I do is change some.png to some.jpg (or gif), it works
fine. Am I doing something horribly wrong, or forget to compile
something that was supposed to be compiled for PNG support? Is it a
known bug with a fix somewhere that I just haven't found (I've
searched all over the PIL site)
Thanks
--
http://mail.python.org/mailman/listinfo/python-list
Re: PIL's ImageFile Parser and PNG's
On Nov 12, 5:28 pm, Silfheed <[EMAIL PROTECTED]> wrote:
> Heyas
>
> So I've been messing around with the PIL and PNG's and came across a
> little problem with PNG's.
>
> So just to clarify, I'm running with the standard ubuntu 8.04 python-
> imaging package that installs zlib and all the other goodies that go
> along to make the PIL work with PNG's nicely.
>
> So this works fine:
>
> from PIL import Image
> x = Image.open('some.png')
> x.show()
>
> But if I try to get a bit fancier and do
> from PIL import ImageFile
> p = ImageFile.Parser()
> p.feed(open('some.png','rb').read())
> image = p.close()
> image.show()
>
> I get a traceback:
> Traceback (most recent call last):
> File "basic_img.py", line 9, in
> image.show()
> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1450, in
> show
> _showxv(self, title, command)
> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 2089, in
> _showxv
> file = image._dump(format=format)
> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 477, in
> _dump
> self.load()
> File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 189,
> in load
> s = read(self.decodermaxblock)
> File "/usr/lib/python2.5/site-packages/PIL/PngImagePlugin.py", line
> 365, in load_read
> return self.fp.read(bytes)
> File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 300,
> in read
> data = self.data[pos:pos+bytes]
> TypeError: 'NoneType' object is unsubscriptable
>
> But if all I do is change some.png to some.jpg (or gif), it works
> fine. Am I doing something horribly wrong, or forget to compile
> something that was supposed to be compiled for PNG support? Is it a
> known bug with a fix somewhere that I just haven't found (I've
> searched all over the PIL site)
>
> Thanks
oh and forgive the errant 's (as in PNG's...)
--
http://mail.python.org/mailman/listinfo/python-list
Re: PIL's ImageFile Parser and PNG's
On Nov 12, 6:04 pm, Silfheed <[EMAIL PROTECTED]> wrote:
> On Nov 12, 5:28 pm,Silfheed<[EMAIL PROTECTED]> wrote:
>
>
>
> > Heyas
>
> > So I've been messing around with the PIL and PNG's and came across a
> > little problem with PNG's.
>
> > So just to clarify, I'm running with the standard ubuntu 8.04 python-
> > imaging package that installs zlib and all the other goodies that go
> > along to make the PIL work with PNG's nicely.
>
> > So this works fine:
>
> > from PIL import Image
> > x = Image.open('some.png')
> > x.show()
>
> > But if I try to get a bit fancier and do
> > from PIL import ImageFile
> > p = ImageFile.Parser()
> > p.feed(open('some.png','rb').read())
> > image = p.close()
> > image.show()
>
> > I get a traceback:
> > Traceback (most recent call last):
> > File "basic_img.py", line 9, in
> > image.show()
> > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1450, in
> > show
> > _showxv(self, title, command)
> > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 2089, in
> > _showxv
> > file = image._dump(format=format)
> > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 477, in
> > _dump
> > self.load()
> > File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 189,
> > in load
> > s = read(self.decodermaxblock)
> > File "/usr/lib/python2.5/site-packages/PIL/PngImagePlugin.py", line
> > 365, in load_read
> > return self.fp.read(bytes)
> > File "/usr/lib/python2.5/site-packages/PIL/ImageFile.py", line 300,
> > in read
> > data = self.data[pos:pos+bytes]
> > TypeError: 'NoneType' object is unsubscriptable
>
> > But if all I do is change some.png to some.jpg (or gif), it works
> > fine. Am I doing something horribly wrong, or forget to compile
> > something that was supposed to be compiled for PNG support? Is it a
> > known bug with a fix somewhere that I just haven't found (I've
> > searched all over the PIL site)
>
> > Thanks
>
> oh and forgive the errant 's (as in PNG's...)
Well, I dunno how much of a valid fix it is, but in ImageFile.py,
simply changing _ParserFile's close() to read:
def close(self):
self.offset = None
as opposed to
def close(self):
self.data = self.offset = None
allows png's to be loaded correctly for me.
--
http://mail.python.org/mailman/listinfo/python-list
CDATA and lxml
Heyas So first off I know that CDATA is generally hated and just shouldn't be done, but I'm simply required to parse it and spit it back out. Parsing is pretty easy with lxml, but it's the spitting back out that's giving me issues. The fact that lxml strips all the CDATA stuff off isnt really a big issue either, so long as I can create CDATA blocks later with <>&'s showing up instead of <>& . I've scoured through the lxml docs, but probably not hard enough, so anyone know the page I'm looking for or have a quick how to? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: CDATA and lxml
On Apr 11, 10:33 am, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> Hi again,
>
> Stefan Behnel wrote:
> > Silfheed wrote:
> >> So first off I know that CDATA is generally hated and just shouldn't
> >> be done, but I'm simply required to parse it and spit it back out.
> >> Parsing is pretty easy with lxml, but it's the spitting back out
> >> that's giving me issues. The fact that lxml strips all the CDATA
> >> stuff off isnt really a big issue either, so long as I can create
> >> CDATA blocks later with <>&'s showing up instead of <>& .
> >> I've scoured through the lxml docs, but probably not hard enough, so
> >> anyone know the page I'm looking for or have a quick how to?
>
> > There's nothing in the docs because lxml doesn't allow you to create CDATA
> > sections. You're not the first one asking that, but so far, no one really
> > had
> > a take on this.
>
> So I gave it a try, then. In lxml 2.1, you will be able to do this:
>
> >>> root = Element("root")
> >>> root.text = CDATA('test')
> >>> tostring(root))
> ''
>
> This does not work for .tail content, only for .text content (no technical
> reason, I just don't see why that should be enabled).
>
> There's also a parser option "strip_cdata" now that allows you to leave CDATA
> sections in the tree. However, they will *not* behave any different than
> normal text, so you can't even see at the API level that you are dealing with
> CDATA. If you want to be really, really sure, you can always do this:
>
> >>> root.text = CDATA(root.text)
>
> Hope that helps,
>
> Stefan
That is immensely cool. Do you plan to stick it into svn soon?
Thanks!
--
http://mail.python.org/mailman/listinfo/python-list
Re: CDATA and lxml
On Apr 11, 3:49 pm, Silfheed <[EMAIL PROTECTED]> wrote:
> On Apr 11, 10:33 am, Stefan Behnel <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi again,
>
> > Stefan Behnel wrote:
> > > Silfheed wrote:
> > >> So first off I know that CDATA is generally hated and just shouldn't
> > >> be done, but I'm simply required to parse it and spit it back out.
> > >> Parsing is pretty easy with lxml, but it's the spitting back out
> > >> that's giving me issues. The fact that lxml strips all the CDATA
> > >> stuff off isnt really a big issue either, so long as I can create
> > >> CDATA blocks later with <>&'s showing up instead of <>& .
> > >> I've scoured through the lxml docs, but probably not hard enough, so
> > >> anyone know the page I'm looking for or have a quick how to?
>
> > > There's nothing in the docs because lxml doesn't allow you to create CDATA
> > > sections. You're not the first one asking that, but so far, no one really
> > > had
> > > a take on this.
>
> > So I gave it a try, then. In lxml 2.1, you will be able to do this:
>
> > >>> root = Element("root")
> > >>> root.text = CDATA('test')
> > >>> tostring(root))
> > ''
>
> > This does not work for .tail content, only for .text content (no technical
> > reason, I just don't see why that should be enabled).
>
> > There's also a parser option "strip_cdata" now that allows you to leave
> > CDATA
> > sections in the tree. However, they will *not* behave any different than
> > normal text, so you can't even see at the API level that you are dealing
> > with
> > CDATA. If you want to be really, really sure, you can always do this:
>
> > >>> root.text = CDATA(root.text)
>
> > Hope that helps,
>
> > Stefan
>
> That is immensely cool. Do you plan to stick it into svn soon?
> Thanks!
Ah, looks like it's there already. Very cool, very cool. Thanks
again.
--
http://mail.python.org/mailman/listinfo/python-list
