-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
21 lines (19 loc) · 810 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ItemsCount(object):
def __init__(self, value):
self._value = value
def __call__(self, sequence):
if isinstance(self._value, (bytes, str,)):
if self._value.endswith("%"):
total_count = len(sequence)
percentage = int(self._value[:-1])
# at least one sentence should be chosen
count = max(1, total_count*percentage // 100)
return sequence[:count]
else:
return sequence[:int(self._value)]
elif isinstance(self._value, (int, float)):
return sequence[:int(self._value)]
else:
ValueError("Unsuported value of items count '%s'." % self._value)
def __repr__(self):
return to_string("<ItemsCount: %r>" % self._value)