Running xinput to change touchpad settings I am looking for some feedback on the code below, mainly for efficiency or mechanism correctness (like eval vs. subprocess). I am also curious if .find() is the best mechanism to use here. I am not familiar with regex, so my crutch (or benefit, depending) is using .find() over learning a sublanguage like regex.

import subprocess

get_device_id = subprocess.Popen(["xinput", "list", "SynPS/2 Synaptics TouchPad"], stdout=subprocess.PIPE)
gdi_str = str(get_device_id.stdout.read())
gdi_id_find = (gdi_str[gdi_str.find('id='):])
gdi_len = 3
gdi = (gdi_id_find[gdi_len:5])
if gdi.isdigit():
    device_id = gdi
else:
    pass


get_prop_id = subprocess.Popen(["xinput", "list-props", "15"], stdout=subprocess.PIPE)
r = str(get_prop_id.stdout.read())

if "2 Synaptics TouchPad" in r:
    b = (r[r.find('libinput Tapping Enabled ('):])
    bLen = len('libinput Tapping Enabled (')
    b = (b[bLen:b.find(')')])
    if b.isdigit():
        prop_id = b
    else:
        pass

subprocess.run(["xinput", "set-prop", device_id, prop_id, "1"])


This code parses the output of:



xinput list SynPS/2 Synaptics TouchPad


to get a numerical device ID, in this case 15:

SynPS/2 Synaptics TouchPad                      id=15   [slave  pointer  (2)]


The code then runs:

xinput list-props 15   # 15 is our parsed id


and parses this output:

Device 'SynPS/2 Synaptics TouchPad':
Device Enabled (147):   1
Coordinate Transformation Matrix (149): 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
libinput Tapping Enabled (300): 0
libinput Tapping Enabled Default (301): 0
libinput Tapping Drag Enabled (302):    1
libinput Tapping Drag Enabled Default (303):    1
libinput Tapping Drag Lock Enabled (304):       0
libinput Tapping Drag Lock Enabled Default (305):       0
libinput Tapping Button Mapping Enabled (306):  1, 0
libinput Tapping Button Mapping Default (307):  1, 0
libinput Natural Scrolling Enabled (282):       0
libinput Natural Scrolling Enabled Default (283):       0
libinput Disable While Typing Enabled (308):    1
libinput Disable While Typing Enabled Default (309):    1
libinput Scroll Methods Available (286):        1, 1, 0
libinput Scroll Method Enabled (287):   1, 0, 0
libinput Scroll Method Enabled Default (288):   1, 0, 0
libinput Click Methods Available (310): 1, 1
libinput Click Method Enabled (311):    1, 0
libinput Click Method Enabled Default (312):    1, 0
libinput Middle Emulation Enabled (291):        0
libinput Middle Emulation Enabled Default (292):        0
libinput Accel Speed (293):     0.000000
libinput Accel Speed Default (294):     0.000000
libinput Left Handed Enabled (298):     0
libinput Left Handed Enabled Default (299):     0
libinput Send Events Modes Available (267):     1, 1
libinput Send Events Mode Enabled (268):        0, 0
libinput Send Events Mode Enabled Default (269):        0, 0
Device Node (270):      "/dev/input/event10"
Device Product ID (271):        2, 7
libinput Drag Lock Buttons (284):       &lt;no items&gt;
libinput Horizontal Scroll Enabled (285):       1


for the purposes of getting the prop-id for "libinput Tapping Enabled", in this case 300:



libinput Tapping Enabled (300): 0


Once the value is established, the code does a basic check for .isdigit() and if True, presents the values to:

subprocess.run(["xinput", "set-prop", device_id, prop_id, "1"])


Which ultimately sets the value to 1:

libinput Tapping Enabled (300): 1
There's no need for a redundant pass after your if; these can be deleted:

else:
    pass




xinput --list and xinput --list-props aren't long-running commands.  It's simpler to subprocess.run() them, with standard output redirected to a variable:

get_device_id = subprocess.run(["xinput", "list",
                                "SynPS/2 Synaptics TouchPad"],
                               capture_output=True)
if get_device_id.returncode != 0
    sys.exit(get_device_id.returncode)
gdi_str = get_device_id.stdout




I don't believe that first command is actually required - xinput --list-props is quite happy to accept a device name instead of a property:

xinput --list-props 'SynPS/2 Synaptics TouchPad'