There’s something that bothers me about the PRange function (and PTri). There are several cases where the function returns an empty pattern.
e.g. :
print(PRange(1,5,1)) → P[1, 2, 3, 4]
print(PRange(1,5,-1)) → P
print(PRange(5,1,1)) → P
print(PRange(5,1,-1)) → P[5, 4, 3, 2]
print(PRange(5,5)) → P
I know it’s the way range() in python act, but I was wondering if we could modify it to always return something.
I try this, but don’t know if it’s a good way.
def PRange(start, stop=1, step=1):
“”" Returns a Pattern equivalent toPattern(range(start, stop, step))
“”"
if start == stop:
return Pattern(start)
if (start > stop and step > 0) or (start < stop and step < 0):
step = step*-1
return Pattern(list(range(*[val for val in (start, stop, step) if val is not None])))