Some Python code improvements (#2264)

* Usage of "enumerate()" with start parameter
* Use the with keyword when dealing with file objects
According to: https://docs.python.org/3.6/tutorial/inputoutput.html
* String format using ".format()"
* Chained comparisons are faster than using the "and" operator
This commit is contained in:
Michal 2018-09-03 00:31:00 +02:00 committed by Jonathan White
parent 08a36b4d40
commit 7592f40de2

View file

@ -39,23 +39,21 @@ else:
keysymMap = {} keysymMap = {}
f = open(keysymdef, "r") with open(keysymdef, "r") as fid:
for line in f: for line in fid:
match = re.search(r'0x([0-9a-fA-F]+)\s+/\* U\+([0-9a-fA-F]+)', line) match = re.search(r'0x([0-9a-fA-F]+)\s+/\* U\+([0-9a-fA-F]+)', line)
if match: if match:
keysym = int(match.group(1), 16) keysym = int(match.group(1), 16)
unicodeVal = int(match.group(2), 16) unicodeVal = int(match.group(2), 16)
# ignore 1:1 mappings # ignore 1:1 mappings
if keysym >= 0x0020 and keysym <= 0x007e: if 0x0020 <= keysym <= 0x007e or 0x00a0 <= keysym <= 0x00ff:
continue continue
if keysym >= 0x00a0 and keysym <= 0x00ff: # ignore unicode | 0x01000000 mappings
continue elif keysym >= 0x1000000:
# ignore unicode | 0x01000000 mappings continue
if keysym >= 0x1000000:
continue
keysymMap[unicodeVal] = keysym keysymMap[unicodeVal] = keysym
keysymMap = collections.OrderedDict(sorted(keysymMap.items(), key=lambda t: t[0])) keysymMap = collections.OrderedDict(sorted(keysymMap.items(), key=lambda t: t[0]))
@ -64,26 +62,24 @@ print("""/*
*/ */
""") """)
print("const int AutoTypePlatformX11::m_unicodeToKeysymLen = " + str(len(keysymMap)) + ";") print("const int AutoTypePlatformX11::m_unicodeToKeysymLen = {0};".format(len(keysymMap)))
print() print()
print("const uint AutoTypePlatformX11::m_unicodeToKeysymKeys[] = {") print("const uint AutoTypePlatformX11::m_unicodeToKeysymKeys[] = {")
keys = keysymMap.keys() keys = keysymMap.keys()
keyLen = len(keys) keyLen = len(keys)
i = 1 for idx, val in enumerate(keys, start=1):
for val in keys:
hexVal = "{0:#0{1}x}".format(val, 6) hexVal = "{0:#0{1}x}".format(val, 6)
if i == keyLen: if idx == keyLen:
print(hexVal) print(hexVal)
elif (i % cols) == 0: elif (idx % cols) == 0:
print(hexVal + ",") print(hexVal + ",")
elif ((i - 1) % cols) == 0: elif ((idx - 1) % cols) == 0:
print(" " + hexVal + ", ", end="") print(" " + hexVal + ", ", end="")
else: else:
print(hexVal + ", ", end="") print(hexVal + ", ", end="")
i += 1
print("};") print("};")
print() print()
@ -91,17 +87,15 @@ print()
print("const uint AutoTypePlatformX11::m_unicodeToKeysymValues[] = {") print("const uint AutoTypePlatformX11::m_unicodeToKeysymValues[] = {")
values = keysymMap.values() values = keysymMap.values()
valuesLen = len(values) valuesLen = len(values)
i = 1 for idx, val in enumerate(values, start=1):
for val in values:
hexVal = "{0:#0{1}x}".format(val, 6) hexVal = "{0:#0{1}x}".format(val, 6)
if i == valuesLen: if idx == valuesLen:
print(hexVal) print(hexVal)
elif (i % cols) == 0: elif (idx % cols) == 0:
print(hexVal + ",") print(hexVal + ",")
elif ((i - 1) % cols) == 0: elif ((idx - 1) % cols) == 0:
print(" " + hexVal + ", ", end="") print(" " + hexVal + ", ", end="")
else: else:
print(hexVal + ", ", end="") print(hexVal + ", ", end="")
i += 1
print("};") print("};")