# sumfunc5.py
# Written by Todd Wareham for CS 2500
"""
Given a type of integer (positive non-zero <= 10,000, negative 
 non-zero >= -10,000, all) and the names of one or more files of strings 
 (all of which need not be integers) as command-line arguments, computes 
 and prints the number, total, minimum, and maximum of all integers of the 
 requested type in the specified files.

Note that range-checking in function getIntList() is now implemented
 with default lower and upper bounds.
"""

import sys 

def getIntList(filename, lower=-10000, upper=10000):
    intList = []
    for line in file(filename):
        for num in line.split():
            try:
                if (lower <= int(num) <= upper):
                    intList.append(int(num))
            except ValueError:
                pass
    return intList

def procFile(numType, filename, *filenames):
    intList = []
    fileList = list(filenames)
    fileList.append(filename)
    for fname in fileList:
        if (numType == "pos"):
            intList.extend(getIntList(fname, lower=1))
        elif (numType == "neg"):
            intList.extend(getIntList(fname, upper=-1))
        else:
            intList.extend(getIntList(fname))
    return len (intList), sum(intList), min(intList), max(intList)



if len(sys.argv) < 3:
    print "usage: ", sys.argv[0], " {pos, neg, all} filename1 {filename2, ...}"
    sys.exit(1)

num, sum, min, max = procFile(sys.argv[1], *sys.argv[2:])
print "#int, sum, min , max = ", num, ",", sum, ",", min, ",", max

