Advertisement
here2share

# powerful_vars_tracking.py

Sep 6th, 2023 (edited)
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. # powerful_vars_tracking.py
  2.  
  3. import re
  4. import sys
  5. from functools import wraps
  6. import inspect
  7. def track_all_changes(variables_to_track):
  8.     def track_changes(func):
  9.         @wraps(func)
  10.         def wrapper(*args, **kwargs):
  11.             frame = inspect.currentframe()
  12.             result = func(*args, **kwargs)
  13.             for k, v in frame.f_back.f_locals.items():
  14.                 if k in variables_to_track:
  15.                     print(f"Local {k}: {v}")
  16.             for k, v in frame.f_back.f_globals.items():
  17.                 if k in variables_to_track:
  18.                     print(f"{func.__name__}() Global {k}: {v}")
  19.             return result
  20.         return wrapper
  21.     module = sys.modules[__name__]
  22.     for name, obj in inspect.getmembers(module):
  23.         if re.sub(r'__[^>]*__', '', name):
  24.             if inspect.isfunction(obj):
  25.                 setattr(module, name, track_changes(obj))
  26.  
  27. track_all_changes(variables)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement