Source code for irfpy.util.options
''' Misc function related to option handling.
.. codeauthor:: Yoshifumi Futaana
'''
[docs]def str2range(str):
''' String conversion to a range list
:param str: String expression of range. "-" or "," separation allowed.
:returns: List of the range. See example below.
For example, if str="1-3", the returned is [1,2,3].
If str="1-3,7,9-12", the returned is [1,2,3,7,9,10,11,12].
Negative value, float value not supported.
>>> print(str2range("1-3"))
[1, 2, 3]
>>> print(str2range("0-3,7-8,13"))
[0, 1, 2, 3, 7, 8, 13]
'''
token = [t.strip() for t in str.split(',')]
rangelist = []
for t in token:
if t.startswith('-'):
raise ValueError(
'Invalid format. Negative value not supported (%s)' % t)
rngidx = t.find('-')
if rngidx == -1:
rangelist.append(int(t))
else:
r0 = int(t[:rngidx])
r1 = int(t[rngidx+1:])
rangelist = rangelist + list(range(r0, r1 + 1))
return rangelist
import unittest
import doctest
[docs]def doctests():
return unittest.TestSuite((
doctest.DocTestSuite(),
))
if __name__ == '__main__':
unittest.main(defaultTest='doctests')