[reportlab-users] isinstance(val, file)

Carl Karsten carl at personnelware.com
Wed Nov 14 16:32:30 EST 2007


Andy Robinson wrote:

> On 14/11/2007, Carl Karsten <carl at personnelware.com> wrote:

>> This is not really a reportlab problem, but close enough.

>>

>> How can I test that the file to be passed to canvas.Canvas(buffer) is really a

>> file type object that can be written to? I am trying to work with a wrapper

>> that looks like this:

>

>

> The traditional python approach would be...

>

> if hasattr(val, 'file'):

> #treat it like a file

> else:

> #treat it like a string

>

> i.e. if it walks like a duck and quacks like a duck...


except this does not apply to the animal that gets passed to
p = canvas.Canvas(val)

>>> import StringIO
>>> val=StringIO.StringIO()
>>> hasattr(val,'file')
False
>>> import os
>>> os.path.split(val)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/posixpath.py", line 77, in split
i = p.rfind('/') + 1
AttributeError: StringIO instance has no attribute 'rfind'

Carl K


More information about the reportlab-users mailing list