# sumfunc2.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 name of a file 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 file.
"""

import sys 

def getIntList(filename, lower, upper):
    intList = []
    for line in open(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):
    if (numType == "pos"):
        return sum(getIntList(filename, 1, 10000))
    elif (numType == "neg"):
        return sum(getIntList(filename, -10000, -1))
    else:
        return sum(getIntList(filename, -10000, 10000))



if len(sys.argv) != 3:
    print "usage: ", sys.argv[0], " {pos, neg, all} filename"
    sys.exit(1)

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

