Skip to content

Commit

Permalink
[Python] Improve Python consistency: Use function_name(…) throughout …
Browse files Browse the repository at this point in the history
…(PEP8)
  • Loading branch information
practicalswift committed Feb 29, 2016
1 parent ada88e1 commit f6d6585
Show file tree
Hide file tree
Showing 92 changed files with 384 additions and 384 deletions.
4 changes: 2 additions & 2 deletions benchmark/scripts/Benchmark_Driver
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def parse_results(res, optset):
tests.append(mem_test)
return tests

def submit_to_LNT(data, url):
def submit_to_lnt(data, url):
print "\nSubmitting results to LNT server..."
json_report = {'input_data': json.dumps(data), 'commit': '1'}
data = urllib.urlencode(json_report)
Expand Down Expand Up @@ -224,7 +224,7 @@ def submit(args):
'Start Time': starttime}
print "End time:\t", endtime

submit_to_LNT(data, args.lnt_host)
submit_to_lnt(data, args.lnt_host)
return 0

def run(args):
Expand Down
42 changes: 21 additions & 21 deletions benchmark/scripts/compare_perf_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
ShowSpeedup = 1
PrintAllScores = 0

def parseInt(word):
def parse_int(word):
try:
return int(word)
except:
raise Exception("Expected integer value, not " + word)

def getScores(fname):
def get_scores(fname):
scores = {}
worstscores = {}
nums = {}
Expand All @@ -65,8 +65,8 @@ def getScores(fname):
if not m.group(KEYGROUP) in scores:
scores[m.group(KEYGROUP)] = []
worstscores[m.group(KEYGROUP)] = []
scores[m.group(KEYGROUP)].append(parseInt(m.group(BESTGROUP)))
worstscores[m.group(KEYGROUP)].append(parseInt(m.group(WORSTGROUP)))
scores[m.group(KEYGROUP)].append(parse_int(m.group(BESTGROUP)))
worstscores[m.group(KEYGROUP)].append(parse_int(m.group(WORSTGROUP)))
if is_total:
nums[m.group(KEYGROUP)] = ""
else:
Expand All @@ -77,10 +77,10 @@ def getScores(fname):
f.close()
return scores, worstscores, runs, nums

def isMaxScore(newscore, maxscore, invert):
def is_max_score(newscore, maxscore, invert):
return not maxscore or (newscore > maxscore if not invert else newscore < maxscore)

def compareScores(key, score1, worstsample1, score2, worstsample2, runs, num):
def compare_scores(key, score1, worstsample1, score2, worstsample2, runs, num):
print num.rjust(3),
print key.ljust(25),
bestscore1 = None
Expand All @@ -91,25 +91,25 @@ def compareScores(key, score1, worstsample1, score2, worstsample2, runs, num):
minworst = not minbest
r = 0
for score in score1:
if isMaxScore(newscore=score, maxscore=bestscore1, invert=minbest):
if is_max_score(newscore=score, maxscore=bestscore1, invert=minbest):
bestscore1 = score
if isMaxScore(newscore=score, maxscore=worstscore1, invert=minworst):
if is_max_score(newscore=score, maxscore=worstscore1, invert=minworst):
worstscore1 = score
if PrintAllScores:
print ("%d" % score).rjust(16),
for score in worstsample1:
if isMaxScore(newscore=score, maxscore=worstscore1, invert=minworst):
if is_max_score(newscore=score, maxscore=worstscore1, invert=minworst):
worstscore1 = score
for score in score2:
if isMaxScore(newscore=score, maxscore=bestscore2, invert=minbest):
if is_max_score(newscore=score, maxscore=bestscore2, invert=minbest):
bestscore2 = score
if isMaxScore(newscore=score, maxscore=worstscore2, invert=minworst):
if is_max_score(newscore=score, maxscore=worstscore2, invert=minworst):
worstscore2 = score
if PrintAllScores:
print ("%d" % score).rjust(16),
r += 1
for score in worstsample2:
if isMaxScore(newscore=score, maxscore=worstscore2, invert=minworst):
if is_max_score(newscore=score, maxscore=worstscore2, invert=minworst):
worstscore2 = score
while r < runs:
if PrintAllScores:
Expand All @@ -136,20 +136,20 @@ def compareScores(key, score1, worstsample1, score2, worstsample2, runs, num):
# check if the worst->best interval for each configuration overlap.
if minbest:
if (bestscore1 < bestscore2 and bestscore2 < worstscore1) \
or (bestscore2 < bestscore1 and bestscore1 < worstscore2):
or (bestscore2 < bestscore1 and bestscore1 < worstscore2):
print "(?)",
else:
if (worstscore1 < worstscore2 and worstscore2 < bestscore1) \
or (worstscore2 < worstscore1 and worstscore1 < bestscore2):
or (worstscore2 < worstscore1 and worstscore1 < bestscore2):
print "(?)",
print

def printBestScores(key, scores):
def print_best_scores(key, scores):
print key,
bestscore = None
minbest = IsTime
for score in scores:
if isMaxScore(newscore=score, maxscore=bestscore, invert=minbest):
if is_max_score(newscore=score, maxscore=bestscore, invert=minbest):
bestscore = score
print ", %d" % bestscore

Expand All @@ -163,19 +163,19 @@ def usage():
sys.exit(1)
file1 = sys.argv[1]
if len(sys.argv) < 3:
scores, worstscores, runs, nums = getScores(file1)
scores, worstscores, runs, nums = get_scores(file1)
keys = list(set(scores.keys()))
keys.sort()
for key in keys:
printBestScores(key, scores[key])
print_best_scores(key, scores[key])
sys.exit(0)

file2 = sys.argv[2]
if len(sys.argv) > 3:
SCORERE = re.compile(sys.argv[3])

scores1, worstscores1, runs1, nums = getScores(file1)
scores2, worstscores2, runs2, nums = getScores(file2)
scores1, worstscores1, runs1, nums = get_scores(file1)
scores2, worstscores2, runs2, nums = get_scores(file2)

runs = runs1
if runs2 > runs:
Expand Down Expand Up @@ -213,4 +213,4 @@ def usage():
if key not in scores2:
print key, "not in", file2
continue
compareScores(key, scores1[key], worstscores1[key], scores2[key], worstscores2[key], runs, nums[key])
compare_scores(key, scores1[key], worstscores1[key], scores2[key], worstscores2[key], runs, nums[key])
4 changes: 2 additions & 2 deletions unittests/Basic/UnicodeGraphemeBreakTest.cpp.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

%{

from GYBUnicodeDataUtils import get_grapheme_cluster_break_tests_as_UTF8
from GYBUnicodeDataUtils import get_grapheme_cluster_break_tests_as_utf8

grapheme_cluster_break_tests = \
get_grapheme_cluster_break_tests_as_UTF8(unicodeGraphemeBreakTestFile)
get_grapheme_cluster_break_tests_as_utf8(unicodeGraphemeBreakTestFile)

}%

Expand Down
30 changes: 15 additions & 15 deletions utils/GYBUnicodeDataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ class UnicodeTrieGenerator(object):
supp_first_level_index_bits = 5
supp_second_level_index_bits = 8

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

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

def get_supp_first_level_index(self, cp):
Expand Down Expand Up @@ -287,17 +287,17 @@ def splat(self, 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 Down Expand Up @@ -379,7 +379,7 @@ def map_index(idx):
j += 1
i += 1

def _int_to_LE_bytes(self, data, width):
def _int_to_le_bytes(self, data, width):
if width == 1:
assert(data & ~0xff == 0)
return [data]
Expand All @@ -388,11 +388,11 @@ def _int_to_LE_bytes(self, data, width):
return [data & 0xff, data & 0xff00]
assert(False)

def _int_list_to_LE_bytes(self, ints, width):
def _int_list_to_le_bytes(self, ints, width):
return [
byte
for elt in ints
for byte in self._int_to_LE_bytes(elt, 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
Expand All @@ -415,16 +415,16 @@ 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_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_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_bytes = self._int_list_to_le_bytes(
supp_lookup1_words, self.supp_lookup1_bytes_per_entry)
supp_lookup2_bytes = self._int_list_to_LE_bytes(
supp_lookup2_bytes = self._int_list_to_le_bytes(
supp_lookup2_words, self.supp_lookup2_bytes_per_entry)
supp_data_bytes = self._int_list_to_LE_bytes(
supp_data_bytes = self._int_list_to_le_bytes(
supp_data_words, self.supp_data_bytes_per_entry)

self.trie_bytes = []
Expand Down Expand Up @@ -502,7 +502,7 @@ def get_extended_grapheme_cluster_rules_matrix(grapheme_cluster_break_property_t

return result

def get_grapheme_cluster_break_tests_as_UTF8(grapheme_break_test_file_name):
def get_grapheme_cluster_break_tests_as_utf8(grapheme_break_test_file_name):
def _convert_line(line):
# Strip comments.
line = re.sub('#.*', '', line).strip()
Expand Down
2 changes: 1 addition & 1 deletion utils/SwiftIntTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def all_real_number_type_names():
def all_numeric_type_names():
return all_integer_type_names() + all_real_number_type_names()

def numeric_type_names_Macintosh_only():
def numeric_type_names_macintosh_only():
return ['Float80']

# Swift_Programming_Language/Expressions.html
Expand Down
58 changes: 29 additions & 29 deletions utils/cmpcodesize/cmpcodesize/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
SortedInfixes = sorted(Infixes)


def addFunction(sizes, function, start_addr, end_addr, group_by_prefix):
def add_function(sizes, function, start_addr, end_addr, group_by_prefix):
if not function or start_addr is None or end_addr is None:
return

Expand Down Expand Up @@ -85,7 +85,7 @@ def flatten(*args):
yield x


def readSizes(sizes, file_name, function_details, group_by_prefix):
def read_sizes(sizes, file_name, function_details, group_by_prefix):
# Check if multiple architectures are supported by the object file.
# Prefer arm64 if available.
architectures = subprocess.check_output(["otool", "-V", "-f", file_name]).split("\n")
Expand Down Expand Up @@ -134,7 +134,7 @@ def readSizes(sizes, file_name, function_details, group_by_prefix):
sectionMatch = sectionPattern.match(line)
if labelMatch:
funcName = labelMatch.group(1)
addFunction(sizes, currFunc, start_addr, end_addr, group_by_prefix)
add_function(sizes, currFunc, start_addr, end_addr, group_by_prefix)
currFunc = funcName
start_addr = None
end_addr = None
Expand All @@ -146,10 +146,10 @@ def readSizes(sizes, file_name, function_details, group_by_prefix):
if sectName == "__textcoal_nt":
sectName = "__text"

addFunction(sizes, currFunc, start_addr, end_addr, group_by_prefix)
add_function(sizes, currFunc, start_addr, end_addr, group_by_prefix)


def compareSizes(old_sizes, new_sizes, name_key, title):
def compare_sizes(old_sizes, new_sizes, name_key, title):
oldSize = old_sizes[name_key]
newSize = new_sizes[name_key]
if oldSize is not None and newSize is not None:
Expand All @@ -160,13 +160,13 @@ def compareSizes(old_sizes, new_sizes, name_key, title):
print("%-26s%16s: %8d %8d %6s" % (title, name_key, oldSize, newSize, perc))


def compareSizesOfFile(old_files, new_files, all_sections, list_categories):
def compare_sizes_of_file(old_files, new_files, all_sections, list_categories):
old_sizes = collections.defaultdict(int)
new_sizes = collections.defaultdict(int)
for oldFile in old_files:
readSizes(old_sizes, oldFile, list_categories, True)
read_sizes(old_sizes, oldFile, list_categories, True)
for newFile in new_files:
readSizes(new_sizes, newFile, list_categories, True)
read_sizes(new_sizes, newFile, list_categories, True)

if len(old_files) == 1 and len(new_files) == 1:
oldBase = os.path.basename(old_files[0])
Expand All @@ -177,43 +177,43 @@ def compareSizesOfFile(old_files, new_files, all_sections, list_categories):
else:
title = "old-new"

compareSizes(old_sizes, new_sizes, "__text", title)
compare_sizes(old_sizes, new_sizes, "__text", title)
if list_categories:
prev = None
for categoryName in sorted(Prefixes.values()) + sorted(Infixes.values()) + ["Unknown"]:
if categoryName != prev:
compareSizes(old_sizes, new_sizes, categoryName, "")
compare_sizes(old_sizes, new_sizes, categoryName, "")
prev = categoryName

if all_sections:
sectionTitle = " section"
compareSizes(old_sizes, new_sizes, "__textcoal_nt", sectionTitle)
compareSizes(old_sizes, new_sizes, "__stubs", sectionTitle)
compareSizes(old_sizes, new_sizes, "__const", sectionTitle)
compareSizes(old_sizes, new_sizes, "__cstring", sectionTitle)
compareSizes(old_sizes, new_sizes, "__objc_methname", sectionTitle)
compareSizes(old_sizes, new_sizes, "__const", sectionTitle)
compareSizes(old_sizes, new_sizes, "__objc_const", sectionTitle)
compareSizes(old_sizes, new_sizes, "__data", sectionTitle)
compareSizes(old_sizes, new_sizes, "__swift1_proto", sectionTitle)
compareSizes(old_sizes, new_sizes, "__common", sectionTitle)
compareSizes(old_sizes, new_sizes, "__bss", sectionTitle)


def listFunctionSizes(size_array):
compare_sizes(old_sizes, new_sizes, "__textcoal_nt", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__stubs", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__const", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__cstring", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__objc_methname", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__const", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__objc_const", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__data", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__swift1_proto", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__common", sectionTitle)
compare_sizes(old_sizes, new_sizes, "__bss", sectionTitle)


def list_function_sizes(size_array):
for pair in sorted(size_array, key=itemgetter(1)):
name = pair[0]
size = pair[1]
yield "%8d %s" % (size, name)


def compareFunctionSizes(old_files, new_files):
def compare_function_sizes(old_files, new_files):
old_sizes = collections.defaultdict(int)
new_sizes = collections.defaultdict(int)
for name in old_files:
readSizes(old_sizes, name, True, False)
read_sizes(old_sizes, name, True, False)
for name in new_files:
readSizes(new_sizes, name, True, False)
read_sizes(new_sizes, name, True, False)

onlyInFile1 = []
onlyInFile2 = []
Expand All @@ -239,13 +239,13 @@ def compareFunctionSizes(old_files, new_files):

if onlyInFile1:
print("Only in old file(s)")
print(os.linesep.join(listFunctionSizes(onlyInFile1)))
print(os.linesep.join(list_function_sizes(onlyInFile1)))
print("Total size of functions only in old file: {}".format(onlyInFile1Size))
print()

if onlyInFile2:
print("Only in new files(s)")
print(os.linesep.join(listFunctionSizes(onlyInFile2)))
print(os.linesep.join(list_function_sizes(onlyInFile2)))
print("Total size of functions only in new file: {}".format(onlyInFile2Size))
print()

Expand Down
Loading

0 comments on commit f6d6585

Please sign in to comment.