-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SimulatedTrace()
class or trace functionality
#24
Comments
experimenting with the following class in class SimulatedTrace:
'''
Utility class for printing a trace as the
simulation model executes.
'''
def __init__(self, trace_level=0):
'''Simulated Trace
Log events as they happen.
Params:
-------
log_level: int, optional (default=0)
Minimum log level of a print statement
in order for it to be logged.
'''
self.trace_level = trace_level
def __call__(self, msg, trace_level=0):
'''Override callable.
This makes objects behave like functions.
decorates the print function. conditional
logic to print output or not.
Params:
------
msg: str
string to print to screen.
trace_level: int, optional (default=0)
minimum trace level in order for the message
to display
'''
if (trace_level >= self.trace_level):
print(msg) |
suggested modification is to include the Recommend importing |
other options
def traceable(debug=True):
def decorator(cls):
class TracedProcess(cls):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.debug = debug
def trace(self, env, msg):
if self.debug:
console.print(f'[bold blue]{env.now:.3f}: [/bold blue]: {msg}')
return TracedProcess
return decorator
class TraceableEnvironment(simpy.Environment):
def __init__(self, debug=False):
super().__init__()
self.debug = debug
def trace(self, msg):
if self.debug:
console.print(f'[bold blue]{self.now:.3f}: [/bold blue]: {msg}') |
of all options the the decorator option is neat, but imo they are ideally used when they keep the original function unchanged. |
An inheritance based option seems the best way forward. more technical to implement at a user end, but intent is clear. A draft for experimentation: from abc import ABC, abstractmethod
class Traceable(ABC):
'''Provide basic trace functionality to subclass
Abstract base class Traceable
Subclasses must call
super().__init__(debug=True) in their __init__() method to
initialise trace.
This adds
'''
def __init__(self, debug=False):
self.debug = debug
self.config = self._default_config()
self.console = Console()
def _default_config(self):
config = {
"name":None,
"name_colour":"bold blue",
"time_colour":'bold blue',
"time_dp":2,
"message_colour":'black',
"tracked":None
}
return config
def _trace_config(self):
config = {
"name":None,
"name_colour":"bold blue",
"time_colour":'bold blue',
"time_dp":2,
"message_colour":'black',
"tracked":None
}
return config
def trace(self, time, msg=None, process_id=None):
'''
Display a trace of an event
'''
if not hasattr(self, 'config'):
raise AttributeError("Your trace has not been initialised. Call super__init__(debug=True) in class initialiser"
"or omit debug for default of no trace.")
# if in debug mode
if self.debug:
# check for override to default configs
process_config = self._trace_config()
self.config.update(process_config)
# conditional logic to limit tracking to specific processes/entities
if self.config['tracked'] is None or process_id in self.config['tracked']:
# display and format time stamp
out = f"[{self.config['time_colour']}][{time:.{self.config['time_dp']}f}]:[/{self.config['time_colour']}]"
# if provided display and format a process ID
if self.config['name'] is not None and process_id is not None:
out += f"[{self.config['name_colour']}]<{self.config['name']} {process_id}>: [/{self.config['name_colour']}]"
# format traced event message
out += f"[{self.config['message_colour']}]{msg}[/{self.config['message_colour']}]"
# print to rich console
self.console.print(out)
|
Add a trace class
General idea:
print
function.log_level
e.g. -1print
method that prints message if the print log_level is >self.log_level
Example implementation:
Example one. Default
log_level
Example 2:
log_level=1
:Example 2:
log_level=2
:The text was updated successfully, but these errors were encountered: