Advertisement
here2share

# track_last_200.py

Sep 8th, 2023
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. # track_last_200.py
  2.  
  3. import re
  4. import sys
  5. from functools import wraps
  6. import inspect
  7.  
  8. last_200 = [''] * 200
  9. prev_line = dict([(k, None) for k in variables])
  10. def track_all_changes(variables_to_track):
  11.     def slide(var, line):
  12.         if prev_line[var] != line:
  13.             last_200.append(line)
  14.             last_200.pop(0)
  15.             prev_line[var] = line
  16.     def track_changes(func):
  17.         @wraps(func)
  18.         def wrapper(*args, **kwargs):
  19.             tracked = []
  20.             frame = inspect.currentframe()
  21.             try:
  22.                 result = func(*args, **kwargs)
  23.             except:
  24.                 for line in last_200:
  25.                     print(line)
  26.                 raise
  27.             for k, v in frame.f_back.f_locals.items():
  28.                 if k in variables_to_track:
  29.                     slide(k, f"Local {k}: {v}")
  30.             for k, v in frame.f_back.f_globals.items():
  31.                 if k in variables_to_track:
  32.                     slide(k, f"{func.__name__}() Global {k}: {v}")
  33.             if tracked not in last_200:
  34.                 return result
  35.  
  36.         return wrapper
  37.     module = sys.modules[__name__]
  38.     for name, obj in inspect.getmembers(module):
  39.         if re.sub(r'__[^>]*__', '', name):
  40.             if inspect.isfunction(obj):
  41.                 setattr(module, name, track_changes(obj))
  42.  
  43. track_all_changes(variables)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement