Module osbot_utils.helpers.trace.Trace_Call__Config

Expand source code
from osbot_utils.utils.Dev import pprint

from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self

PRINT_MAX_STRING_LENGTH    = 100
PRINT_PADDING__DURATION    = 100
PRINT_PADDING_PARENT_INFO  = 60

class Trace_Call__Config(Kwargs_To_Self):
    title                      : str
    capture_locals             : bool = False
    capture_duration           : bool
    capture_extra_data         : bool
    capture_frame              : bool = True
    capture_frame_stats        : bool
    deep_copy_locals           : bool
    trace_capture_lines        : bool
    ignore_start_with          : list
    print_padding_duration     : int = PRINT_PADDING__DURATION
    print_padding_parent_info  : int = PRINT_PADDING_PARENT_INFO
    print_duration             : bool
    print_max_string_length    : int  = PRINT_MAX_STRING_LENGTH
    print_locals               : bool
    print_traces_on_exit       : bool
    print_lines_on_exit        : bool
    show_parent_info           : bool = False
    show_caller                : bool
    show_method_class          : bool = True
    show_source_code_path      : bool
    trace_capture_all          : bool
    trace_capture_source_code  : bool
    trace_capture_start_with   : list
    trace_capture_contains     : list
    trace_enabled              : bool = True
    trace_ignore_start_with    : list
    trace_show_internals       : bool
    trace_up_to_depth          : int
    with_duration_bigger_than  : float

    def __init__(self, **wargs):
        super().__init__(**wargs)
        #self.locked()

    def all(self, up_to_depth=0, print_traces=True):
        self.trace_capture_all    = True
        self.print_traces_on_exit = print_traces
        self.trace_up_to_depth    = up_to_depth
        return self

    def capture(self, starts_with=None, contains=None, ignore=None):
        if starts_with:
            if type(starts_with) is str:
                starts_with = [starts_with]
            self.trace_capture_start_with = starts_with
        if contains:
            if type(contains) is str:
                contains = [contains]
            self.trace_capture_contains = contains
        if ignore:
            if type(ignore) is str:
                ignore = [ignore]
            self.ignore_start_with = ignore
        self.print_traces_on_exit = True
        return self

    def duration(self, bigger_than=0, padding=PRINT_PADDING__DURATION):
        self.capture_duration          = True
        self.print_duration            = True
        self.print_padding_duration          = padding
        self.with_duration_bigger_than = bigger_than
        return self

    def locals(self):
        self.capture_locals = True
        self.print_locals   = True
        return self

    def lines(self, print_traces=True, print_lines=True):
        self.trace_capture_lines  = True
        self.print_traces_on_exit = print_traces
        self.print_lines_on_exit  = print_lines
        return self

    def print_config(self):
        pprint(self.__locals__())
        return self

    def print_on_exit(self, value=True):
        self.print_traces_on_exit = value
        return self

    def up_to_depth(self, depth):
        self.trace_up_to_depth = depth
        return self

Classes

class Trace_Call__Config (**wargs)

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 Trace_Call__Config(Kwargs_To_Self):
    title                      : str
    capture_locals             : bool = False
    capture_duration           : bool
    capture_extra_data         : bool
    capture_frame              : bool = True
    capture_frame_stats        : bool
    deep_copy_locals           : bool
    trace_capture_lines        : bool
    ignore_start_with          : list
    print_padding_duration     : int = PRINT_PADDING__DURATION
    print_padding_parent_info  : int = PRINT_PADDING_PARENT_INFO
    print_duration             : bool
    print_max_string_length    : int  = PRINT_MAX_STRING_LENGTH
    print_locals               : bool
    print_traces_on_exit       : bool
    print_lines_on_exit        : bool
    show_parent_info           : bool = False
    show_caller                : bool
    show_method_class          : bool = True
    show_source_code_path      : bool
    trace_capture_all          : bool
    trace_capture_source_code  : bool
    trace_capture_start_with   : list
    trace_capture_contains     : list
    trace_enabled              : bool = True
    trace_ignore_start_with    : list
    trace_show_internals       : bool
    trace_up_to_depth          : int
    with_duration_bigger_than  : float

    def __init__(self, **wargs):
        super().__init__(**wargs)
        #self.locked()

    def all(self, up_to_depth=0, print_traces=True):
        self.trace_capture_all    = True
        self.print_traces_on_exit = print_traces
        self.trace_up_to_depth    = up_to_depth
        return self

    def capture(self, starts_with=None, contains=None, ignore=None):
        if starts_with:
            if type(starts_with) is str:
                starts_with = [starts_with]
            self.trace_capture_start_with = starts_with
        if contains:
            if type(contains) is str:
                contains = [contains]
            self.trace_capture_contains = contains
        if ignore:
            if type(ignore) is str:
                ignore = [ignore]
            self.ignore_start_with = ignore
        self.print_traces_on_exit = True
        return self

    def duration(self, bigger_than=0, padding=PRINT_PADDING__DURATION):
        self.capture_duration          = True
        self.print_duration            = True
        self.print_padding_duration          = padding
        self.with_duration_bigger_than = bigger_than
        return self

    def locals(self):
        self.capture_locals = True
        self.print_locals   = True
        return self

    def lines(self, print_traces=True, print_lines=True):
        self.trace_capture_lines  = True
        self.print_traces_on_exit = print_traces
        self.print_lines_on_exit  = print_lines
        return self

    def print_config(self):
        pprint(self.__locals__())
        return self

    def print_on_exit(self, value=True):
        self.print_traces_on_exit = value
        return self

    def up_to_depth(self, depth):
        self.trace_up_to_depth = depth
        return self

Ancestors

Class variables

var capture_duration : bool
var capture_extra_data : bool
var capture_frame : bool
var capture_frame_stats : bool
var capture_locals : bool
var deep_copy_locals : bool
var ignore_start_with : list
var print_duration : bool
var print_lines_on_exit : bool
var print_locals : bool
var print_max_string_length : int
var print_padding_duration : int
var print_padding_parent_info : int
var print_traces_on_exit : bool
var show_caller : bool
var show_method_class : bool
var show_parent_info : bool
var show_source_code_path : bool
var title : str
var trace_capture_all : bool
var trace_capture_contains : list
var trace_capture_lines : bool
var trace_capture_source_code : bool
var trace_capture_start_with : list
var trace_enabled : bool
var trace_ignore_start_with : list
var trace_show_internals : bool
var trace_up_to_depth : int
var with_duration_bigger_than : float

Methods

def all(self, up_to_depth=0, print_traces=True)
Expand source code
def all(self, up_to_depth=0, print_traces=True):
    self.trace_capture_all    = True
    self.print_traces_on_exit = print_traces
    self.trace_up_to_depth    = up_to_depth
    return self
def capture(self, starts_with=None, contains=None, ignore=None)
Expand source code
def capture(self, starts_with=None, contains=None, ignore=None):
    if starts_with:
        if type(starts_with) is str:
            starts_with = [starts_with]
        self.trace_capture_start_with = starts_with
    if contains:
        if type(contains) is str:
            contains = [contains]
        self.trace_capture_contains = contains
    if ignore:
        if type(ignore) is str:
            ignore = [ignore]
        self.ignore_start_with = ignore
    self.print_traces_on_exit = True
    return self
def duration(self, bigger_than=0, padding=100)
Expand source code
def duration(self, bigger_than=0, padding=PRINT_PADDING__DURATION):
    self.capture_duration          = True
    self.print_duration            = True
    self.print_padding_duration          = padding
    self.with_duration_bigger_than = bigger_than
    return self
def lines(self, print_traces=True, print_lines=True)
Expand source code
def lines(self, print_traces=True, print_lines=True):
    self.trace_capture_lines  = True
    self.print_traces_on_exit = print_traces
    self.print_lines_on_exit  = print_lines
    return self
def locals(self)
Expand source code
def locals(self):
    self.capture_locals = True
    self.print_locals   = True
    return self
def print_config(self)
Expand source code
def print_config(self):
    pprint(self.__locals__())
    return self
def print_on_exit(self, value=True)
Expand source code
def print_on_exit(self, value=True):
    self.print_traces_on_exit = value
    return self
def up_to_depth(self, depth)
Expand source code
def up_to_depth(self, depth):
    self.trace_up_to_depth = depth
    return self

Inherited members