0

Similar to this question: Tuple declaration in Python

I have this function:

def get_mouse():
    # Get: x:4631 y:506 screen:0 window:63557060
    mouse = os.popen( "xdotool getmouselocation" ).read().splitlines()
    print mouse
    return mouse

When I run it it prints:

['x:2403 y:368 screen:0 window:60817757']

I can split the line and create 4 separate fields in a list but from Python code examples I've seen I feel there is a better way of doing it. I'm thinking something like x:= or window:=, etc.

I'm not sure how to properly define these "named tuple fields" nor how to reference them in subsequent commands?

I'd like to read more on the whole subject if there is a reference link handy.

4
  • Do you mean a dictionary? Such as dict([('x',1234),('y',368),('screen',0),('window',60817757)]) Commented Jan 14, 2020 at 2:28
  • how's the get_mouse function to do with your question? Please be focus on what you need Commented Jan 14, 2020 at 2:39
  • Just a note, it seems you may be using Python 2, which is no longer supported! You should make the switch to Python 3. Commented Jan 14, 2020 at 2:46
  • @LPython Ubuntu 16.04 doesn't hit EOL until 2021 and it supports Python 2.17 and Python 3.5. I'm trying to support Ubuntu 16.04 to Ubuntu 20.04 and Gnome 3.18 to 3.22+ in the same program. I use a lot of try: and except: statements. It will get worse when I try to support Windows 10 and WSL2 too. Commented Jan 14, 2020 at 3:22

2 Answers 2

1

It seems it would be a better option to use a dictionary here. Dictionaries allow you to set a key, and a value associated to that key. This way you can call a key such as dictionary['x'] and get the corresponding value from the dictionary (if it exists!)

data = ['x:2403 y:368 screen:0 window:60817757'] #Your return data seems to be stored as a list
result = dict(d.split(':') for d in data[0].split()) 

result['x']
#'2403'
result['window']
#'60817757'

You can read more on a few things here such as;

Comprehensions

Dictionaries

Happy learning!

Sign up to request clarification or add additional context in comments.

3 Comments

Looks good, except substitute mouse for result. Sorry it seemed obvious to me the function was getting the screen x,y coordinates where the mouse pointer is. It seems a unique situation and not so obvious to others.
You can rename the variables however you like, just take note on the comprehension performed and how calls to the dictionary are made. These are the two key elements to how this functions.
Although default, I find for d in data[0].split(' ')) more readable. Thank you for your answer and the bonus reading links.
1

try

dict(mouse.split(':') for el in mouse

This should give you a dict (rather than tuples, though dicts are mutable and also required hashability of keys)

{x: 2403, y:368, ...}

Also the splitlines is probably not needed, as you are only reading one line. You could do something like:

mouse = [os.popen( "xdotool getmouselocation" ).read()]

Though I don't know what xdotool getmouselocation does or if it could ever return multiple lines.

2 Comments

Shouldn't it be something like dict(mouse.split(':') for el in mouse[0].split()) (assuming mouse always has one element; I agree the splitlines is redundant). Also, the values of the dictionary would be strings instead of integers.
.splitlines() was a carry over from copied function wmctrl which populated list of all windows. I thought of taking it out but reconsidered based on the fact xdotool might return more than one line if there was errors. Granted there should only be one mouse location but who knows. Can you elaborate on "mutability" and "hashability of keys" a bit? Putting into a dict seems very simple but how does one read a dictionary?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.