generate methods at runtime, but the wrong one gets called

S

Steven Samuel Cole

Hello,

I am writing an application that controls robots. Different robots can
do different kinds of movements, such as e.g. open gripper, rotate
gripper, etc. My RobotControl class should support all kinds of
robots. I therefore delegate the actual control work to extra
control-specific classes, one class per movement type, e.g.
OpenGripperControl, RotateGripperControl. These movement control
classes are created by a simple class factory.

This is my class half-way through:

class RobotControl:

def __init__(self, movementTypes):
self.__controls = {}
motionControlFactory = MotionControlFactory()
for movementType in movementTypes:
self.__controls[movementType] =
motionControlFactory.create(movementType)

def __setMovementTypeValue(self, movementType, value):
control = self.__controls[movementType]
control.SetValue(value)

# I want to provide methods like these to the client app,
# but without typing them explicitely:
def SetOpenGripper(self, value):
self.__setMovementTypeValue('OpenGripper', value)

def SetRotateGripper(self, value):
self.__setMovementTypeValue('RotateGripper', value)


My application:

movementTypes = ['OpenGripper', 'RotateGripper']
robotCtrl = RobotControl(movementTypes)
robotCtrl.SetOpenGripper(1.0)
robotCtrl.SetRotateGripper(2.0)


While control object creation is abstracted out into the class
factory, SetOpenGripper(...) and SetRotateGripper(...) are still in
there and I obviously want to get rid of them, at least in their
explicitely implemented form. Instead, I want to generate them at
runtime, depending on what movement types are required.

I tried this in class RobotControl in __init__() in the 'for
movementType ...' loop:

funcName = 'Set' + movementType
function = lambda self, value:
self.__setMovementTypeValue(movementType, value)
method = new.instancemethod(function, self, self.__class__)
setattr(self, funcName, method)

and the code somewhat seems to work, but instead of SetOpenGripper,
SetRotateGripper is called.

My questions:

1.) Does this look like a somewhat reasonable approach to someone who
knows more about Python than me ?
2.) What could I be doing wrong ?

I have a suspicion that my error is not even related to all this fancy
runtime code generation stuff, but something really dumb and I've just
been coding for too long to see it.

http://mail.python.org/pipermail/python-list/2007-June/446601.html
shows a somewhat comparable constellation and it was a good guideline.
But there, the function is created by the class factory, as well and I
unfortunately can't do that.

Thank you very much,

Steve
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top