/scripts/getavg.py
Python | 58 lines | 48 code | 8 blank | 2 comment | 15 complexity | 6ccaefe31d49e2bd3baa35dac4df5001 MD5 | raw file
Possible License(s): GPL-2.0
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import os
- import sys
- from optparse import OptionParser
- usage = "usage: %prog [options]"
- parser = OptionParser(usage = usage)
- parser.add_option("-i", "--input", dest="inputfile",
- help="input file, two tab separated columns -- x and y",
- metavar="INPUT")
- parser.add_option("-b", "--bounds", dest="bounds",
- help="bounds on x for extremum finding, comma separated list"
- + " of two values 'a,b' ('x' values are taken only"
- + " from [a, b]) or one number 'a' ('x' values are "
- + " taken if they are more than 'a')",
- metavar="INPUT")
- (options, args) = parser.parse_args()
- if options.inputfile == None:
- inputfile = sys.stdin
- else:
- inputfile = open(options.inputfile, 'r')
- lower = None
- upper = None
- if options.bounds != None:
- bounds = options.bounds.split(',')
- if len(bounds) == 2:
- lower = float(bounds[0])
- upper = float(bounds[1])
- elif len(bounds) == 1:
- lower = float(bounds[0])
- input = []
- for line in inputfile:
- input.append([float(i) for i in line.split()])
- size = len(input)
- if size == 0:
- print "Error, input is empty"
- os._exit(1)
- sum = 0.0
- count = 0
- for i in input:
- x = i[0]
- y = i[1]
- if lower != None and upper != None:
- if x < lower or x > upper:
- continue
- elif lower != None and x < lower:
- continue
- sum += y
- count += 1
- print sum / count