~mynameisvc/+junk/MPMMS

« back to all changes in this revision

Viewing changes to assignment2.py

  • Committer: Vincent Chow
  • Date: 2008-05-30 16:17:05 UTC
  • Revision ID: vchow@debiandownstairs-20080530161705-409282507c36358f
Two functions, constructSublist and writeSublist

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        if x == '-o':
47
47
            outputfile = y
48
48
 
49
 
            # rwbuffer = memsize // recordsize
50
49
 
51
50
    # print outputfile, inputfile, recordsize, maxphases, memsize
52
51
 
53
 
    rwbuffer = memsize // recordsize
 
52
    rwbuffer = memsize // recordsize    # how many records are we able to read into memory?
54
53
 
55
 
        # if something > p
56
 
        # print "Number of phases needed exceeds the maximum allowed."
 
54
    # canSort()
57
55
 
58
56
 
59
57
 
60
58
def canSort():
61
59
    pass
 
60
    # if something > p
 
61
    # print "Number of phases needed exceeds the maximum allowed."
 
62
 
62
63
 
63
64
 
64
65
# constructSublist("input.txt", "output.txt", 2)
65
66
def constructSublist(inputfile, outputfile, rwbuffer):
66
67
 
67
68
    freader = open(inputfile, 'r', False) # I/O is unbuffered. All read/write go directly to disk
68
 
    fwriter = open(outputfile, 'w')
69
69
 
 
70
    # read as many records as will fit into memory
70
71
    unsortedString= freader.readline()
71
 
 
72
 
 
73
 
    uid = 1
74
 
    sublistDestination = "phase" + str(phase) + "_" + str(uid) + ".txt"
75
 
    
76
72
    for i in range(rwbuffer-1):
77
73
        unsortedString += freader.readline()
78
74
 
80
76
 
81
77
    unsortedList = map(int, unsortedList)
82
78
 
83
 
    ## unsortedList = sorted(unsortedList)
84
79
    unsortedList.sort()
85
80
 
86
81
    unsortedList = map(str, unsortedList) # converts the list of integers to a list of strings
87
82
 
88
 
    fwriter.write("\n".join(unsortedList))
89
 
 
90
 
    print unsortedList
91
 
    print sublistDestination
92
 
    
 
83
    writeSubLists("\n".join(unsortedList))
 
84
 
 
85
 
 
86
 
93
87
    freader.close()
 
88
 
 
89
    
 
90
def writeSubLists(sortedSublist):
 
91
 
 
92
    uid = 0
 
93
 
 
94
    uid += 1
 
95
 
 
96
    # sorted sub-lists should be written to files such phase1_1.txt, phase1_2.txt, phase1_3.txt, etc.
 
97
    sublistDestination = "phase" + str(phase) + "_" + str(uid) + ".txt" 
 
98
 
 
99
    fwriter = open(sublistDestination, 'w')
 
100
 
 
101
    fwriter.write(sortedSublist)
 
102
 
94
103
    fwriter.close()
95
104
 
 
105
    print "Successfully wrote to", sublistDestination
 
106
 
96
107
def displayStats():
97
108
    print "Total I/O Time:"
98
109
    print "Total Processing Time:"