Skip to content

Commit 186d2c1

Browse files
author
Zheyuan Chen
authored
Merge pull request #6 from terciodemelo/fix-to-infix
Refactors To.__next__ for better code quality
2 parents d9d345f + aef95e5 commit 186d2c1

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

syntax_sugar/infix.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,30 @@ def __iter__(self):
5151
def __next__(self):
5252
if self.step == 0 or not self.step /is_a/ int:
5353
raise TypeError('Interval must be an integer different from 0')
54-
elif self.type == 'number':
55-
if self.step >= 0 and self.curr > self.end:
56-
raise StopIteration
57-
elif self.step <= 0 and self.curr < self.end:
58-
raise StopIteration
59-
else:
60-
ret = self.curr
61-
self.curr += self.step
62-
return ret
54+
def next_number():
55+
too_big = self.step > 0 and self.curr > self.end
56+
too_small = self.step < 0 and self.curr < self.end
57+
58+
if too_big or too_small: raise StopIteration
59+
60+
ret = self.curr
61+
self.curr += self.step
62+
return ret
63+
64+
def next_char():
65+
too_big = self.step > 0 and ord(self.curr) > ord(self.end)
66+
too_small = self.step < 0 and ord(self.curr) < ord(self.end)
67+
68+
if too_big or too_small: raise StopIteration
69+
70+
ret = self.curr
71+
self.curr = chr(ord(self.curr) + self.step)
72+
return ret
73+
74+
if self.type == 'number':
75+
return next_number()
6376
elif self.type == 'char':
64-
if self.step >= 0 and ord(self.curr) > ord(self.end):
65-
raise StopIteration
66-
elif self.step <= 0 and ord(self.curr) < ord(self.end):
67-
raise StopIteration
68-
else:
69-
ret = self.curr
70-
self.curr = chr(ord(self.curr) + self.step)
71-
return ret
77+
return next_char()
7278
else:
7379
raise StopIteration
7480

0 commit comments

Comments
 (0)