stack_demo_01.py

#!/usr/bin/python3
# ===================================================================
# demonstrate a stack using queue module
#
# From: ww.geekforgeeks.org/stack-in-python/
#
# Note: This class is not thread safe
# ===================================================================
# Quote, "stack n."
# The set of things a person has to do in the future. "I haven't 
# done it yet because every time I pop my stack something new 
# gets pushed." If you are interrupted several times in the 
# middle of a conversation, "My stack overflowed" means 
# "I forget what we were talking about." 
#            -The Hacker's Dictionary 
#             Friedrich L. Bauer
#             German computer scientist who proposed
#             "stack method of expression evaluation"
#             in 1955
# ===================================================================

from queue import LifoQueue

e = ['a', 'b', 'c', 'd']       # test stack elements


# ---- create LIFO stack
print()
print('create LIFO stack')
stack = LifoQueue(maxsize=0)

# ---- number of elements in the queue
print()
print(f'The stack contains {stack.qsize()} elements')

# ---- push elements onto the stack
print()
for x in e:
    print(f'push {x}')
    stack.put(x)

# ---- what is the status of the stack now?
print()
print(f'stack empty: {stack.empty()}')
print(f'stack size : {stack.qsize()}')

# ---- pop elements from the stack
print()
while(not stack.empty()):
    print(f'pop {stack.get()}')

# ---- what is the status of the stack now?
print()
print(f'stack empty: {stack.empty()}')
print(f'stack size : {stack.qsize()}')

# ---- end demo
print()