How to read data from a closed file and use that data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuugie
    New Member
    • Sep 2010
    • 32

    How to read data from a closed file and use that data?

    I'm trying to make a definition(writ eHistogram) to write a histogram and I can't figure it out. I understand the concept of it, but I just can't figure out how to do it. Could someone help me within the hour. It is suppose to take the data from tallies.txt and have a similar output, except it suppose to show the percents as "a - b, c - d, and e - e" instead of a, b, c, d and e.

    Thank You!

    Code:
    class TallySheet:
      """Manage tallies for a collection of values.
    
      Values can either be from a consecutive range of integers, or a
      consecutive sequence of characters from the alphabet.
      """
      def __init__(self, minVal, maxVal):
        """Create an initially empty tally sheet.
    
        minVal    the minimum acceptable value for later insertion
        maxVal    the minimum acceptable value for later insertion
        """
        self._minV = minVal
        self._maxV = maxVal
        maxIndex = self._toIndex(maxVal)
        self._tallies = [0] * (maxIndex + 1)   # a list of counters, each initially zero
    
      def increment(self, val):
        """Increment the tally for the respective value.
        
        raise a TypeError if the given value is not of the proper type
        raise a ValueError if the given value is not within proper range
        """
        ind = self._toIndex(val)
        if not 0 <= ind < len(self._tallies):
          raise ValueError('parameter '+str(val)+' out of range')
        self._tallies[ind] += 1
    
      def getCount(self, val):
        """Return the total number of current tallies for the given value.
    
        raise a TypeError if the given value is not of the proper type
        raise a ValueError if the given value is not within proper range
        """
        ind = self._toIndex(val)
        if not 0 <= ind < len(self._tallies):
          raise ValueError('parameter '+str(val)+' out of range')
        return self._tallies[ind]
    
      def getTotalCount(self):
        """Return the total number of current tallies."""
        return sum(self._tallies)
    
      def _toIndex(self, val):
        """Convert from a native value to a legitimate index.
    
        Return the resulting index (such that _minV is mapped to 0)
        """
        try:
          if isinstance(self._minV, str):
            i = ord(val) - ord(self._minV)
          else:
            i = int( val - self._minV )
        except TypeError:
          raise TypeError('parameter '+str(val)+' of incorrect type')
        return i
    
      def writeTable(self, outfile):
        """Write a comprehensive table of results.
    
        Report each value, the count for that value, and the percentage usage.
    
        outfile   an already open file with write access.
        """
        outfile.write('Value  Count Percent \n----- ------ -------\n')
        total = max(self.getTotalCount(), 1)  # avoid division by zero
        for ind in range(len(self._tallies)):
          label = self._makeLabel(ind)
          count = self._tallies[ind]
          pct = 100.0 * count / total
          outfile.write('%s %6d %6.2f%%\n' % (label, count, pct))
    
      def _makeLabel(self, ind):
        """Convert index to a string in native range."""
        if isinstance(self._minV, int):
          return '%5d' % (ind + self._minV)
        else:
          return '  %s  ' % chr(ind + ord(self._minV))
    
       
    if __name__ == '__main__':
      t = TallySheet('a', 'e')
    
      for i in range(10):
        t.increment('a')
    
      if t.getCount('a') == 10:
        print 'Test1: Success'
      else:
        print 'Test1: Failure'
    
      for i in range(5):
        t.increment('b')
    
      if t.getCount('b') == 5:
        print 'Test2: Success'
      else:
        print 'Test2: Failure'
    
      for i in range(10):
        t.increment('c')
    
      if t.getCount('c') == 10:
        print 'Test3: Success'
      else:
        print 'Test3: Failure'
    
      for i in range(5):
        t.increment('d')
    
      if t.getCount('d') == 5:
        print 'Test4: Success'
      else:
        print 'Test4: Failure'
    
      if t.getTotalCount() == 30:
        print 'Test5: Success'
      else:
        print 'Test5: Failure'
    
      f = file('tally.txt', 'w')
      t.writeTable(f)
      f.close()
      print "Please open and check tally.txt file"
      f2 = file('histogram.txt','w')
      t.writeHistogram(f2, f)
      f2.close()
      print "Please open and check histogram.txt file"
    
      raw_input("Press the ENTER key to continue...")
    Any help would be much appreciated! Thank you once again.
Working...