How to redirect operation methods to some sepcific method easily?

V

Victor Lin

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'ªÑªFÅv¯qÁ`ÃB']
b = dataSource[u'¨CªÑ²bÃB']
c = dataSource[u'·í©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.
 
S

Steven D'Aprano

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*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top