Package entropy :: Module misc :: Class Lifo

Class Lifo

source code


This class can be used to build LIFO buffers, also commonly known as "stacks". Lifo allows you to store and retrieve Python objects from its stack, in a very smart way. This implementation is much faster than the one provided by Python (queue module) and more sofisticated.

Sample code:

>>> # load Lifo
>>> from entropy.misc import Lifo
>>> stack = Lifo()
>>> item1 = set([1,2,3])
>>> item2 = ["a","b", "c"]
>>> item3 = None
>>> item4 = 1
>>> stack.push(item4)
>>> stack.push(item3)
>>> stack.push(item2)
>>> stack.push(item1)
>>> stack.is_filled()
True
# discarding all the item matching int(1) in the stack
>>> stack.discard(1)
>>> item3 is stack.pop()
True
>>> item2 is stack.pop()
True
>>> item1 is stack.pop()
True
>>> stack.pop()
ValueError exception (stack is empty)
>>> stack.is_filled()
False
>>> del stack
Instance Methods
 
__init__(self)
Lifo class constructor
source code
 
__nonzero__(self)
Return if stack is empty.
source code
 
__len__(self)
Return stack size.
source code
None
push(self, item)
Push an object into the stack.
source code
None
insert(self, item)
Insert item at the bottom of the stack.
source code
None
clear(self)
Clear the stack.
source code
bool
is_filled(self)
Tell whether Lifo contains data that can be popped out.
source code
None
discard(self, entry)
Remove given object from stack.
source code
any Python object
pop(self)
Pop the uppermost item of the stack out of it.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

source code 

Lifo class constructor

Overrides: object.__init__

push(self, item)

source code 

Push an object into the stack.

Parameters:
  • item (Python object) - any Python object
Returns: None
None

insert(self, item)

source code 

Insert item at the bottom of the stack.

Parameters:
  • item (Python object) - any Python object
Returns: None
None

clear(self)

source code 

Clear the stack.

Returns: None
None

is_filled(self)

source code 

Tell whether Lifo contains data that can be popped out.

Returns: bool
fill status

discard(self, entry)

source code 

Remove given object from stack. Any matching object, through identity and == comparison will be removed.

Parameters:
  • entry (any Python object) - object in stack
Returns: None
None

pop(self)

source code 

Pop the uppermost item of the stack out of it.

Returns: any Python object
object stored in the stack
Raises:
  • ValueError - if stack is empty