Function Verification

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ws

    #1

    Function Verification

    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:
    This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available. Unless explicitly noted oth...





    (((
    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')


    )))

  • Ben Cartwright

    #2
    Re: Function Verification

    Ws wrote:[color=blue]
    > 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").[/color]
    <snip>[color=blue]
    > 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."[/color]
    <snip>[color=blue]
    > 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?[/color]

    Why not just call the function with an empty string?

    def _VerifyOutputSt ream(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

    Comment

    • Ws

      #3
      Re: Function Verification

      Ah, damn. That would've been soo much simpler. =S

      Thanks for the advice man.

      -Wes

      Ben Cartwright wrote:[color=blue]
      > Ws wrote:[color=green]
      > > 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").[/color]
      > <snip>[color=green]
      > > 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."[/color]
      > <snip>[color=green]
      > > 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?[/color]
      >
      > Why not just call the function with an empty string?
      >
      > def _VerifyOutputSt ream(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[/color]

      Comment

      Working...