By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,660 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,660 IT Pros & Developers. It's quick & easy.

How to redirect operation methods to some sepcific method easily?

P: n/a
Hi,
I'd like to write some class that can help me build reusable formula
easily, some simple code like this.

# -*- coding: utf8 -*-

class OperationResult:
def __init__(self, left, right):
self.dataSource = dataSource

def __add__(self, other):
self.dataSource.stack.append('+')
return self

def __div__(self, other):
self.dataSource.stack.append('/')
return self

class Operand:
def __init__(self, dataSource, name):
self.dataSource = dataSource
self.name = name

def __add__(self, other):
self.dataSource.stack.append(self)
self.dataSource.stack.append(other)
self.dataSource.stack.append('+')
return OperationResult(self.dataSource)

class DataSource:
def __init__(self):
self.stack = []

def __getitem__(self, key):
return Operand(self, key)

def ROE(dataSource):
a = dataSource[u'股東權益總額']
b = dataSource[u'每股淨額']
c = dataSource[u'當季*均市價']
return (a + b) / c

Now I can use ROE formula easily, it will return a object that contain
formula stack like this
[a, b, +, c, /]

And then I can get real data of those items from database or other
place. The formula depends on nothing. It don't care where to get data
and how to calculate it. I can reuse it easily.

Now, here comes the problem : I have to override all the operation
methods, such as __add__ and __mul__.
I know the most stupid way is just to write all of them like this.

def __add__(self, other):
self.leftOperation('add', other)

def __mul__(self, other):
self.leftOperation('mul', other)

But I don't want to do that in this stupid way. I want to use little
code to redirect these operation methods to some specific method with
it's name.

What can I do?

Thanks.
Aug 3 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Sun, 03 Aug 2008 02:44:34 -0700, Victor Lin wrote:
Now, here comes the problem : I have to override all the operation
methods, such as __add__ and __mul__. I know the most stupid way is just
to write all of them like this.

def __add__(self, other):
self.leftOperation('add', other)

def __mul__(self, other):
self.leftOperation('mul', other)

But I don't want to do that in this stupid way. I want to use little
code to redirect these operation methods to some specific method with
it's name.

What can I do?

I usually dislike code that uses exec, but this is one place where I'm
tempted to make an exception (mainly because I can't think of an
alternative). Untested and at your own risk:

def factory(name):
"""Return a method name and a function."""
def func(self, other):
return self.LeftOperation(name, other)
return "__%s__" % name, func
class Parrot(object):
for name in ["mul", "add", "sub", "pow"]: # and many more...
name, f = factory(name)
exec "%s = f" % name
del name, f
Urrggh. I feel dirty. *wink*

--
Steve
Aug 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.