Austin W. Austin W. - 3 months ago 8
Python Question

How to count the number of calls to a method?

I'm working with the pi2go lite robot. This is my code

import pi2go, time
import sys
import tty
import termios
import time
pi2go.init()

def stepCount():
countL += 0
countR += 0


speed = 60
try:
pi2go.stepForward(60,16)
print stepCount

finally:
pi2go.cleanup()


The question is I am wondering how to count everytime the "pi2go.stepForward(60,16)" is used.

Answer
counter = dict(ok=0, fail=0, all=0)
try:
    pi2go.stepForward(60,16)
    counter['ok'] += 1
except:
    counter['fail'] += 1
finally:
    counter['all'] += 1
    pi2go.cleanup()