Expressive array utilities with an optional C backend.
Project description
smartarr
smartarr is an expressive array utility layer for Python with a clean smart(arr) API and an optional C backend for fast core operations.
Why it exists
The goal is simple:
- one readable interface for lists, tuples, and NumPy arrays
- fast common operations when the C extension is available
- chainable transformations that still feel like Python
from smartarr import smart
value = smart([1, 2, 3, 4, 5]).rotate(2).reverse().middle()
print(value) # 1
Install
Local install from this project:
python -m pip install .
Editable install for development:
python -m pip install -e .
Optional NumPy support:
python -m pip install ".[numpy]"
If a compiler is available, smartarr builds its C extension automatically. If not, installation still succeeds and falls back to the pure Python backend.
Quick start
from smartarr import smart
arr = smart([1, 2, 3, 4])
print(arr.first()) # 1
print(arr.last()) # 4
print(arr.middle()) # 3
print(arr.middle(mode="left")) # 2
print(arr.middle(mode="avg")) # 2.5
print(arr.size()) # 4
print(len(arr)) # 4
print(arr.rotate(1).to_list()) # [4, 1, 2, 3]
print(arr.reverse().to_list()) # [4, 3, 2, 1]
print(arr.chunk(2).to_list()) # [[1, 2], [3, 4]]
print(arr.window(2).to_list()) # [[1, 2], [2, 3], [3, 4]]
Design notes
middle()defaults to the right-middle element for even-length arrays, matchingarr[len(arr) // 2].SmartArrayoperations are non-mutating by default. Methods likerotate()andreverse()return a new wrapper and leave the original array unchanged.- Prefer
len(arr)orarr.size()for length checks.arr.len()remains as a compatibility alias. - Structural methods return a new
SmartArray, so chaining works naturally. - Terminal methods return a final value, such as an element, a number, or a dictionary.
- Flat transformations preserve the original adapter where practical. Shape-changing operations return list-shaped data by default.
random()returns one element whennis omitted, samples without replacement whenreplace=False, and samples with replacement whenreplace=True. Aseedmakes output deterministic.flatten()performs a deep recursive flatten.reshape()validates that the requested shape exactly matches the flattened element count.reduce()requires either a non-empty array or an explicitinitialvalue.
Supported input types
listtuple- NumPy
ndarraywhen NumPy is installed - general iterables such as
range
Current feature coverage
V1
firstlastmiddlerotatereverselenis_empty
V2
chunkwindowrandomfindcountcontainsis_sortedsorted
V3
mapfilterreducesummeanmedianminmaxpeaksuniqueduplicatesfrequencyflattenreshape
Development
Run tests:
python -m unittest discover -s tests -v
License
MIT
Project details
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 smartarr-0.1.1.tar.gz.
File metadata
- Download URL: smartarr-0.1.1.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
046b6a3cdd449de6054dbcd3e0576f728e28bce34ae8aed00fe7f716cc3a9de8
|
|
| MD5 |
e9ff8a227bbf374f12d50052a0c65e0b
|
|
| BLAKE2b-256 |
3fc776a6eceb1fb56548ba611372751a65ace6530021e1c62a9bed884c241105
|
File details
Details for the file smartarr-0.1.1-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: smartarr-0.1.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 18.7 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ee994a59d8029c8d773a1d1ceb869aabcad6431ad3a2c2764fe23c3da62b8ca
|
|
| MD5 |
ab44a8484a70ed6f995c85276cdd2bba
|
|
| BLAKE2b-256 |
696e86b8b6060ecbc58873c2aad3219293bf1762c78ec179b4124a7d2c15b0d8
|