Skip to content

Commit

Permalink
Handle missing xrange in Python 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
shakefu committed Aug 4, 2015
1 parent 0e4dc8b commit 9ef3aa8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 7 additions & 0 deletions pytool/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import textwrap


# Handle Python 3 without using 2to3
try:
xrange
except:
xrange = range


def wrap(text, width=70, indent=''):
"""
Return `text` wrapped to `width` while trimming leading indentation and
Expand Down
10 changes: 5 additions & 5 deletions test/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_list_proxy_comparison_operator():
def test_list_proxy_contains_operator():
l = [1, 2]
p = pytool.proxy.ListProxy(l)
for i in xrange(4):
for i in range(4):
eq_(i in l, i in p)


Expand Down Expand Up @@ -80,7 +80,7 @@ def test_list_proxy_slicing():
eq_(l[1:3], [5, 10])
p[1:3] = p
eq_(p, l)
p[1:3] = xrange(5)
p[1:3] = range(5)
eq_(p, l)
eq_(p[1:5], [0, 1, 2, 3])
del p[1:]
Expand All @@ -97,21 +97,21 @@ def test_list_proxy_addition():
n = p + [3, 4]
eq_(n, [1, 2, 3, 4])
ok_(isinstance(n, list))
n = p + xrange(2)
n = p + range(2)
eq_(n, [1, 2, 0, 1])
ok_(isinstance(n, list))
n = [3, 4] + p
eq_(n, [3, 4, 1, 2])
ok_(isinstance(n, list))
n = xrange(2) + p
n = range(2) + p
eq_(n, [0, 1, 1, 2])
ok_(isinstance(n, list))
n = p.__radd__(p)
eq_(n, [1, 2, 1, 2])
ok_(isinstance(n, list))
p += [3, 4]
eq_(p, [1, 2, 3, 4])
p += xrange(2)
p += range(2)
eq_(p, [1, 2, 3, 4, 0, 1])
p += p
eq_(p, [1, 2, 3, 4, 0, 1, 1, 2, 3, 4, 0, 1])
Expand Down
4 changes: 2 additions & 2 deletions test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ def test_week_seconds_start():

def test_week_seconds():
start = pytool.time.week_start(datetime.now())
for i in xrange(7):
for i in range(7):
eq_(pytool.time.week_seconds(start + timedelta(days=i)),
timedelta(days=i).total_seconds())


def test_week_seconds_to_datetime():
for i in xrange(7):
for i in range(7):
eq_(pytool.time.week_start(datetime.now()) + timedelta(days=i),
pytool.time.week_seconds_to_datetime(
timedelta(days=i).total_seconds()))
Expand Down

0 comments on commit 9ef3aa8

Please sign in to comment.