Project

General

Profile

« Previous | Next » 

Revision 1472

xml_func.py: _avg: Support empty inputs by returning None. Moved _range after _rangeStart/_rangeEnd since it's less frequently used.

View differences:

lib/xml_func.py
176 176
    return units.quantity2str(quantity)
177 177
funcs['_units'] = _units
178 178

  
179
def _range(items):
180
    items = dict(conv_items(float, items))
181
    from_ = items.get('from', None)
182
    to = items.get('to', None)
183
    if from_ == None or to == None: return None
184
    return str(to - from_)
185
funcs['_range'] = _range
186

  
187 179
def parse_range(str_, range_sep='-'):
188 180
    default = (str_, None)
189 181
    start, sep, end = str_.partition(range_sep)
......
205 197
    return parse_range(value)[1]
206 198
funcs['_rangeEnd'] = _rangeEnd
207 199

  
200
def _range(items):
201
    items = dict(conv_items(float, items))
202
    from_ = items.get('from', None)
203
    to = items.get('to', None)
204
    if from_ == None or to == None: return None
205
    return str(to - from_)
206
funcs['_range'] = _range
207

  
208 208
def _avg(items):
209 209
    count = 0
210 210
    sum_ = 0.
211 211
    for name, value in conv_items(float, items):
212 212
        count += 1
213 213
        sum_ += value
214
    return str(sum_/count)
214
    if count == 0: return None # input is empty
215
    else: return str(sum_/count)
215 216
funcs['_avg'] = _avg
216 217

  
217 218
class CvException(Exception):

Also available in: Unified diff