Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most of the time such measurement involves some heavy processing inside the main loop, so I developed a simple wrapper function that prints loop progress for any iterable:</p> <pre><code>from datetime import datetime def print_progress(iterable, percent_step=1): total = float(len(iterable)) # or 1 means that iterable has &lt; 100 elems abs_step = int((total * percent_step)/100) or 1 for i, obj in enumerate(iterable): if i and not i % abs_step: print "{0:.2%} processed, {1:%H:%M:%S}".format(i/total, datetime.now()) yield obj </code></pre> <p><code>percent_step</code> argument defines the granularity level of printing: each time the loop finished processing the <code>percent_step</code> % amount of data, the total percentage of processed data is printed on the screen.</p> <p>Then can apply this wrapper to the loop:</p> <pre><code>for x in print_progress(my_list): # processing ... </code></pre> <p>The same iterator could be applied for <em>django querysets</em>, but calculating total object count before the loop could be expensive by itself, so in case of retrieving all objects from the database like <em>PostgreSQL</em> it is better to replace <code>len</code> with direct SQL:</p> <pre><code>"select reltuples from pg_class where relname='%s'" % table_name </code></pre> <h3>Another variant that prints progress after the specified amount of seconds:</h3> <pre><code>def print_progress(iterable, second_step=10): total = float(len(iterable)) import time time1 = time.time() for i, obj in enumerate(iterable): if time.time() - time1 &gt; second_step: print "{0:.2%} processed".format(i/total) time1 = time.time() yield obj </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload