Skip to content

Commit e9e992e

Browse files
committed
Optimize savePinModeAlt()
See micropython/micropython#17515 (comment)
1 parent 199796d commit e9e992e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

cv2_drivers/displays/cv2_display.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,33 @@ def savePinModeAlt(self, pin):
103103
# convert the pin to a string and parse it. Example formats:
104104
# "Pin(GPIO16, mode=OUT)"
105105
# "Pin(GPIO16, mode=ALT, alt=SPI)"
106-
pinStr = str(pin)
106+
pin_str = str(pin)
107107

108108
# Extract the "mode" parameter from the pin string
109-
if "mode=" in pinStr:
109+
try:
110110
# Split between "mode=" and the next comma or closing parenthesis
111-
modeStr = pinStr.split("mode=")[1].split(",")[0].split(")")[0]
111+
mode_str = pin_str[pin_str.index("mode=") + 5:].partition(",")[0].partition(")")[0]
112112

113113
# Look up the mode in Pin class dictionary
114-
mode = Pin.__dict__[modeStr]
115-
else:
114+
mode = Pin.__dict__[mode_str]
115+
except (ValueError, KeyError):
116116
# No mode specified, just set to -1 (default)
117117
mode = -1
118118

119119
# Extrct the "alt" parameter from the pin string
120-
if "alt=" in pinStr:
120+
try:
121121
# Split between "alt=" and the next comma or closing parenthesis
122-
altStr = pinStr.split("alt=")[1].split(",")[0].split(")")[0]
122+
alt_str = pin_str[pin_str.index("alt=") + 4:].partition(",")[0].partition(")")[0]
123123

124124
# Sometimes the value comes back as a number instead of a valid
125125
# "ALT_xyz" string, so we need to check it
126-
if "ALT_" + altStr in Pin.__dict__:
126+
if "ALT_" + alt_str in Pin.__dict__:
127127
# Look up the alt in Pin class dictionary (with "ALT_" prefix)
128-
alt = Pin.__dict__["ALT_" + altStr]
128+
alt = Pin.__dict__["ALT_" + alt_str]
129129
else:
130130
# Convert the altStr to an integer
131-
alt = int(altStr)
132-
else:
131+
alt = int(alt_str)
132+
except (ValueError, KeyError):
133133
# No alt specified, just set to -1 (default)
134134
alt = -1
135135

0 commit comments

Comments
 (0)