From c4970a016fffc0dd85a03342e78c4fa0de60e8c3 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Mon, 4 Jan 2021 20:16:42 +0545 Subject: [PATCH] debug functions: have a good default value (#5202) I had to keep providing default value before when profiling simple functions as an API. I added a default now, and also made `debugtools` customizable so that you could edit the code to, for example, have `pdb` enabled by default. --- dvc/_debug.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/dvc/_debug.py b/dvc/_debug.py index e4c029e9a..43624002b 100644 --- a/dvc/_debug.py +++ b/dvc/_debug.py @@ -3,7 +3,7 @@ from contextlib import ExitStack, contextmanager @contextmanager -def instrument(html_output): +def instrument(html_output=False): """Run a statistical profiler""" try: from pyinstrument import Profiler # pylint: disable=import-error @@ -25,7 +25,7 @@ def instrument(html_output): @contextmanager -def profile(dump): +def profile(dump_path: str = None): """Run a cprofile""" import cProfile @@ -35,10 +35,10 @@ def profile(dump): yield prof.disable() - if not dump: + if not dump_path: prof.print_stats(sort="cumtime") return - prof.dump_stats(dump) + prof.dump_stats(dump_path) @contextmanager @@ -56,14 +56,17 @@ def debug(): @contextmanager -def debugtools(args): +def debugtools(args: "argparse.Namespace" = None, **kwargs): + kw = vars(args) if args else {} + kw.update(kwargs) + with ExitStack() as stack: - if args.pdb: + if kw.get("pdb"): stack.enter_context(debug()) - if args.cprofile: - stack.enter_context(profile(args.cprofile_dump)) - if args.instrument: - stack.enter_context(instrument(args.instrument_open)) + if kw.get("cprofile") or kw.get("cprofile_dump"): + stack.enter_context(profile(kw.get("cprofile_dump"))) + if kw.get("instrument") or kw.get("instrument_open"): + stack.enter_context(instrument(kw.get("instrument_open", False))) yield