profile_code#

scikitplot._ipymagic.profile_code(code, namespace, local_namespace=None, *, sort='cumulative', limit=25, stats_file=None)[source]#

Run code under cProfile and summarise the profile.

Parameters:
codecode object or str

What to run (exec).

namespacemapping

Global namespace (the notebook’s).

local_namespacemapping or None, default=None

Local namespace; namespace when None.

sort{“cumulative”, “tottime”, “ncalls”}, default=”cumulative”

Sort key of the table.

limitint, default=25

Rows of the table.

stats_filestr or None, default=None

Write the pstats data there (readable by pstats, snakeviz, python -m pstats).

Returns:
ProfileResult

The profile (also of a run that raised, up to the error).

BaseException or None

What the code raised, for the caller to re-raise after showing the profile; else an IpyMagicError (code "stats_file") when the stats could not be written; None when all went well.

Raises:
ProfilerBusyError

When another profiler is active (nothing is run).

MagicUsageError

For a bad sort, limit or stats_file (nothing is run).

Parameters:
Return type:

tuple[ProfileResult, BaseException | None]

Examples

>>> from scikitplot._ipymagic import profile_code
>>> result, error = profile_code("sum(range(10))", {})
>>> error is None and result.total_calls >= 1
True