Module osbot_utils.helpers.Python_Audit
Expand source code
import sys
from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
from osbot_utils.helpers.Print_Table import Print_Table
from osbot_utils.utils.Call_Stack import Frame_Data, Call_Stack
class Python_Audit(Kwargs_To_Self):
audit_events : list
frame_depth : int = 10
def hook_callback(self, event, args):
if event != 'sys._getframe': # since sys._getframe will trigger an event (and cause a recursive loop) we have to ignore it
frame = sys._getframe().f_back
self.audit_events.append((event, args,frame))
def data(self):
data = []
for index, item in enumerate(self.audit_events):
(event, args, frame) = item
call_stack = Call_Stack(max_depth=self.frame_depth)
call_stack.capture_frame(frame)
data.append({'index':index, 'event': event, 'args': args, 'stack': call_stack.stats()})
return data
def start(self):
sys.addaudithook(self.hook_callback)
return self
def events(self):
return self.audit_events
def events_by_type(self):
events_by_type = {}
for event, args, stack in self.audit_events:
events_by_type[event] = events_by_type.get(event, 0) + 1
return events_by_type
def print(self):
with Print_Table() as _:
_.add_data(self.data())
_.set_order('index', 'event', 'args', 'stack')
_.print()
def size(self):
return len(self.events)
Classes
class Python_Audit (**kwargs)
-
A mixin class to strictly assign keyword arguments to pre-defined instance attributes during initialization.
This base class provides an init method that assigns values from keyword arguments to instance attributes. If an attribute with the same name as a key from the kwargs is defined in the class, it will be set to the value from kwargs. If the key does not match any predefined attribute names, an exception is raised.
This behavior enforces strict control over the attributes of instances, ensuring that only predefined attributes can be set at the time of instantiation and avoids silent attribute creation which can lead to bugs in the code.
Usage
class MyConfigurableClass(Kwargs_To_Self): attribute1 = 'default_value' attribute2 = True attribute3 : str attribute4 : list attribute4 : int = 42
# Other methods can be added here
Correctly override default values by passing keyword arguments
instance = MyConfigurableClass(attribute1='new_value', attribute2=False)
This will raise an exception as 'attribute3' is not predefined
instance = MyConfigurableClass(attribute3='invalid_attribute')
this will also assign the default value to any variable that has a type defined. In the example above the default values (mapped by default__kwargs and locals) will be: attribute1 = 'default_value' attribute2 = True attribute3 = '' # default value of str attribute4 = [] # default value of list attribute4 = 42 # defined value in the class
Note
It is important that all attributes which may be set at instantiation are predefined in the class. Failure to do so will result in an exception being raised.
Methods
init(**kwargs): The initializer that handles the assignment of keyword arguments to instance attributes. It enforces strict attribute assignment rules, only allowing attributes that are already defined in the class to be set.
Initialize an instance of the derived class, strictly assigning provided keyword arguments to corresponding instance attributes.
Parameters
**kwargs: Variable length keyword arguments.
Raises
Exception
- If a key from kwargs does not correspond to any attribute pre-defined in the class, an exception is raised to prevent setting an undefined attribute.
Expand source code
class Python_Audit(Kwargs_To_Self): audit_events : list frame_depth : int = 10 def hook_callback(self, event, args): if event != 'sys._getframe': # since sys._getframe will trigger an event (and cause a recursive loop) we have to ignore it frame = sys._getframe().f_back self.audit_events.append((event, args,frame)) def data(self): data = [] for index, item in enumerate(self.audit_events): (event, args, frame) = item call_stack = Call_Stack(max_depth=self.frame_depth) call_stack.capture_frame(frame) data.append({'index':index, 'event': event, 'args': args, 'stack': call_stack.stats()}) return data def start(self): sys.addaudithook(self.hook_callback) return self def events(self): return self.audit_events def events_by_type(self): events_by_type = {} for event, args, stack in self.audit_events: events_by_type[event] = events_by_type.get(event, 0) + 1 return events_by_type def print(self): with Print_Table() as _: _.add_data(self.data()) _.set_order('index', 'event', 'args', 'stack') _.print() def size(self): return len(self.events)
Ancestors
Class variables
var audit_events : list
var frame_depth : int
Methods
def data(self)
-
Expand source code
def data(self): data = [] for index, item in enumerate(self.audit_events): (event, args, frame) = item call_stack = Call_Stack(max_depth=self.frame_depth) call_stack.capture_frame(frame) data.append({'index':index, 'event': event, 'args': args, 'stack': call_stack.stats()}) return data
def events(self)
-
Expand source code
def events(self): return self.audit_events
def events_by_type(self)
-
Expand source code
def events_by_type(self): events_by_type = {} for event, args, stack in self.audit_events: events_by_type[event] = events_by_type.get(event, 0) + 1 return events_by_type
def hook_callback(self, event, args)
-
Expand source code
def hook_callback(self, event, args): if event != 'sys._getframe': # since sys._getframe will trigger an event (and cause a recursive loop) we have to ignore it frame = sys._getframe().f_back self.audit_events.append((event, args,frame))
def print(self)
-
Expand source code
def print(self): with Print_Table() as _: _.add_data(self.data()) _.set_order('index', 'event', 'args', 'stack') _.print()
def size(self)
-
Expand source code
def size(self): return len(self.events)
def start(self)
-
Expand source code
def start(self): sys.addaudithook(self.hook_callback) return self
Inherited members