Common: Add queue_to_list()

This commit is contained in:
Mike Salvatore 2022-05-10 13:23:50 -04:00
parent 2804ba9b07
commit 487d1c55af
2 changed files with 37 additions and 0 deletions

View File

@ -1,3 +1,7 @@
import queue
from typing import Any, List
class abstractstatic(staticmethod):
__slots__ = ()
@ -15,3 +19,14 @@ class Singleton(type):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
def queue_to_list(q: queue.Queue) -> List[Any]:
list_ = []
try:
while True:
list_.append(q.get_nowait())
except queue.Empty:
pass
return list_

View File

@ -0,0 +1,22 @@
from queue import Queue
from common.utils.code_utils import queue_to_list
def test_empty_queue_to_empty_list():
q = Queue()
list_ = queue_to_list(q)
assert len(list_) == 0
def test_queue_to_list():
expected_list = [8, 6, 7, 5, 3, 0, 9]
q = Queue()
for i in expected_list:
q.put(i)
list_ = queue_to_list(q)
assert list_ == expected_list