import json
import inspect
from queue import Queue
def serialize_queue(queue, dry_run=False):
"""Serializes a queue object for diagnostics.
Args:
queue: The queue object to serialize.
dry_run: If True, only prints the code that would be executed.
If False, actually serializes and returns the string.
Returns:
A string representation of the queue object, or None if dry_run is True.
"""
if dry_run:
print("Dry run mode: Printing code to be executed.")
code = f"""
queue_data = {{
'q': {repr(queue.queue)},
'qsize': {repr(queue.qsize)},
'is_empty': {repr(queue.empty())},
'maxsize': {repr(queue.maxsize)}
}}
return json.dumps(queue_data)
"""
return None
else:
queue_data = {
'q': queue.queue,
'qsize': queue.qsize,
'is_empty': queue.empty(),
'maxsize': queue.maxsize
}
return json.dumps(queue_data, indent=4) # indent for readability
if __name__ == '__main__':
# Example Usage
q = Queue()
q.put(1)
q.put(2)
q.put(3)
# Serialize with dry-run
serialize_queue(q, dry_run=True)
# Serialize without dry-run
serialized_string = serialize_queue(q)
print(serialized_string)
Add your comment