Counter

A simple progress counter to print the number of iterations and time elapsed in a for-loop execution.

from mlxtend.utils import Counter

Overview

The Counter class implements an object for displaying the number of iterations and time elapsed in a for-loop. Please note that the Counter was implemented for efficiency; thus, the Counter offers only very basic functionality in order to avoid relatively expensive evaluations (of if-else statements).

References

Example 1 - Counting the iterations in a for-loop

from mlxtend.utils import Counter
import time

cnt = Counter()
for i in range(20):
    # do some computation
    time.sleep(0.1)
    cnt.update()
20 iter | 2 sec

Note that the first number displays the current iteration, and the second number shows the time elapsed after initializing the Counter.

API

Counter(stderr=False, start_newline=True, precision=0, name=None)

Class to display the progress of for-loop iterators.

Parameters

Attributes

Examples

>>> cnt = Counter()
>>> for i in range(20):
...     # do some computation
...     time.sleep(0.1)
...     cnt.update()
20 iter | 2 sec
>>> print('The counter was initialized.'
' %d seconds ago.' % (time.time() - cnt.start_time))
The counter was initialized 2 seconds ago
>>> print('The counter was last updated'
' %d seconds ago.' % (time.time() - cnt.end_time))
The counter was last updated 0 seconds ago.

For more usage examples, please see http://rasbt.github.io/mlxtend/user_guide/utils/Counter/

Methods


update()

Print current iteration and time elapsed.