#
#  fraction.py
#  ENRAGE
#

try:
    import psyco
    psyco.full()
except:
    pass

import sys
from random import random


print "fraction: version 1.1"

def main(argv=None):
    if not argv:
        argv = sys.argv

    if len(sys.argv) < 4:
        print "usage: python %s fraction infile outfile" % sys.argv[0]
        sys.exit(1)

    fraction = float(sys.argv[1])
    infile = sys.argv[2]
    outfile = argv[3]

    doFraction(fraction, infile, outfile)


def doFraction(fraction, inFileName, outFileName):
    infile = open(inFileName)
    outfile = open(outFileName, "w")

    totalIndex = 0
    fractionIndex = 0
    for line in infile:
        totalIndex += 1
        if random() <= fraction:
            outfile.write(line)
            fractionIndex += 1

    infile.close()
    outfile.close()

    print "%d / %d = %.2f" % (fractionIndex, totalIndex, float(fractionIndex) / totalIndex)


if __name__ == "__main__":
    main(sys.argv)