Skip to content

Commit

Permalink
Allow for configuration of bounds and counting method.
Browse files Browse the repository at this point in the history
  • Loading branch information
strichter committed Jan 8, 2017
1 parent 3f67514 commit db9283a
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/z3c/rml/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ def fromUnicode(self, ustr):
class IntegerSequence(Sequence):
"""A sequence of integers."""

def __init__(
self,
numberingStartsAt=0,
lowerBoundInclusive=True,
upperBoundInclusive=True,
*args, **kw):
super(Sequence, self).__init__(*args, **kw)
self.numberingStartsAt = numberingStartsAt
self.lowerBoundInclusive = lowerBoundInclusive
self.upperBoundInclusive = upperBoundInclusive

def fromUnicode(self, ustr):
ustr = ustr.strip()
pieces = self.splitre.split(ustr)
Expand All @@ -218,13 +229,24 @@ def fromUnicode(self, ustr):
continue
# The piece is a range.
if '-' in piece:
start, end = piece.split('-')
start, end = map(int, piece.split('-'))
# Make sure internally numbering starts as 0
start -= self.numberingStartsAt
end -= self.numberingStartsAt
# Apply lower-bound exclusive.
if not self.lowerBoundInclusive:
start += 1
# Apply upper-bound inclusive.
if self.upperBoundInclusive:
end += 1
# Make range lower and upper bound inclusive.
numbers.append((int(start), int(end)+1))
numbers.append((start, end))
continue
# The piece is just a number
numbers.append((int(piece), int(piece)+1))
return list(numbers)
value = int(piece)
value -= self.numberingStartsAt
numbers.append((value, value+1))
return numbers

class Choice(BaseChoice):
"""A choice of several values. The values are always case-insensitive."""
Expand Down

0 comments on commit db9283a

Please sign in to comment.