Search code examples
pythonperformancecoding-styleprogress-bareasy-install

Python: how to use a progressbar inside my function


I am using the following function:

def LAS2TXTGridClip(inFile,poly,MinPoints=1):
        sf = shapefile.Reader(poly) #open shpfile
        sr = sf.shapeRecords()
        poly_filename, ext = path.splitext(poly)
        inFile_filename = os.path.splitext(os.path.basename(inFile))[0]
        for i in xrange(len(sr)):
            verts = np.array(sr[i].shape.points,float)
            record = sr[i].record[0]
            inside_points = [p for p in lasfile.File(inFile,None,'r') if pnpoly(p.x, p.y, verts)]
            if len(inside_points) >= MinPoints:
                file_out = open("{0}_{1}_{2}.txt".format(poly_filename, inFile_filename, record), "w")
                for p in inside_points:
                    file_out.write("%s %s %s %s %s %s %s %s %s %s %s" % (p.x, p.y, p.z, p.intensity,p.return_number,p.number_of_returns,p.scan_direction,p.flightline_edge,p.classification,p.scan_angle,record)+ "\n")
                file_out.close()

where for i in xrange(len(sr)): the function will be process several times. The len(sr) is around half million and I wish a insert a progress bar in order to have an idea of the time I need to wait (it's friday). I have the following question:

  1. Which is the "best and easy" progressbar for python 27 on windows OS 64bit?
  2. I found progressbar module but I have problem to use easy_install progressbar after this step.
  3. where is the best position to insert the progressbar?

Solution

  • What problems you are having with the module progressbar? It's very nice solution.

    $ cd progressbar-2.2/
    $ sudo python setup.py install
    ...blablabla...
    $ python
    >>> from progressbar import ProgressBar
    >>> pbar = ProgressBar(10)
    >>> for i in range(10):
    ...     pbar.update(i+1)
    ... 
    100% |######################################################################|