Question about decorators (with context?)

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

    #1

    Question about decorators (with context?)

    Hi,

    sorry for the vague subject, but I can't come up with a better one so far.

    I am currently writing a wrapper around ImageMagicks MagickWand
    library using the ctypes module. During this development i have to
    handle exceptions in an slightly different way, then python normally
    handles them.

    I am wrapping the libraries functionality in a class that looks like
    that. (I leave out the ctypes calls and stuff as far as it is
    possible.)

    class MagickWand(obje ct):

    def __init__(self):
    self._wand = <create a MagickWand instance using ctypes>

    @my_funky_decor ator
    def read_image(self , filename):
    return <ctypes call of the MagickWand C function to read an image>

    You can see, I plan to have my own MagickWand object, which wraps the
    C struct and functions. The normal habit of MagickWand is to call a
    function, that returns either True or False. Based on this the user
    has to check for an exception or not.

    I will have to wrap a lot of methods to get all the functionality I
    need. And the checking for an exception looks always the same. So I
    want to write short methods, which are decorated by my_funky_decora tor
    which handles the error checking and exception handling. Sadly, this
    decorator needs access to self._wand from the current object.

    So far I can only think of a solution, where I have to do something like that.

    @my_funky_decor ator(wand=<some way to access the _wand>)
    def read_image(self , filename):
    pass

    So, I like to ask, if someone could enlighten me how to implement the
    easy decorator I mentioned in my first "example".

    Thanks and best regards,
    Oliver

    --
    Oliver Andrich <oliver.andrich @gmail.com--- http://roughbook.de/
  • Duncan Booth

    #2
    Re: Question about decorators (with context?)

    "Oliver Andrich" <oliver.andrich @gmail.comwrote :
    I will have to wrap a lot of methods to get all the functionality I
    need. And the checking for an exception looks always the same. So I
    want to write short methods, which are decorated by my_funky_decora tor
    which handles the error checking and exception handling. Sadly, this
    decorator needs access to self._wand from the current object.
    >
    So far I can only think of a solution, where I have to do something
    like that.
    >
    @my_funky_decor ator(wand=<some way to access the _wand>)
    def read_image(self , filename):
    pass
    >
    So, I like to ask, if someone could enlighten me how to implement the
    easy decorator I mentioned in my first "example".
    Just make sure your wrapper takes self as its first argument:

    def my_funky_decora tor(f):
    @functools.wrap s(f) # Omit this if Python <2.5
    def wrapper(self, *args, **kw):
    try:
    print "wand is", self._wand
    return f(self, *args, **kw)
    except whatever:
    self.handleExce ption()
    return wrapper

    class MagickWand(obje ct):

    def __init__(self):
    self._wand = <create a MagickWand instance using ctypes>

    @my_funky_decor ator
    def read_image(self , filename):
    return <ctypes call of the MagickWand C function to read an image>

    Comment

    Working...