# sumfunc6a.py
# Written by Todd Wareham for CS 2500
"""
Module containing functions for extracting and processing integer-lists
 stored in textfiles.
"""

def getIntList(filename, lower=-10000, upper=10000):
    """
    getIntList(filename, lower, upper):
        Extract all strings corresponding to integers in file
         filename and return in a list those integers between
         loower and upper inclusive. Note that lower and
         upper have defau,lt values -10000 and 10000,
         respectively.
    """
    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):
    """
    procFile(numType, filename, *filenames):
        Extract list L of all integers in files in file-list
         [filename *filenames] of type numType in {"neg", "pos",
         "all"{ and return quadruple consisting of the number 
         of elements in L, sum of elements in L, minimum value in 
         L, and maximum value in L.
    """
    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)



##  Module testing code

if __name__ == "__main__":

    import sys 

    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

