dynamically inserting function into an object

Discussion in 'Python' started by michael, Jan 13, 2005.

  1. michael

    michael Guest

    Hi,

    below is a snipplet that could be seen as a part of a spreadsheet with
    getter and setter properties and a way how to dynamically insert
    function to be used when setting the value of a "cell" instance


    import new
    import inspect

    class Cell (object):

    def __init__ (self, initialvalue = 0):
    self._func = None
    self.__value = initialvalue

    def setvalue (self, newvalue):
    if self._func:
    self.__value = self._recalculate (newvalue)
    else:
    self.__value = newvalue

    def getvalue (self):
    return self.__value

    def _recalculate (self, value):

    ret_value = self._func (value)

    return ret_value

    def delvalue (self):
    del self.__value


    value = property(getvalue, setvalue, delvalue, "I'm the 'value'
    property.")

    def curry(self, func, *args):
    self._func = new.function(func.func_code, func.func_globals,
    argdefs=args)

    func = property(curry, "I'm the 'func' property.")

    def func (value, firstcell, secondcell):
    return value + firstcell.value + secondcell.value

    cell0 = Cell (10)
    cell1 = Cell (20)

    curriedcell = Cell (100)

    print "uncurried initial %d " % (curriedcell.value)

    curriedcell.value = 60

    print "uncurried set %d " % (curriedcell.value)

    curriedcell.curry (func, cell0, cell1)
    curriedcell.value = 62

    print "curried set %d " % (curriedcell.value)



    Is there a better way to do this or am I totally on the wrong way ?

    Regards

    Michael
    michael, Jan 13, 2005
    #1
    1. Advertising

  2. michael

    John Roth Guest

    If what you want is to insert a method into an
    instance, look at new.instancemethod.

    John Roth

    "michael" <> wrote in message
    news:...
    > Hi,
    >
    > below is a snipplet that could be seen as a part of a spreadsheet with
    > getter and setter properties and a way how to dynamically insert
    > function to be used when setting the value of a "cell" instance
    >
    >
    > import new
    > import inspect
    >
    > class Cell (object):
    >
    > def __init__ (self, initialvalue = 0):
    > self._func = None
    > self.__value = initialvalue
    >
    > def setvalue (self, newvalue):
    > if self._func:
    > self.__value = self._recalculate (newvalue)
    > else:
    > self.__value = newvalue
    >
    > def getvalue (self):
    > return self.__value
    >
    > def _recalculate (self, value):
    >
    > ret_value = self._func (value)
    >
    > return ret_value
    >
    > def delvalue (self):
    > del self.__value
    >
    >
    > value = property(getvalue, setvalue, delvalue, "I'm the 'value'
    > property.")
    >
    > def curry(self, func, *args):
    > self._func = new.function(func.func_code, func.func_globals,
    > argdefs=args)
    >
    > func = property(curry, "I'm the 'func' property.")
    >
    > def func (value, firstcell, secondcell):
    > return value + firstcell.value + secondcell.value
    >
    > cell0 = Cell (10)
    > cell1 = Cell (20)
    >
    > curriedcell = Cell (100)
    >
    > print "uncurried initial %d " % (curriedcell.value)
    >
    > curriedcell.value = 60
    >
    > print "uncurried set %d " % (curriedcell.value)
    >
    > curriedcell.curry (func, cell0, cell1)
    > curriedcell.value = 62
    >
    > print "curried set %d " % (curriedcell.value)
    >
    >
    >
    > Is there a better way to do this or am I totally on the wrong way ?
    >
    > Regards
    >
    > Michael
    John Roth, Jan 13, 2005
    #2
    1. Advertising

  3. michael

    hanz Guest

    # class Cell(object):
    # def __init__(self, initialvalue = 0):
    # self._func = lambda x: x
    # self.__value = initialvalue
    #
    # def setvalue (self, newvalue):
    # self.__value = self._func(newvalue)
    #
    # def getvalue (self):
    # return self.__value
    #
    # def delvalue (self):
    # del self.__value
    #
    # value = property(getvalue, setvalue, delvalue, "I'm the 'value'
    property.")
    #
    # def curry(self, func, *args):
    # self._func = lambda x: func(x, *args)
    hanz, Jan 13, 2005
    #3
  4. michael

    Guest

    *damn* it :) python rocks.
    thx

    michael .oO (resetting c/c++/java crap collected over the years)
    , Jan 14, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ceri
    Replies:
    2
    Views:
    416
    Ceri Locke
    Dec 17, 2003
  2. Jeffrey Todd
    Replies:
    1
    Views:
    4,090
    Peter Blum
    Jun 2, 2005
  3. Anonymous Sender
    Replies:
    7
    Views:
    411
    Andrew Thompson
    May 25, 2004
  4. thunk
    Replies:
    1
    Views:
    77
    Marnen Laibow-Koser
    Feb 3, 2010
  5. cyprian
    Replies:
    0
    Views:
    102
    cyprian
    Jul 18, 2007
Loading...

Share This Page