Skip to content

Commit

Permalink
[Python] Improve Python consistency: "variable in function should be …
Browse files Browse the repository at this point in the history
…lowercase" (N806)

The repo contains roughly 80 Python scripts. "snake_case" naming is used for
local variables in all those scripts. This is the form recommended by the PEP 8
naming recommendations (Python Software Foundation) and typically associated
with idiomatic Python code.

However, in nine of the 80 scripts there were at least one instance of
"camelCase" naming prior to this commit.

This commit improves consistency in the Python code base by making sure that
these nine remaining files follow the variable naming convention used for
Python code in the project.

References:
* PEP 8: https://www.python.org/dev/peps/pep-0008/
* pep8-naming: https://pypi.python.org/pypi/pep8-naming
  • Loading branch information
practicalswift committed Mar 2, 2016
1 parent 659811e commit e33f174
Show file tree
Hide file tree
Showing 11 changed files with 535 additions and 535 deletions.
50 changes: 25 additions & 25 deletions benchmark/scripts/Benchmark_Driver
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ DRIVER_DIR = os.path.dirname(os.path.realpath(__file__))
def parse_results(res, optset):
# Parse lines like this
# #,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),MEDIAN(μs),PEAK_MEMORY(B)
SCORERE = re.compile(r"(\d+),[ \t]*(\w+)," +
",".join([r"[ \t]*([\d.]+)"] * 7))
score_re = re.compile(r"(\d+),[ \t]*(\w+)," +
",".join([r"[ \t]*([\d.]+)"] * 7))
# The Totals line would be parsed like this.
TOTALRE = re.compile(r"()(Totals)," +
",".join([r"[ \t]*([\d.]+)"] * 7))
KEYGROUP = 2
VALGROUP = 4
MEMGROUP = 9
total_re = re.compile(r"()(Totals)," +
",".join([r"[ \t]*([\d.]+)"] * 7))
key_group = 2
val_group = 4
mem_group = 9

tests = []
for line in res.split():
m = SCORERE.match(line)
m = score_re.match(line)
if not m:
m = TOTALRE.match(line)
m = total_re.match(line)
if not m:
continue
testresult = int(m.group(VALGROUP))
testname = m.group(KEYGROUP)
testresult = int(m.group(val_group))
testname = m.group(key_group)
test = {}
test['Data'] = [testresult]
test['Info'] = {}
test['Name'] = "nts.swift/" + optset + "." + testname + ".exec"
tests.append(test)
if testname != 'Totals':
mem_testresult = int(m.group(MEMGROUP))
mem_testresult = int(m.group(mem_group))
mem_test = {}
mem_test['Data'] = [mem_testresult]
mem_test['Info'] = {}
Expand Down Expand Up @@ -89,26 +89,26 @@ def instrument_test(driver_path, test, num_samples):
[peak_memory])

# Average sample results
NUM_SAMPLES_INDEX = 2
MIN_INDEX = 3
MAX_INDEX = 4
AVG_START_INDEX = 5
num_samples_index = 2
min_index = 3
max_index = 4
avg_start_index = 5

# TODO: Correctly take stdev
avg_test_output = test_outputs[0]
avg_test_output[AVG_START_INDEX:] = map(int,
avg_test_output[AVG_START_INDEX:])
avg_test_output[avg_start_index:] = map(int,
avg_test_output[avg_start_index:])
for test_output in test_outputs[1:]:
for i in range(AVG_START_INDEX, len(test_output)):
for i in range(avg_start_index, len(test_output)):
avg_test_output[i] += int(test_output[i])
for i in range(AVG_START_INDEX, len(avg_test_output)):
for i in range(avg_start_index, len(avg_test_output)):
avg_test_output[i] = int(round(avg_test_output[i] /
float(len(test_outputs))))
avg_test_output[NUM_SAMPLES_INDEX] = num_samples
avg_test_output[MIN_INDEX] = min(test_outputs,
key=lambda x: x[MIN_INDEX])[MIN_INDEX]
avg_test_output[MAX_INDEX] = max(test_outputs,
key=lambda x: x[MAX_INDEX])[MAX_INDEX]
avg_test_output[num_samples_index] = num_samples
avg_test_output[min_index] = min(test_outputs,
key=lambda x: x[min_index])[min_index]
avg_test_output[max_index] = max(test_outputs,
key=lambda x: x[max_index])[max_index]
avg_test_output = map(str, avg_test_output)

return avg_test_output
Expand Down
20 changes: 10 additions & 10 deletions stdlib/public/stubs/UnicodeExtendedGraphemeClusters.cpp.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ trie_generator.verify(grapheme_cluster_break_property_table)
}%
/// Before compression:
/// BMP_lookup: ${len(trie_generator.BMP_lookup)}
/// BMP_data: ${len(trie_generator.BMP_data)} x ${len(trie_generator.BMP_data[0])}
/// bmp_lookup: ${len(trie_generator.bmp_lookup)}
/// bmp_data: ${len(trie_generator.bmp_data)} x ${len(trie_generator.bmp_data[0])}
/// supp_lookup1: ${len(trie_generator.supp_lookup1)}
/// supp_lookup2: ${len(trie_generator.supp_lookup2)} x ${len(trie_generator.supp_lookup2[0])}
/// supp_data: ${len(trie_generator.supp_data)} x ${len(trie_generator.supp_data[0])}
Expand All @@ -44,8 +44,8 @@ trie_generator.verify(grapheme_cluster_break_property_table)
}%
/// After compression:
/// BMP_lookup: ${len(trie_generator.BMP_lookup)}
/// BMP_data: ${len(trie_generator.BMP_data)} x ${len(trie_generator.BMP_data[0])}
/// bmp_lookup: ${len(trie_generator.bmp_lookup)}
/// bmp_data: ${len(trie_generator.bmp_data)} x ${len(trie_generator.bmp_data[0])}
/// supp_lookup1: ${len(trie_generator.supp_lookup1)}
/// supp_lookup2: ${len(trie_generator.supp_lookup2)} x ${len(trie_generator.supp_lookup2[0])}
/// supp_data: ${len(trie_generator.supp_data)} x ${len(trie_generator.supp_data[0])}
Expand Down Expand Up @@ -95,22 +95,22 @@ struct _swift_stdlib_GraphemeClusterBreakPropertyTrieMetadataTy {
SWIFT_RUNTIME_STDLIB_INTERFACE
extern "C" const struct _swift_stdlib_GraphemeClusterBreakPropertyTrieMetadataTy
_swift_stdlib_GraphemeClusterBreakPropertyTrieMetadata = {
${trie_generator.BMP_first_level_index_bits},
${trie_generator.BMP_data_offset_bits},
${trie_generator.bmp_first_level_index_bits},
${trie_generator.bmp_data_offset_bits},
${trie_generator.supp_first_level_index_bits},
${trie_generator.supp_second_level_index_bits},
${trie_generator.supp_data_offset_bits},
${trie_generator.BMP_lookup_bytes_per_entry},
${trie_generator.BMP_data_bytes_per_entry},
${trie_generator.bmp_lookup_bytes_per_entry},
${trie_generator.bmp_data_bytes_per_entry},
${trie_generator.supp_lookup1_bytes_per_entry},
${trie_generator.supp_lookup2_bytes_per_entry},
${trie_generator.supp_data_bytes_per_entry},
${len(trie_generator.trie_bytes)},
${trie_generator.BMP_lookup_bytes_offset},
${trie_generator.BMP_data_bytes_offset},
${trie_generator.bmp_lookup_bytes_offset},
${trie_generator.bmp_data_bytes_offset},
${trie_generator.supp_lookup1_bytes_offset},
${trie_generator.supp_lookup2_bytes_offset},
${trie_generator.supp_data_bytes_offset},
Expand Down
92 changes: 46 additions & 46 deletions utils/GYBUnicodeDataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ def get_numeric_value(self, cp):
class UnicodeTrieGenerator(object):
# Note: if you change any of these parameters, don't forget to update the
# ASCII art above.
BMP_first_level_index_bits = 8
bmp_first_level_index_bits = 8

supp_first_level_index_bits = 5
supp_second_level_index_bits = 8

def get_bmp_first_level_index(self, cp):
return cp >> self.BMP_data_offset_bits
return cp >> self.bmp_data_offset_bits

def get_bmp_data_offset(self, cp):
return cp & ((1 << self.BMP_data_offset_bits) - 1)
return cp & ((1 << self.bmp_data_offset_bits) - 1)

def get_supp_first_level_index(self, cp):
return cp >> (self.supp_second_level_index_bits + self.supp_data_offset_bits)
Expand All @@ -235,7 +235,7 @@ def create_tables(self):
Don't change parameter values after calling this method.
"""
self.BMP_data_offset_bits = 16 - self.BMP_first_level_index_bits
self.bmp_data_offset_bits = 16 - self.bmp_first_level_index_bits

self.supp_data_offset_bits = \
21 - self.supp_first_level_index_bits - \
Expand All @@ -250,12 +250,12 @@ def create_tables(self):
self.supp_data_offset_bits)

# A mapping from BMP first-level index to BMP data block index.
self.BMP_lookup = [i for i in range(0, 1 << self.BMP_first_level_index_bits)]
self.bmp_lookup = [i for i in range(0, 1 << self.bmp_first_level_index_bits)]

# An array of BMP data blocks.
self.BMP_data = [
[-1 for i in range(0, 1 << self.BMP_data_offset_bits)]
for i in range(0, 1 << self.BMP_first_level_index_bits)
self.bmp_data = [
[-1 for i in range(0, 1 << self.bmp_data_offset_bits)]
for i in range(0, 1 << self.bmp_first_level_index_bits)
]

# A mapping from supp first-level index to an index of the second-level
Expand All @@ -277,27 +277,27 @@ def create_tables(self):
]

def splat(self, value):
for i in range(0, len(self.BMP_data)):
for j in range(0, len(self.BMP_data[i])):
self.BMP_data[i][j] = value
for i in range(0, len(self.bmp_data)):
for j in range(0, len(self.bmp_data[i])):
self.bmp_data[i][j] = value

for i in range(0, len(self.supp_data)):
for j in range(0, len(self.supp_data[i])):
self.supp_data[i][j] = value

def set_value(self, cp, value):
if cp <= 0xffff:
data_block_index = self.BMP_lookup[self.get_bmp_first_level_index(cp)]
self.BMP_data[data_block_index][self.get_bmp_data_offset(cp)] = value
data_block_index = self.bmp_lookup[self.get_bmp_first_level_index(cp)]
self.bmp_data[data_block_index][self.get_bmp_data_offset(cp)] = value
else:
second_lookup_index = self.supp_lookup1[self.get_supp_first_level_index(cp)]
data_block_index = self.supp_lookup2[second_lookup_index][self.get_supp_second_level_index(cp)]
self.supp_data[data_block_index][self.get_supp_data_offset(cp)] = value

def get_value(self, cp):
if cp <= 0xffff:
data_block_index = self.BMP_lookup[self.get_bmp_first_level_index(cp)]
return self.BMP_data[data_block_index][self.get_bmp_data_offset(cp)]
data_block_index = self.bmp_lookup[self.get_bmp_first_level_index(cp)]
return self.bmp_data[data_block_index][self.get_bmp_data_offset(cp)]
else:
second_lookup_index = self.supp_lookup1[self.get_supp_first_level_index(cp)]
data_block_index = self.supp_lookup2[second_lookup_index][self.get_supp_second_level_index(cp)]
Expand All @@ -310,9 +310,9 @@ def fill_from_unicode_property(self, unicode_property):

def verify(self, unicode_property):
for cp in range(0, 0x110000):
expectedValue = unicode_property.get_value(cp)
actualValue = self.get_value(cp)
assert(expectedValue == actualValue)
expected_value = unicode_property.get_value(cp)
actual_value = self.get_value(cp)
assert(expected_value == actual_value)

def freeze(self):
"""Compress internal trie representation.
Expand All @@ -333,17 +333,17 @@ def map_index(idx):
# result of the `map` is explicitly converted to a `list`.
return list(map(map_index, indexes))

# If self.BMP_data contains identical data blocks, keep the first one,
# remove duplicates and change the indexes in self.BMP_lookup to point to
# If self.bmp_data contains identical data blocks, keep the first one,
# remove duplicates and change the indexes in self.bmp_lookup to point to
# the first one.
i = 0
while i < len(self.BMP_data):
while i < len(self.bmp_data):
j = i + 1
while j < len(self.BMP_data):
if self.BMP_data[i] == self.BMP_data[j]:
self.BMP_data.pop(j)
self.BMP_lookup = \
remap_indexes(self.BMP_lookup, old_idx=j, new_idx=i)
while j < len(self.bmp_data):
if self.bmp_data[i] == self.bmp_data[j]:
self.bmp_data.pop(j)
self.bmp_lookup = \
remap_indexes(self.bmp_lookup, old_idx=j, new_idx=i)
else:
j += 1
i += 1
Expand Down Expand Up @@ -395,17 +395,17 @@ def _int_list_to_le_bytes(self, ints, width):
for byte in self._int_to_le_bytes(elt, width)]

def serialize(self, unicode_property):
self.BMP_lookup_bytes_per_entry = 1 if len(self.BMP_data) < 256 else 2
self.BMP_data_bytes_per_entry = 1
self.bmp_lookup_bytes_per_entry = 1 if len(self.bmp_data) < 256 else 2
self.bmp_data_bytes_per_entry = 1

self.supp_lookup1_bytes_per_entry = 1 if len(self.supp_lookup2) < 256 else 2
self.supp_lookup2_bytes_per_entry = 1 if len(self.supp_data) < 256 else 2
self.supp_data_bytes_per_entry = 1

BMP_lookup_words = list(self.BMP_lookup)
BMP_data_words = [
bmp_lookup_words = list(self.bmp_lookup)
bmp_data_words = [
unicode_property.to_numeric_value(elt)
for block in self.BMP_data
for block in self.bmp_data
for elt in block]

supp_lookup1_words = list(self.supp_lookup1)
Expand All @@ -415,10 +415,10 @@ def serialize(self, unicode_property):
for block in self.supp_data
for elt in block]

BMP_lookup_bytes = self._int_list_to_le_bytes(
BMP_lookup_words, self.BMP_lookup_bytes_per_entry)
BMP_data_bytes = self._int_list_to_le_bytes(
BMP_data_words, self.BMP_data_bytes_per_entry)
bmp_lookup_bytes = self._int_list_to_le_bytes(
bmp_lookup_words, self.bmp_lookup_bytes_per_entry)
bmp_data_bytes = self._int_list_to_le_bytes(
bmp_data_words, self.bmp_data_bytes_per_entry)

supp_lookup1_bytes = self._int_list_to_le_bytes(
supp_lookup1_words, self.supp_lookup1_bytes_per_entry)
Expand All @@ -429,11 +429,11 @@ def serialize(self, unicode_property):

self.trie_bytes = []

self.BMP_lookup_bytes_offset = 0
self.trie_bytes += BMP_lookup_bytes
self.bmp_lookup_bytes_offset = 0
self.trie_bytes += bmp_lookup_bytes

self.BMP_data_bytes_offset = len(self.trie_bytes)
self.trie_bytes += BMP_data_bytes
self.bmp_data_bytes_offset = len(self.trie_bytes)
self.trie_bytes += bmp_data_bytes

self.supp_lookup1_bytes_offset = len(self.trie_bytes)
self.trie_bytes += supp_lookup1_bytes
Expand Down Expand Up @@ -478,9 +478,9 @@ def get_extended_grapheme_cluster_rules_matrix(grapheme_cluster_break_property_t
dict.fromkeys(any_value, None)

# Iterate over rules in the order of increasing priority.
for firstList, action, secondList in reversed(rules):
for first in firstList:
for second in secondList:
for first_list, action, second_list in reversed(rules):
for first in first_list:
for second in second_list:
rules_matrix[first][second] = action

# Make sure we can pack one row of the matrix into a 'uint16_t'.
Expand Down Expand Up @@ -532,10 +532,10 @@ def _convert_line(line):
if code_point >= 0xd800 and code_point <= 0xdfff:
code_point = 0x200b
code_point = (b'\U%(cp)08x' % {b'cp': code_point}).decode('unicode_escape', 'strict')
as_UTF8_bytes = bytearray(code_point.encode('utf8', 'strict'))
as_UTF8_escaped = ''.join(['\\x%(byte)02x' % {'byte': byte} for byte in as_UTF8_bytes])
test += as_UTF8_escaped
curr_bytes += len(as_UTF8_bytes)
as_utf8_bytes = bytearray(code_point.encode('utf8', 'strict'))
as_utf8_escaped = ''.join(['\\x%(byte)02x' % {'byte': byte} for byte in as_utf8_bytes])
test += as_utf8_escaped
curr_bytes += len(as_utf8_bytes)

return (test, boundaries)

Expand Down
Loading

0 comments on commit e33f174

Please sign in to comment.