Skip to content

Instantly share code, notes, and snippets.

@shuantsu
Last active June 22, 2023 18:34
Show Gist options
  • Save shuantsu/763a5e4f949529620305376032df3e67 to your computer and use it in GitHub Desktop.
Save shuantsu/763a5e4f949529620305376032df3e67 to your computer and use it in GitHub Desktop.
Debugger context manager in python

This is a simple class I created to debug code in python

You just import the Debugger class and instantiate it on a "with" block.

If you want to disable the debugger simply put debug(False) at the beginning of your code.

TIP: You can use pdb first to see where your script is taking most of the execution time and then put that code inside of a Debugger block.

You can call it like this on the terminal:

time ./app.py

This will be the output:

--BEGIN--
------------------------------------------------------------  
Thu Jun 22 15:31:25 2023 - <downloading data from the servers>
------------------------------------------------------------  


------------------------------------------------------------   
Thu Jun 22 15:31:28 2023 - </downloading data from the servers>
Elapsed time: 3.00
------------------------------------------------------------   
--END--


--BEGIN--
------------------------------------------------------------   
Thu Jun 22 15:31:28 2023 - <making costly math with data>      
------------------------------------------------------------   


------------------------------------------------------------  
Thu Jun 22 15:31:30 2023 - </making costly math with data>    
Elapsed time: 2.00
------------------------------------------------------------  
--END--


--BEGIN--
------------------------------------------------------------  
Thu Jun 22 15:31:30 2023 - <uploading results to the database>
------------------------------------------------------------  


------------------------------------------------------------
Thu Jun 22 15:31:35 2023 - </uploading results to the database>
Elapsed time: 5.01
------------------------------------------------------------
--END--

./app.py  0.02s user 0.03s system 0% cpu 10.105 total
#!/usr/bin/env python3
from Debugger import *
import time, random
#debug(False)
with Debugger("downloading data from the servers"):
time.sleep(random.randint(0,5))
with Debugger("making costly math with data"):
time.sleep(random.randint(0,5))
with Debugger("uploading results to the database"):
time.sleep(random.randint(0,5))
#!/usr/bin/env python3
import time
class Debugger:
debug = True
def __init__(self, message):
self.message = message
self.t1 = time.time()
def __enter__(self):
if (self.debug):
print()
print('--BEGIN--')
print('-'*60)
print(f'{time.strftime("%c")} - <{self.message}>')
print('-'*60)
print()
def __exit__(self, exc_type, exc_value, traceback):
if (self.debug):
print()
print('-'*60)
print(f'{time.strftime("%c")} - </{self.message}>')
print('Elapsed time: %0.2f' % (time.time() - self.t1))
print('-'*60)
print('--END--')
print()
def debug(status=True):
Debugger.debug = status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment