Function Verification

W

Ws

Hi all

I'm trying to write up a module that *safely* sets sys.stderr and
sys.stdout, and am currently having troubles with the function
verification. I need to assure that the function can indeed be called
as the Python manual specifies that sys.stdout and sys.stderr should be
defined (standard file-like objects, only requiring a function named
"write").

For an example output wrapper class, it could look something so simple
as this:
class OutputWrapper:
def __init__(self,CallBack,*args,**kwargs):
self.cb = CallBack
def write(self,str):
self.cb(str,*args,**kwargs)

My problem is in verifying the class we're trying to redirect output
to.
This is what I have so far:
def _VerifyOutputStream(fh):
if 'write' not in dir(fh):
raise AttributeError, "The Output Stream should have a write
method."
if not callable(fh.write):
raise TypeError, "The Output Stream's write method is not
callable."

(((
Traceback (most recent call last):
Traceback (most recent call last):
)))

In the above _VerifyOutputStream function, how would I verify that the
fh.write method requires only one argument, as the built-in file
objects do?

Thanks in advance

-Wes

PS: As a point of reference, to make your lives easier, the links to
the Python manual pages:
http://docs.python.org/lib/module-sys.html
http://docs.python.org/lib/bltin-file-objects.html



(((
My experimentation in IDLE yielded no results, really, either.

def write(self, str, noreq=None):
pass['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'co_argcount', 'co_cellvars',
'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags',
'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals',
'co_stacksize', 'co_varnames']('self', 'str', 'noreq')


)))
 
B

Ben Cartwright

Ws said:
I'm trying to write up a module that *safely* sets sys.stderr and
sys.stdout, and am currently having troubles with the function
verification. I need to assure that the function can indeed be called
as the Python manual specifies that sys.stdout and sys.stderr should be
defined (standard file-like objects, only requiring a function named
"write").
My problem is in verifying the class we're trying to redirect output
to.
This is what I have so far:
def _VerifyOutputStream(fh):
if 'write' not in dir(fh):
raise AttributeError, "The Output Stream should have a write
method."
if not callable(fh.write):
raise TypeError, "The Output Stream's write method is not
callable."
In the above _VerifyOutputStream function, how would I verify that the
fh.write method requires only one argument, as the built-in file
objects do?

Why not just call the function with an empty string?

def _VerifyOutputStream(fh):
fh.write('')

Note that you don't need to manually check for AttributeError or
TypeError. Python will do that for you. It's generally better to act
first and ask forgiveness later.

--Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top