Progress when parsing a large file with SAX

Diez B. Roggisch deets at nospam.web.de
Mon Feb 12 08:21:42 EST 2007


Anastasios Hatzis wrote:

> Diez B. Roggisch wrote:
> 
> ...
> 
> I got the same problem with large XML as Marc.
> 
> So you deserve also my thanks for the example. :-)
> 
>> 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
>> 
> 
> I guess some client impl need to call read() on a wrapped xml file until
> all portions of the file are read.

You should fed the PercentageFile-object to the xml-parser, like this:

parser = xml.sax.make_parser()
pf = PercentageFile(filename)
parser.parse(pf)

 
>>    @property
>>    def percentage(self):
>>        return float(self.delivered) / self.size * 100.0
>> 
> 
> @property?
> 
> What is that supposed to do?

It's making percentage a property, so that you can access it like this:

pf.percentage

instead of 

pf.percentage()

Google python property for details, or pydoc property.

Diez



More information about the Python-list mailing list