Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
We need to deal with a range ('0-3' or '0:3'), a list ('0,1,2,3') and a single item
'''
# First we deal with a list
rep_cap_list = repeated_capability.split(',')
if len(rep_cap_list) > 1:
# We have a list so call ourselves again to let the iterable instance handle it
return _convert_repeated_capabilities(rep_cap_list, prefix)
# Now we deal with ranges
# We remove any prefix and change ':' to '-'
r = repeated_capability.strip().replace(prefix, '').replace(':', '-')
rc = r.split('-')
if len(rc) > 1:
if len(rc) > 2:
raise errors.InvalidRepeatedCapabilityError("Multiple '-' or ':'", repeated_capability)
start = int(rc[0])
end = int(rc[1])
if end < start:
rng = range(start, end - 1, -1)
else:
rng = range(start, end + 1)
return _convert_repeated_capabilities(rng, prefix)
# If we made it here, it must be a simple item so we remove any prefix and return
return [repeated_capability.replace(prefix, '').strip()]
Each instance should return a list of strings, without prefix
- '0' --> ['0']
- 0 --> ['0']
- '0, 1' --> ['0', '1']
- 'ScriptTrigger0, ScriptTrigger1' --> ['0', '1']
- '0-1' --> ['0', '1']
- '0:1' --> ['0', '1']
- '0-1,4' --> ['0', '1', '4']
- range(0, 2) --> ['0', '1']
- slice(0, 2) --> ['0', '1']
- (0, 1, 4) --> ['0', '1', '4']
- ('0-1', 4) --> ['0', '1', '4']
- (slice(0, 1), '2', [4, '5-6'], '7-9', '11:14', '16, 17') -->
['0', '2', '4', '5', '6', '7', '8', '9', '11', '12', '13', '14', '16', '17']
'''
raise errors.InvalidRepeatedCapabilityError('Invalid type', type(arg))
except errors.InvalidRepeatedCapabilityError:
pass
try:
convert_repeated_capabilities(['5', '6-8-10'])
assert False
except errors.InvalidRepeatedCapabilityError:
pass
try:
convert_repeated_capabilities(('5', '6-8-10'))
assert False
except errors.InvalidRepeatedCapabilityError:
pass
try:
convert_repeated_capabilities('5,6-8-10')
assert False
except errors.InvalidRepeatedCapabilityError:
pass
try:
convert_repeated_capabilities(5.0)
assert False
except errors.InvalidRepeatedCapabilityError:
pass
try:
convert_repeated_capabilities([5.0, '0'])
assert False
except errors.InvalidRepeatedCapabilityError:
pass
try:
convert_repeated_capabilities((5.0, '0'))
assert False
except errors.InvalidRepeatedCapabilityError:
pass