Function Verification

2006-06-06 Thread 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."

(((
On a side note, I have derived the above exception names to use via.
experimentation in an interactive shell:

>>> class SomeClass:pass
...
>>> w = SomeClass()
>>> w.write
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: w instance has no attribute 'write'
>>> w.write = "Hurr, strings are not callable!"
>>> w.write()
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: 'str' object 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?

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.


>>> class C:
def write(self, str, noreq=None):
pass
>>> c=C()
>>> dir(c.write.func_code)
['__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']
>>> c.write.func_code.co_argcount
3
>>> c.write.func_code.co_varnames
('self', 'str', 'noreq')


)))

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Function Verification

2006-06-06 Thread Ws
Ah, damn. That would've been soo much simpler. =S

Thanks for the advice man.

-Wes

Ben Cartwright wrote:
> Ws wrote:
> > 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb problem with mod_python, please help

2004-11-30 Thread ws Wang
Damjan <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> > MySQLdb is working fine at command line, however when I tried to use
> > it with mod_python, it give me a "server not initialized" error.
> 
> Maybe its this problem?
> http://www.modpython.org/FAQ/faqw.py?req=show&file=faq02.013.htp

yep! that's the culprit. 

I havent' recompile php, but by removing loadmodule php, mod_python+mysql 
worked.

Thank you very much Damjan.
-- 
http://mail.python.org/mailman/listinfo/python-list