How to redirect operation methods to some sepcific method easily?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Victor Lin

    How to redirect operation methods to some sepcific method easily?

    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(s elf)
    self.dataSource .stack.append(o ther)
    self.dataSource .stack.append(' +')
    return OperationResult (self.dataSourc e)

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

    def __getitem__(sel f, 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.leftOperat ion('add', other)

    def __mul__(self, other):
    self.leftOperat ion('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.
  • Steven D'Aprano

    #2
    Re: How to redirect operation methods to some sepcific methodeasily?

    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.leftOperat ion('add', other)
    >
    def __mul__(self, other):
    self.leftOperat ion('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.LeftOperat ion(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

    Comment

    Working...