snapshot of 4.0a development. initial git repo commit
[erange.git] / fraction.py
1 #
2 #  fraction.py
3 #  ENRAGE
4 #
5
6 try:
7     import psyco
8     psyco.full()
9 except:
10     pass
11
12 from random import random
13 import sys
14
15 print "%s: version 1.0" % sys.argv[0]
16
17 def main(argv=None):
18     if not argv:
19         argv = sys.argv
20
21     if len(sys.argv) < 4:
22         print "usage: python %s fraction infile outfile" % sys.argv[0]
23         sys.exit(1)
24
25     fraction = float(sys.argv[1])
26     infile = sys.argv[2]
27     outfile = argv[3]
28
29     doFraction(fraction, infile, outfile)
30
31
32 def doFraction(fraction, inFileName, outFileName):
33     infile = open(inFileName)
34     outfile = open(outFileName, "w")
35
36     totalIndex = 0
37     fractionIndex = 0
38     for line in infile:
39         totalIndex += 1
40         if random() <= fraction:
41             outfile.write(line)
42             fractionIndex += 1
43
44     infile.close()
45     outfile.close()
46
47     print "%d / %d = %.2f" % (fractionIndex, totalIndex, float(fractionIndex) / totalIndex)
48
49
50 if __name__ == "__main__":
51     main(sys.argv)