Progress when parsing a large file with SAX

Diez B. Roggisch deets at nospam.web.de
Mon Feb 12 06:20:02 EST 2007


marc.omorain at gmail.com wrote:

> Hi there,
> 
> I have a 28mb XML file which I parse with SAX. I have some processing
> to do in the startElement / endElement callbacks, which slows the
> parsing down to about 60 seconds on my machine.
> 
> My application is unresponsive for this time, so I would like to show
> a progress bar. I could show a spinner to show that the application is
> responsive, but I would prefer to show a percentage. Is there any way
> to query the parser to see how many bytes of the input file have been
> processed so far?

I'd create a file-like object that does this for you. It should wrap the
original file, and count the number of bytes delivered. Something along
these lines (untested!!!):

class PercentageFile(object):

   def __init__(self, filename):
       self.size = os.stat(filename)[6]
       self.delivered = 0
       self.f = file(filename)

   def read(self, size=None):
       if size is None:
          self.delivered = self.size
          return self.f.read()
       data = self.f.read(size)
       self.delivered += len(data)
       return data

   @property
   def percentage(self):
       return float(self.delivered) / self.size * 100.0

Diez
   



More information about the Python-list mailing list