How do I get multiple arguments into a class object that I can use in dictionary functions

Asked by Melvin Raymond on 2019-04-09

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"

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
Invalid direction

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Melvin Raymond
Last query:
Last reply:
Melvin Raymond (fribian) said : #1

I'm running this in SikuliX IDE by the way.

RaiMan (raimund-hocke) said : #2

why so complicated?

this simple def will do:

def move(direction, counter = 1):
    if direction < 1 or direction > 4:
        return # do nothing
    key = Key.RIGHT
    if direction == 2:
        key = Key.LEFT
    elif direction == 3:
        key = Key.UP
    if direction == 4:
        key = Key.DOWN
    for n in range(counter):

To make it more readable place this outside the def
LEFT = 2
UP = 3
DOWN = 4

the use it:
move(RIGHT) # one cell
move(DOWN, 3) # 3 cells

Melvin Raymond (fribian) said : #3

I figured it wouldn't be that easy to use the Dictionary way, and what you have shown here is pretty much what I ended up doing. Using the Jython/Python traditional "if" approach was a lot easier. I had done this and it works. There are of course a lot of things left out such as where the mouse is located but I was able to manipulate the location of the selected cell. This pretty much answers my question though. Below is the code I ended up using in case anyone wants to try and utilize it. This one allows me to move multiple times. It doesn't check for invalid "direction" or "loopvalue" variables, but can easily be done.

def grid_movement(direction, loopvalue):
  print "Executing 'grid_movement' function"
    if loopvalue > 0:
      print "Moving to new cell"
      if direction == 1:
        loopvalue = loopvalue - 1
      elif direction == 2:
        loopvalue = loopvalue - 1
      elif direction == 3:
        loopvalue = loopvalue - 1
      elif direction == 4:
        loopvalue = loopvalue - 1
     print "Grid selection in that direction is completed"

moveright = 1
moveleft = 2
moveup = 3
movedown = 4

grid_movement(moveright, 3)
grid_movement(movedown, 25)
grid_movement(moveleft, 3)
grid_movement(moveup, 25)

Thanks Railman.

RaiMan (raimund-hocke) said : #4

why so complicated?

while(loopvalue > 0):
  if direction == 1:
  elif direction == 2:
  elif direction == 3:
  elif direction == 4:
  loopvalue -= 1

Melvin Raymond (fribian) said : #5

Yep. That's much better. I hadn't made attempts to clean it up yet. I do want to keep some of the print statements in there for Robot Framework to make things easier for me to troubleshoot when freaky things happen. I will use this code you have here though as it's much less cluttered and easy to follow.

Thanks RaiMan