logstack module
- class src.core.logstack.LogStack
Bases:
object
A class to manage a stack-based log of variable values with support for undo operations.
The LogStack class is designed to keep track of the changes made to variables over time. It stores the history of variable values along with timestamps, allowing for operations like pushing new values, popping the most recent value, peeking at previous values, pruning old entries, and calculating memory usage.
- log
A dictionary where keys are variable names and values are lists of tuples storing old values and their corresponding timestamps.
- Type:
dict
- last_pruned
The last time the log was pruned, stored as a Unix timestamp.
- Type:
float
- get_memory_usage()
Calculates the memory usage of the log stack.
This method computes the total memory usage of the log stack, including the memory used by the log dictionary, variable names, and their stored values.
- Returns:
The total memory usage of the log stack in megabytes (MB).
- Return type:
float
- peek(var_name, index=1)
Peeks at a specific previous value in the log stack for a given variable.
This method allows you to view a previous value without removing it from the stack. The index parameter specifies how far back to look (1 for the most recent, 2 for the second most recent, etc.).
- Parameters:
var_name (str) – The name of the variable to peek at.
index (int) – The position in the log stack to peek at (1 for the most recent).
- Returns:
- The value at the specified position in the log stack, or None if the
position is out of range or the variable does not exist.
- Return type:
Any
- pop(var_name)
Pops the most recent value from the log stack for a given variable.
If the variable has an entry in the log and it is not empty, the most recent value is removed and returned. If the log is empty or the variable does not exist in the log, None is returned.
- Parameters:
var_name (str) – The name of the variable whose most recent value is to be popped.
- Returns:
The most recent old value of the variable, or None if no value is available.
- Return type:
Any
- prune(retention_time=50000)
Prunes old log entries based on a specified retention time.
This method removes any entries in the log that are older than the specified retention_time. The default retention time is 50,000 seconds. Pruning is only performed if enough time has passed since the last pruning.
- Parameters:
retention_time (int, optional) – The maximum age of log entries to retain, in seconds. Entries older than this will be removed. Defaults to 50,000.
- push(var_name, old_value)
Pushes the old value of a variable onto the log stack.
If the variable does not have an existing log, a new entry is created. The old value is stored along with the current timestamp.
- Parameters:
var_name (str) – The name of the variable whose value is being logged.
old_value (Any) – The old value of the variable to be pushed onto the log.