473,320 Members | 1,848 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.

How to do a Decorator Here?

Need some decorator help.

I have a class. And I want to add behavior to one of this class's
methods to be run before the class runs the actual method. Is this
what decorators are for?

So the class I want to work with is string.Template

Let's say I have this:
from string import Template
a=Template("$var1 is a test")

def preprocess(var1):
#Real code here will be more complicated, just an example
var1=var1.upper()

a.substitute(var1="greg")

So how can I have preprocess run before substitute is run? I want the
user to be able to call a.substitute and have preprocess run
automatically.

Or is this not what decorators do? I'm trying to avoid subclassing if I can.

Thanks in advance for the help.

-Greg
Feb 20 '07 #1
3 1147
On Feb 20, 8:20 pm, "Gregory Piñero" <gregpin...@gmail.comwrote:
Need some decorator help.

I have a class. And I want to add behavior to one of this class's
methods to be run before the class runs the actual method. Is this
what decorators are for?

So the class I want to work with is string.Template

Let's say I have this:
from string import Template
a=Template("$var1 is a test")

def preprocess(var1):
#Real code here will be more complicated, just an example
var1=var1.upper()

a.substitute(var1="greg")

So how can I have preprocess run before substitute is run? I want the
user to be able to call a.substitute and have preprocess run
automatically.

Or is this not what decorators do? I'm trying to avoid subclassing if I can.

Thanks in advance for the help.

-Greg
That sounds like aspect oriented programming. Spring Python (http://
springpython.python-hosting.com) offers a way to wrap objects with
method interceptors. Method interceptors give you full control before
and after method calls.

Sometimes decorators can be used to do that as well, but they have
constraints in where they can be used. I found them too inflexible for
my needs, so I built an AOP solution.

Greg

Feb 20 '07 #2
On Feb 20, 12:20 pm, "Gregory Piñero" <gregpin...@gmail.comwrote:
Need some decorator help.

I have a class. And I want to add behavior to one of this class's
methods to be run before the class runs the actual method. Is this
what decorators are for?

So the class I want to work with is string.Template

Let's say I have this:
from string import Template
a=Template("$var1 is a test")

def preprocess(var1):
#Real code here will be more complicated, just an example
var1=var1.upper()

a.substitute(var1="greg")

So how can I have preprocess run before substitute is run? I want the
user to be able to call a.substitute and have preprocess run
automatically.

Or is this not what decorators do? I'm trying to avoid subclassing if I can.

Thanks in advance for the help.

-Greg
You could just overload Template and put your own version of
substitute in there. I'm not sure a decorator is appropriate in this
case. Here is an overloading example:

Expand|Select|Wrap|Line Numbers
  1. from string import Template
  2.  
  3. # this assumes you are only interested in keyword arguments
  4. # the form substitute(map) will not work
  5. class MyTemplate(Template):
  6. def substitute(self,**kwargs):
  7. for k,v in kwargs.iteritems():
  8. kwargs[k] = v.upper()
  9. return super(MyTemplate,self).substitute(self,**kwargs)
  10.  
  11. if __name__ == "__main__":
  12. a = MyTemplate("$var1 is a test")
  13. print a.substitute(var1 = "greg")
  14.  
Feb 21 '07 #3
On Feb 20, 9:20 pm, "Gregory Piñero" <gregpin...@gmail.comwrote:
Or is this not what decorators do? I'm trying to avoid subclassing if I can.
Your problem, overriding a method, is what inheritance was made for.
If you want
to know more about decorators, see Dr Mertz's last article

http://www-128.ibm.com/developerwork...l-cpdecor.html
Michele Simionato

Feb 21 '07 #4

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

Similar topics

7
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: ...
17
by: Jim Jewett | last post by:
Guido has said that he is open to considering *one* alternative decorator syntax. At the moment, (Phillip Eby's suggestion) J4 <URL: http://www.python.org/moin/PythonDecorators > (section 5.21...
30
by: Ron_Adam | last post by:
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a...
5
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
4
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.