Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47933 > unrolled thread
| Started by | Jayakrishnan Damodaran <jakyjk05@gmail.com> |
|---|---|
| First post | 2013-06-13 03:40 -0700 |
| Last post | 2013-06-13 13:26 +0200 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
How to pass instance into decorator function Jayakrishnan Damodaran <jakyjk05@gmail.com> - 2013-06-13 03:40 -0700
Re: How to pass instance into decorator function Peter Otten <__peter__@web.de> - 2013-06-13 13:26 +0200
| From | Jayakrishnan Damodaran <jakyjk05@gmail.com> |
|---|---|
| Date | 2013-06-13 03:40 -0700 |
| Subject | How to pass instance into decorator function |
| Message-ID | <e84043fc-9c4e-46a9-b7a1-8c136a4fbdf6@googlegroups.com> |
I have a class which calculates some salary allowances. Instead of a blind calculation I need to check some conditions before I can return the calculated amount. Like if all the allowances calculated till now plus the one in progress must not exceed the total salary(this may occur since these allowances are a percentage of basic pay). Below is a sample code FYI
class CompanySalaryBreakUpRule(object):
'''Class to calculate the various salary components.
This class is inherited by individual company classes whom implement salary calculations.
Various component calculations are implemented as @property and hence be accessed as class properties.
'''
grossPay = 0.0
remainingAmount = 0.0
def checkValid(self, func):
from functools import wraps
@wraps(func)
def wrapper(self, *args, **kwargs):
allowanceToCheck = func(self, *args, **kwargs)
if allowanceToCheck > self.remainingAmount:
allowanceToCheck = self.remainingAmount
else:
self.remainingAmount = self.remainingAmount - allowanceToCheck
return allowanceToCheck
return wrapper
@property
@checkValid
def dearnessAllowance(self):
return self.basic * 0.2 # we have a function that calculates basic pay
But executing this raises an exception
@checkValid
TypeError: checkValid() takes exactly 2 arguments (0 given)
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-06-13 13:26 +0200 |
| Message-ID | <mailman.3179.1371122752.3114.python-list@python.org> |
| In reply to | #47933 |
Jayakrishnan Damodaran wrote:
> I have a class which calculates some salary allowances. Instead of a blind
> calculation I need to check some conditions before I can return the
> calculated amount. Like if all the allowances calculated till now plus the
> one in progress must not exceed the total salary(this may occur since
> these allowances are a percentage of basic pay). Below is a sample code
> FYI
>
> class CompanySalaryBreakUpRule(object):
> '''Class to calculate the various salary components.
> This class is inherited by individual company classes whom implement
> salary calculations. Various component calculations are implemented as
> @property and hence be accessed as class properties.
>
> '''
> grossPay = 0.0
> remainingAmount = 0.0
>
> def checkValid(self, func):
> from functools import wraps
>
> @wraps(func)
> def wrapper(self, *args, **kwargs):
> allowanceToCheck = func(self, *args, **kwargs)
> if allowanceToCheck > self.remainingAmount:
> allowanceToCheck = self.remainingAmount
> else:
> self.remainingAmount = self.remainingAmount -
> allowanceToCheck
> return allowanceToCheck
> return wrapper
>
> @property
> @checkValid
> def dearnessAllowance(self):
> return self.basic * 0.2 # we have a function that calculates basic
> pay
>
> But executing this raises an exception
> @checkValid
> TypeError: checkValid() takes exactly 2 arguments (0 given)
Remember that
@checkValid
def dearnessAllowance(self):
...
is a shortcut for
def dearnessAllowance(self):
...
dearnessAllowance = checkValid(dearnessAllowance)
The function call happens in the class body, so checkValid() is still normal
function that has knowlegde neither about the class nor (as the self arg
implies) about an instance of that class.
While it would be sufficient to remove its first argument
> class CompanySalaryBreakUpRule(object):
...
> def checkValid(func):
...
> @property
> @checkValid
> def dearnessAllowance(self):
...
I recommend that you also move it out of the class body:
def checkValid(func):
...
> class CompanySalaryBreakUpRule(object):
...
> @property
> @checkValid
> def dearnessAllowance(self):
...
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web