← Back to team overview

sikuli-driver team mailing list archive

[Question #680146]: How do I get multiple arguments into a class object that I can use in dictionary functions

 

New question #680146 on Sikuli:
https://answers.launchpad.net/sikuli/+question/680146

I don't know if I can even do this or not. I'm experimenting with a class object that "I think" is using a dictionary to simulate a switch statement in Jython/Python. I'm aware that Python doesn't have a switch statement. But, my goal is to get some code to manipulate movement around a table in my application. I can do this with a bunch of If and else statements enclosed in a while(1) to carry out moving right, left, up, and down multiple times. The thing is, I want to avoid having to use the while(1) and if else statements if I can make shorter code def or class that is more efficient.  So, I found some class object out on the web and got it working with my "Right", "Left", "Up", and "Down" which I'll eventually try to replace with type(Key.RIGHT), type(Key.LEFT), type(Key.UP) and type(Key.DOWN).

Now to clarify the original question: Can I get a counter argument into this class and down to the directional "def" functions so I can make them happen more than just once? I know I can just keep calling into this class Grid(object) but that requires an external multiple call into it. Or, will I have to encase the dictionary inside a while(1) with a break and a counter (and if so, how)? Maybe it's not worth trying it this way. I don't know? 


class Grid(object):
    print "Executing class 'Grid(object):' function"
    def direction(self, direction):
        """Dispatch method"""
        method_name = 'direction_' + str(direction)
        # Get the method from 'self'. Default to a lambda.
        method = getattr(self, method_name, lambda: "Invalid direction")
        # Call the method as we return it
        return method()
 
    def direction_1(self):
        return "Right"
 
    def direction_2(self):
        return "Left"
 
    def direction_3(self):
        return "Up"

    def direction_4(self):
        return "Down"

a=Grid()
moveright = 1
moveleft = 2
moveup = 3
movedown = 4
movestrange = 5

print a.direction(moveright)
print a.direction(moveleft)
print a.direction(moveup)
print a.direction(movedown)
print a.direction(movestrange)


-------------------------------------------- output from execution below --------------------

Executing class 'Grid(object):' function
Right
Left
Up
Down
Invalid direction


-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.