Remove xrange for run both Python 2 and Python 3

In python 3, range() does what xrange() used to do and xrange() does not
exist. If you want to write code that will run on both Python 2 and
Python 3, you can't use xrange().

range() can actually be faster in some cases - eg. if iterating over the
same sequence multiple times. xrange() has to reconstruct the integer
object every time, but range() will have real integer objects.
(It will always perform worse in terms of memory however)

xrange() isn't usable in all cases where a real list is needed.
For instance, it doesn't support slices, or any list methods.

Change-Id: I5233438a864bb00d04ba7fb2b1688cacb0473691
This commit is contained in:
Luong Anh Tuan 2016-10-12 11:39:01 +07:00
parent 92c0f6eeec
commit 05f12bfffa
7 changed files with 11 additions and 11 deletions

View File

@ -296,7 +296,7 @@ class Expr(ASTNode):
if isinstance(expr_tree, p.ParseResults):
expr_tree = expr_tree.asList()
if isinstance(expr_tree, list):
for i in xrange(0, len(expr_tree)):
for i in range(0, len(expr_tree)):
if isinstance(expr_tree[i], list):
expr_tree[i] = Expr(span, expr_tree[i])
self.expr_tree = expr_tree

View File

@ -118,7 +118,7 @@ class Span(object):
startcolno = 0
endlineno = 0
endcolno = 0
for lineno in xrange(0, len(splitted)):
for lineno in range(0, len(splitted)):
line = splitted[lineno]
if current_pos <= self.lo <= len(line) + current_pos:
startlineno = lineno + 1
@ -139,7 +139,7 @@ class Span(object):
splitted = self._text.splitlines()
current_pos = 0
lineno = 0
for _ in xrange(0, len(splitted)):
for _ in range(0, len(splitted)):
line = splitted[lineno]
if current_pos < self.lo < len(line) + current_pos:
return lineno + 1

View File

@ -93,7 +93,7 @@ def banana_grammar(emitter=emit.PrintEmitter()):
def action_create_connections(s, l, t):
ast_conn = ast.into_connection(t[0])
ast_conn.span = ast.make_span(s, l, t)
for i in xrange(1, len(t)):
for i in range(1, len(t)):
next_conn = ast.into_connection(t[i])
ast_conn.connect_to(next_conn, emitter)
return ast_conn

View File

@ -143,12 +143,12 @@ class MonascaAggregateLDP(bt.BaseLDP):
lambda x: x["metric"]["timestamp"], l),
separated_metrics)
metric_count = len(separated_metrics)
for index in xrange(0, len(separated_metrics[0])):
for index in range(0, len(separated_metrics[0])):
new_value = reducer[0](
separated_metrics[0][index]["metric"]["value"],
metric_count)
new_timestamp = separated_metrics[0][index]["metric"]["timestamp"]
for metric_index in xrange(1, metric_count):
for metric_index in range(1, metric_count):
new_value = reducer[1](new_value, helpers.interpolate(
new_timestamp,
separated_metrics[metric_index],

View File

@ -113,13 +113,13 @@ class MonascaCombineLDP(bt.BaseLDP):
lambda l: map(
lambda x: x["metric"]["timestamp"], l[1]),
separated_metrics)
for index in xrange(0, len(separated_metrics[0][1])):
for index in range(0, len(separated_metrics[0][1])):
current_env = {
separated_metrics[0][0]:
separated_metrics[0][1][index]["metric"]["value"]
}
timestamp = all_timestamp[0][index]
for metric_index in xrange(1, len(separated_metrics)):
for metric_index in range(1, len(separated_metrics)):
metric_prop = separated_metrics[metric_index]
metric_name = metric_prop[0]
current_env[metric_name] = helpers.interpolate(

View File

@ -116,7 +116,7 @@ class MonascaDerivativeLDP(bt.BaseLDP):
last_timestamp = timestamps[0]
tmp_all_values = [all_values[0]]
tmp_timestamps = [last_timestamp]
for index in xrange(1, len(timestamps)):
for index in range(1, len(timestamps)):
if timestamps[index] == last_timestamp:
continue
else:
@ -135,7 +135,7 @@ class MonascaDerivativeLDP(bt.BaseLDP):
float(all_values[1] - all_values[0]) /
float(timestamps[1] - timestamps[0])
]
for index in xrange(1, n):
for index in range(1, n):
new_values.append(
float(all_values[index + 1] - all_values[index - 1]) /
float(timestamps[index + 1] - timestamps[index - 1])

View File

@ -37,7 +37,7 @@ class TestMonascaAggregateLDP(MonanasTestCase):
# iterable = map(lambda i: {"metric": {"value": i}}, iterable)
cnt = len(iterable)
acc = fn[0](iterable[0], cnt)
for index in xrange(1, cnt):
for index in range(1, cnt):
acc = fn[1](acc, iterable[index], cnt)
return acc