# sumfunc3.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 total of all integers of the requested type in the 
 specified files.
"""

import sys 

def getIntList(filename, lower, upper):
    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, 1, 10000))
        elif (numType == "neg"):
            intList.extend(getIntList(fname, -10000, -1))
        else:
            intList.extend(getIntList(fname, -10000, 10000))
    return sum(intList)



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

sum = procFile(sys.argv[1], *sys.argv[2:])
print "sum = ", sum

