Robot Framework
frange.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from .robottypes import is_integer, is_string
17 
18 
19 
20 def frange(*args):
21  if all(is_integer(arg) for arg in args):
22  return list(range(*args))
23  start, stop, step = _get_start_stop_step(args)
24  digits = max(_digits(start), _digits(stop), _digits(step))
25  factor = pow(10, digits)
26  return [x / factor
27  for x in range(round(start*factor), round(stop*factor), round(step*factor))]
28 
29 
31  if len(args) == 1:
32  return 0, args[0], 1
33  if len(args) == 2:
34  return args[0], args[1], 1
35  if len(args) == 3:
36  return args
37  raise TypeError('frange expected 1-3 arguments, got %d.' % len(args))
38 
39 
40 def _digits(number):
41  if not is_string(number):
42  number = repr(number)
43  if 'e' in number:
44  return _digits_with_exponent(number)
45  if '.' in number:
46  return _digits_with_fractional(number)
47  return 0
48 
49 
51  mantissa, exponent = number.split('e')
52  mantissa_digits = _digits(mantissa)
53  exponent_digits = int(exponent) * -1
54  return max(mantissa_digits + exponent_digits, 0)
55 
56 
58  fractional = number.split('.')[1]
59  if fractional == '0':
60  return 0
61  return len(fractional)
def _digits_with_fractional(number)
Definition: frange.py:57
def _digits(number)
Definition: frange.py:40
def _digits_with_exponent(number)
Definition: frange.py:50
def _get_start_stop_step(args)
Definition: frange.py:30
def frange(*args)
Like range() but accepts float arguments.
Definition: frange.py:20