Introduction¶
The trace
function¶
The hunter.trace
function can take 2 types of arguments:
- Keyword arguments like
module
,function
oraction
. This is for convenience. - Callbacks that take an
event
argument:- Builtin predicates like:
hunter.Query
,hunter.When
,hunter.And
orhunter.Or
. - Actions like:
hunter.CodePrinter
,hunter.Debugger
orhunter.VarsPrinter
- Builtin predicates like:
Note that hunter.trace
will use hunter.Q
when you pass multiple positional arguments or keyword arguments.
The Q
function¶
The hunter.Q
function provides a convenience API for you:
Q(module='foobar')
is converted toQuery(module='foobar')
.Q(module='foobar', action=Debugger)
is converted toWhen(Query(module='foobar'), Debugger)
.Q(module='foobar', actions=[CodePrinter, VarsPrinter('name')])
is converted toWhen(Query(module='foobar'), CodePrinter, VarsPrinter('name'))
.Q(Q(module='foo'), Q(module='bar'))
is converted toOr(Q(module='foo'), Q(module='bar'))
.Q(your_own_callback, module='foo')
is converted toOr(your_own_callback, Q(module='foo'))
.
The builtin predicates and actions¶
All the builtin predicates (hunter.Query
, hunter.When
, hunter.And
and hunter.Or
) support the |
and &
operators:
Query(module='foo') | Query(module='bar')
is converted toOr(Query(module='foo'), Query(module='bar'))
Query(module='foo') & Query(module='bar')
is converted toAnd(Query(module='foo'), Query(module='bar'))
Activation¶
You can activate Hunter in two ways.
via code¶
import hunter
hunter.trace(
...
)
via environment variable¶
Set the PYTHONHUNTER
environment variable. Eg:
PYTHONHUNTER="module='os.path'" python yourapp.py
On Windows you’d do something like:
set PYTHONHUNTER=module='os.path'
python yourapp.py
The activation works with a clever .pth
file that checks for that env var presence and before your app runs does something like this:
from hunter import *
trace(
<whatever-you-had-in-the-PYTHONHUNTER-env-var>
)
That also means that it will do activation even if the env var is empty, eg: PYTHONHUNTER=""
.