A chainable stack-based utility library
Reason this release was yanked:
no changes
Project description
StackLib
A chainable, stack-based utility library for Python, inspired by stack-based languages like Forth and PostScript.
StackLib focuses on:
- method chaining
- expressive stack manipulation
- functional-style helpers
- simplicity over strict error handling
Installation
pip install stacklib
Basic Usage
from stacklib.Stack import stack
stack = stack()
stack.push(10).push(5).add()
print(f'Stack: {stack}')
Stack Model
- The top of the stack is index
0 - Most operations modify the stack in-place
- Most stack operations are chainable
- Some operations (comparisons / logic) return values instead of the stack
Features
- Chainable stack operations
- Arithmetic operations
- Stack manipulation helpers (
dup,swap,rot,over) - Functional helpers (
map,filter,map_if,apply_at) - Logical and comparison operators
- Snapshot / restore system
Creating a Stack
from stacklib.Stack import stack
stack = stack()
This creates a new, empty stack instance.
Core Stack Operations
push(item)
Adds an item to the top of the stack.
print(f'Stack before: {stack}')
stack.push(5)
print(f'Stack after: {stack}')
Output:
Stack before: []
Stack after: [5]
drop(num=1)
Removes items from the top of the stack.
stack.push(5).push(10)
print(f'Stack before: {stack}')
stack.drop()
print(f'Stack after: {stack}')
Output:
Stack before: [10, 5]
Stack after: [5]
dup()
Duplicates the top item.
stack.push(1)
print(f'Stack before: {stack}')
stack.dup()
print(f'Stack after: {stack}')
Output:
Stack before: [1]
Stack after: [1, 1]
swap()
Swaps the top two items.
stack.push(1).push(2)
print(f'Stack before: {stack}')
stack.swap()
print(f'Stack after: {stack}')
Output:
Stack before: [2, 1]
Stack after: [1, 2]
rot()
Rotates the top three items.
stack.push("Im still").push(1).push(2).push(3)
print(f'Stack before: {stack}')
stack.rot()
print(f'Stack after: {stack}')
Output:
Stack before: [3, 2, 1, "Im still"]
Stack after: [2, 1, 3, "Im still"]
over()
Copies the second item to the top.
stack.push(1).push(2)
print(f'Stack before: {stack}')
stack.over()
print(f'Stack after: {stack}')
Output:
Stack before: [2, 1]
Stack after: [1, 2, 1]
Stack Cycling
cycle()
Moves the last item to the top.
stack.push(1).push(2).push(3)
print(f'Stack before: {stack}')
stack.cycle()
print(f'Stack after: {stack}')
Output:
Stack before: [3, 2, 1]
Stack after: [1, 3, 2]
rcycle()
Moves the top item to the bottom.
stack.push(1).push(2).push(3)
print(f'Stack before: {stack}')
stack.rcycle()
print(f'Stack after: {stack}')
Output:
Stack before: [3, 2, 1]
Stack after: [2, 1, 3]
Arithmetic Operations
These operate on:
- the top two stack items or
- the top item and a provided argument
All are chainable
add(num=None)
sub(num=None)
mul(num=None)
div(num=None)
mod(num=None)
pow(exponent=None)
neg()
absolute()
Example:
stack.push(10).push(5).add()
print(f'Stack: {stack}')
Output:
Stack: [15]
Packing & Unpacking
pack(num)
Groups items into a list
if given an argument (num) it will group that ammount of items starting from the top of the stack
num defaults to 2
stack.push(1).push(2).push(3).push(4)
print(f'Stack before: {stack}')
stack.pack(3)
print(f'Stack after: {stack}')
Output:
Stack before: [4, 3, 2, 1]
Stack after: [[4, 3, 2], 1]
unpack()
Unpacks a list back into stack items
stack.unpack()
print(f'Stack after: {stack}')
Output: [3, 2, 1]
Stack after: [3, 2, 1, 3, 2, 1]
Queries
is_empty()
Returns True if the stack is empty
depth()
Returns the number of items
peek(index)
Returns the value at a specific index
index defaults to 0
last()
Returns the last item in the stack
Comparison & Logic
These return boolean values (not the stack)
greater(num)
less(num)
equal(item)
stack.push(10).push(5)
stack.push(10).push(5)
print(f'Stack before: {stack}')
print(stack.greater())
print(f'Stack after: {stack}')
Output:
Stack before: [5, 10, 5, 10]
False
Stack after: [5, 10, 5, 10]
Logical Operations
sand(item) → logical AND
sor(item) → logical OR
(if no argument is given on sand / sor, they uses both top items on the stack)
snot() → logical NOT
stack.push(True)
stack.push(True)
print(f'Stack before: {stack}')
stack.snot()
print(f'Stack after: {stack}')
Output:
Stack before: [True, True]
False
Stack after: [False, True]
Functional Helpers
map(func)
Applies a function to all items.
stack.push(1).push(2).push(3)
print(f'Stack before: {stack}')
stack.map(lambda x: x * 2)
print(f'Stack after: {stack}')
Output:
Stack before: [3, 2, 1]
Stack after: [6, 4, 2]
filter(func)
Keeps only items where func(item) is True
apply_at(func, index)
Applies a function to a specific index
index defaults to 0
map_if(func, filter_func)
Applies func only to items that match filter_func
Snapshots
snapshot()
Saves the current stack state.
restore()
Restores the most recent snapshot.
stack.push(1).snapshot()
stack.push(99)
print(f'Stack before: {stack}')
stack.restore()
print(f'Stack after: {stack}')
Output:
Before: [99, 1]
After: [1]
Why This Exists
This library exists because:
- stack-based programming is fun
- it’s a good mental model for certain problems
- and it was a fun idea for a project
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stacklib-0.1.2.tar.gz.
File metadata
- Download URL: stacklib-0.1.2.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7208b6d56ad03820d4ffdfe8553c5cb7fc98c2324404ed1dbb63d5500d9ced79
|
|
| MD5 |
9143dba76892e83f3e1f61566400d90d
|
|
| BLAKE2b-256 |
db64b0fc8524c832d7e241f99b095d778b200fbbd070d19a6f77bbe7766bb69f
|
File details
Details for the file stacklib-0.1.2-py3-none-any.whl.
File metadata
- Download URL: stacklib-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fe9b0496e68c5e7eb1461d94c6014af4bc53a4d82bf46d0be29cd290d1138d6
|
|
| MD5 |
a6609e3f9f3d7ac984de884f3b47f3e4
|
|
| BLAKE2b-256 |
93764f946beb2be943d11d14cfb4fed8f2302b7c9fd3102ac855ca996570bb80
|