473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function Verification

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 "<stdin>", 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 "<stdin>", 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')
)))

Jun 7 '06 #1
2 1702
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"). <snip> 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." <snip> 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

Jun 7 '06 #2
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").

<snip>
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."

<snip>
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


Jun 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BadOmen | last post by:
I have made an array in a function in a Class now I need to get to that Array from an other function in an other Class, How do I do that? The Array has no fixed size as it is dependent on how...
2
by: Konstantin Zakharenko | last post by:
Hello, Our QA team have running a lot of test scripts (for automated regression testing), they run them on the different databases (Oracle/MS SQL). Several of those tests are dependent on the...
7
by: SOR | last post by:
Although this currently defeats the spam bots in some respects - isnt it just a mater of time before the spammers figure out a way to verify a signup via email using rotating disposable email...
38
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
0
by: pwilliams | last post by:
NCOALink Change of Address Verification Each year over 40 million Americans change their mailing addresses. This change is equivalent to every person in California deciding to change addresses...
1
by: mchlle | last post by:
I have and email button on one of my forms but I want to use that code on a few more forms but don't want to copy the code on each form because any changes I make to the code will need to be...
13
by: Kal | last post by:
I have a small console app that started out in dotnet 1.1 in VS 2003. That version can be copied to a W2K3 server where it runs fine. I set up a new project in VS 2005 and copied the code files...
4
by: d1srupt3r | last post by:
I am developing my form into a captcha secured form and I used header() function if the user didn't put the verification code properly and still when the user go back to the form page the filled he...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.