TheSilence TheSilence - 1 year ago 141
Python Question

Construct a list of integers from bigger to smaller in python

I'm attempting to develop a little mouse controller app. It should get (X, Y) coordinates and make the cursor go there.

The problem is that when it attempts going to an X coordinate smaller than the current one.

import win32con
from win32api import GetCursorPos, SetCursorPos, mouse_event, GetSystemMetrics
from time import sleep

def clickWithCursor(xDesired, yDesired):
xCurrent, yCurrent = GetCursorPos()

slope = float(yDesired - yCurrent) / float(xDesired - xCurrent)

def goAhead(x, y):
for x in range(min(xCurrent, xDesired), max(xDesired, xCurrent), 2):
y = int(slope * (x - xCurrent) + yCurrent)
SetCursorPos((int(x), y))


return goAhead(0, 0)

def main():
clickWithCursor(243, 184)


Above is just a very bad attempt to do so which does not give me the result I was looking for. I was looking all over how to do it and just could not find the right way.

In short, I wanna construct a list so it would go logically from bigger to smaller value, or smaller to bigger, according to paramerters order.

So, if I would give range(4, 1) which I want to be resulted in: [4, 3, 2] or range(1, 4), it would'nt mind and construct it the right way...

I refactored the code according to the answers and made it more readable for other users to review. Note the "sequence" method in class MouseController:

from win32api import GetCursorPos, SetCursorPos, mouse_event
from time import sleep

class CursorPositionPrinter(object):
"""docstring for CursorPositionPrinter"""
def print_cursor_pos(self):
print GetCursorPos()

def __init__(self):
super(CursorPositionPrinter, self).__init__()

class AutoClicker(object):
"""docstring for AutoClicker"""
def click(self, times):

xCurrent, yCurrent = GetCursorPos()
for i in xrange(times):
self.simple_click(xCurrent, yCurrent)

def simple_click(self, x, y):
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)

def __init__(self):
super(AutoClicker, self).__init__()

class MouseController(CursorPositionPrinter, AutoClicker):
"""docstring for MouseController
Controlls your mouse magically!!!"""

def sequence(self, a, b, n):
mn, mx = a, b
step = -n if mn > mx else n

for value in xrange(mn, mx, step):
yield value

def click_with_cursor(self, xDesired, yDesired):
self.go_to_coordinates(xDesired, yDesired)
self.simple_click(xDesired, yDesired)

def go_to_coordinates(self, xDesired, yDesired):

xCurrent, yCurrent = GetCursorPos()

slope = float(yDesired - yCurrent) / float(xDesired - xCurrent)

for x in self.sequence(xCurrent, xDesired, 2):
y = int(slope * (x - xCurrent) + yCurrent)
SetCursorPos((int(x), y))

SetCursorPos((xDesired, yDesired))

def __init__(self, latency=0.02):
super(MouseController, self).__init__()
self.latency = latency

Answer Source

range(a, b, -1 if a > b else 1)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download