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,C allBack,*args,* *kwargs):
self.cb = CallBack
def write(self,str) :
self.cb(str,*ar gs,**kwargs)
My problem is in verifying the class we're trying to redirect output
to.
This is what I have so far:
def _VerifyOutputSt ream(fh):
if 'write' not in dir(fh):
raise AttributeError, "The Output Stream should have a write
method."
if not callable(fh.wri te):
raise TypeError, "The Output Stream's write method is not
callable."
(((
On a side note, I have derived the above exception names to use via.
experimentation in an interactive shell:
[color=blue][color=green][color=darkred]
>>> class SomeClass:pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> w = SomeClass()
>>> w.write[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: w instance has no attribute 'write'[color=blue][color=green][color=darkred]
>>> w.write = "Hurr, strings are not callable!"
>>> w.write()[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
)))
In the above _VerifyOutputSt ream 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:
(((
My experimentation in IDLE yielded no results, really, either.
[color=blue][color=green][color=darkred]
>>> class C:[/color][/color][/color]
def write(self, str, noreq=None):
pass[color=blue][color=green][color=darkred]
>>> c=C()
>>> dir(c.write.fun c_code)[/color][/color][/color]
['__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'][color=blue][color=green][color=darkred]
>>> c.write.func_co de.co_argcount[/color][/color][/color]
3[color=blue][color=green][color=darkred]
>>> c.write.func_co de.co_varnames[/color][/color][/color]
('self', 'str', 'noreq')
)))
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,C allBack,*args,* *kwargs):
self.cb = CallBack
def write(self,str) :
self.cb(str,*ar gs,**kwargs)
My problem is in verifying the class we're trying to redirect output
to.
This is what I have so far:
def _VerifyOutputSt ream(fh):
if 'write' not in dir(fh):
raise AttributeError, "The Output Stream should have a write
method."
if not callable(fh.wri te):
raise TypeError, "The Output Stream's write method is not
callable."
(((
On a side note, I have derived the above exception names to use via.
experimentation in an interactive shell:
[color=blue][color=green][color=darkred]
>>> class SomeClass:pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> w = SomeClass()
>>> w.write[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: w instance has no attribute 'write'[color=blue][color=green][color=darkred]
>>> w.write = "Hurr, strings are not callable!"
>>> w.write()[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
)))
In the above _VerifyOutputSt ream 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:
(((
My experimentation in IDLE yielded no results, really, either.
[color=blue][color=green][color=darkred]
>>> class C:[/color][/color][/color]
def write(self, str, noreq=None):
pass[color=blue][color=green][color=darkred]
>>> c=C()
>>> dir(c.write.fun c_code)[/color][/color][/color]
['__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'][color=blue][color=green][color=darkred]
>>> c.write.func_co de.co_argcount[/color][/color][/color]
3[color=blue][color=green][color=darkred]
>>> c.write.func_co de.co_varnames[/color][/color][/color]
('self', 'str', 'noreq')
)))
Comment