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