tracecalls#

class tracecalls(trace='<default>', exclude='<default>', regex=False, repeats=False, custom=None, verbose=None)[source]#

Bases: prettyobj

Trace all function calls.

Alias to sys.steprofile().

Parameters:
  • trace (str/list/regex) – the module(s)/file(s) to trace calls from (’’ matches all, but this is usually undesirable)

  • exclude (str/list/regex) – a list of modules/files to exclude (default excludes builtins; set to None to not exclude anything)

  • regex (bool) – whether to interpret trace and exclude as regexes rather than simple string matching

  • repeats (bool) – whether to record repeat calls of the same function (default False)

  • custom (func) – if provided, use this rather than the built in logic for checking for matches

  • verbose (bool) – how much information to print (False=silent, None=default, True=debug)

Examples:

import mymodule as mm

# In context block
with sc.tracecalls('mymodule'):
    mm.big_operation()

# Explicitly
tc = sc.tracecalls('*mysubmodule*', exclude='^init*', regex=True, repeats=True)
tc.start()
mm.big_operation()
tc.stop()
tc.df.disp()
New in version 3.2.0.
New in version 3.2.1: “custom” argument added; “kwargs” removed

Methods

start()[source]#

Start profiling

stop(disp=None)[source]#

Stop profiling

disp(maxlen=60)[source]#

Display the results

to_df()[source]#

Convert to a dataframe; if repeats=True, also count repeats

check_expected(expected, die=False)[source]#

Check function calls against a list of expected function calls.

Parameters:
  • expected (set/list/any) – if a list of set of strings, check those function names; if object(s) or classes are supplied, check each method

  • die (bool) – raise an exception if any expected function calls were not called

Example:

# Check which methods of a class are called
with sc.tracecalls() as tc:
    my_obj = MyObj()
    my_obj.run()

expected = tc.check_expected(MyObj) # Equivalent to tc.check_expected(my_obj)
print(expected)