473,320 Members | 1,694 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,320 software developers and data experts.

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(object):

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

@my_funky_decorator
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_decorator
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_decorator(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 <ol************@gmail.com--- http://roughbook.de/
Sep 24 '06 #1
1 911
"Oliver Andrich" <ol************@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_decorator
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_decorator(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_decorator(f):
@functools.wraps(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.handleException()
return wrapper

class MagickWand(object):

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

@my_funky_decorator
def read_image(self, filename):
return <ctypes call of the MagickWand C function to read an image>
Sep 24 '06 #2

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

Similar topics

17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
4
by: Doug Holton | last post by:
First let me say please see the wiki page about python decorators if you haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators I propose (and others have) that built-in...
12
by: Colin J. Williams | last post by:
Christopher T. King suggested that "we're trying to kill too many birds with one stone". ...
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
11
by: Arien Malec | last post by:
I've been following the decorator debate with some interest, and it's taken me a reasonably long time to understand what is meant by a decorator. One of the issues is that the Decorator pattern is...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
9
by: Bengt Richter | last post by:
;-) We have @deco def foo(): pass as sugar (unless there's an uncaught exception in the decorator) for def foo(): pass foo = deco(foo) The binding of a class name is similar, and class...
13
by: king kikapu | last post by:
Hi, i am new to python and i have a question about how decorators are working. I have understand HOW they do their magic but i am trying to figure out WHEN they do it... I have the...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.