Robot Framework Integrated Development Environment (RIDE)
misc.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 __future__ import division
17 
18 from operator import add, sub
19 
20 from .platform import PY2
21 from .robottypes import is_integer
22 from .unic import unic
23 
24 
25 
34 def roundup(number, ndigits=0, return_type=None):
35  result = _roundup(number, ndigits)
36  if not return_type:
37  return_type = float if ndigits > 0 else int
38  return return_type(result)
39 
40 
41 # Python 2 rounds half away from zero (as taught in school) but Python 3
42 # uses "bankers' rounding" that rounds half towards the even number. We want
43 # consistent rounding and expect Python 2 style to be more familiar for users.
44 if PY2:
45 
48  _roundup = round
49 else:
50  def _roundup(number, ndigits):
51  precision = 10 ** (-1 * ndigits)
52  if number % (0.5 * precision) == 0 and number % precision != 0:
53  operator = add if number > 0 else sub
54  number = operator(number, 0.1 * precision)
55  return round(number, ndigits)
56 
57 
58 
76 def printable_name(string, code_style=False):
77  if code_style and '_' in string:
78  string = string.replace('_', ' ')
79  parts = string.split()
80  if code_style and len(parts) == 1 \
81  and not (string.isalpha() and string.islower()):
82  parts = _split_camel_case(parts[0])
83  return ' '.join(part[0].upper() + part[1:] for part in parts)
84 
85 
86 def _split_camel_case(string):
87  tokens = []
88  token = []
89  for prev, char, next in zip(' ' + string, string, string[1:] + ' '):
90  if _is_camel_case_boundary(prev, char, next):
91  if token:
92  tokens.append(''.join(token))
93  token = [char]
94  else:
95  token.append(char)
96  if token:
97  tokens.append(''.join(token))
98  return tokens
99 
100 
101 def _is_camel_case_boundary(prev, char, next):
102  if prev.isdigit():
103  return not char.isdigit()
104  if char.isupper():
105  return next.islower() or prev.isalpha() and not prev.isupper()
106  return char.isdigit()
107 
108 
109 def plural_or_not(item):
110  count = item if is_integer(item) else len(item)
111  return '' if count == 1 else 's'
112 
113 
114 
115 def seq2str(sequence, quote="'", sep=', ', lastsep=' and '):
116  sequence = [quote + unic(item) + quote for item in sequence]
117  if not sequence:
118  return ''
119  if len(sequence) == 1:
120  return sequence[0]
121  last_two = lastsep.join(sequence[-2:])
122  return sep.join(sequence[:-2] + [last_two])
123 
124 
125 
126 def seq2str2(sequence):
127  if not sequence:
128  return '[ ]'
129  return '[ %s ]' % ' | '.join(unic(item) for item in sequence)
def printable_name(string, code_style=False)
Generates and returns printable name from the given string.
Definition: misc.py:76
def _is_camel_case_boundary(prev, char, next)
Definition: misc.py:101
def seq2str2(sequence)
Returns sequence in format [ item 1 | item 2 | ...
Definition: misc.py:126
def roundup(number, ndigits=0, return_type=None)
Rounds number to the given number of digits.
Definition: misc.py:34
def seq2str(sequence, quote="'", sep=', ', lastsep=' and ')
Returns sequence in format ‘'item 1’, 'item 2' and 'item 3'`.
Definition: misc.py:115
def _split_camel_case(string)
Definition: misc.py:86