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]]
print(arr.peaks().to_list()) # local maxima values
print(arr.peaks(mode="index").to_list()) # local maxima indexes
print(arr.duplicates().to_list()) # unique duplicated values
print(arr.duplicates(mode="all").to_list()) # every repeated occurrence after the first
print(arr.argmax()) # 3
print(arr.argsort().to_list()) # [0, 1, 2, 3]
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.sample(frac)selects a fraction of the array. By default it samples without replacement and preserves the original order of selected elements.shuffle()returns a randomly reordered copy.peaks()returns local maxima values by default. Usemode="index"to return their positions.duplicates()returns each duplicated value once by default. Usemode="all"to return every repeated occurrence after the first.flatten()performs a deep recursive flatten across nested iterables, except string-like values such asstr,bytes, andbytearray.flatten()follows the iteration order of the source containers. Ordered iterables stay ordered; unordered iterables such assetkeep their native iteration behavior.reshape()validates that the requested shape exactly matches the flattened element count.window()returns an empty result when the requested window size is larger than the array length.reduce()requires either a non-empty array or an explicitinitialvalue.frequency()returns counts in first-seen insertion order on Python 3.7+.take(),drop(), andremove_at()support negative indexes and raise clearIndexErrors when out of range.group_by()returns adictof lists, andpartition()returns a native tuple of lists.shape()requires a regular rectangular nested structure.join()requires string elements.split()is the inverse helper for a one-element string array.lazy()records operations and runs them later withexecute().
Performance Notes
- The current C backend accelerates core access and array operations such as
first,last,middle,rotate,reverse,find,count,contains,chunk,window, andis_sorted. - Higher-level methods such as
map,filter,reduce,sum,mean,median,peaks,unique,duplicates,frequency,flatten, andreshapecurrently run in Python. - This means the API is already hybrid, but the biggest performance wins will come from moving heavier algorithms into the C layer over time.
Supported input types
listtuple- NumPy
ndarraywhen NumPy is installed - general iterables such as
range
Current feature coverage
Access and Position
firstlastmiddlelensizeis_emptyfindindicescountcontainsargmaxargminargsort
Structural and Slicing
rotatereversechunkwindowslicetakedropflattenreshapeinsertremove_atreplace
Grouping and Combination
group_bypartitionzipzip_with
Numeric and Rolling
summeanmedianminmaxdiffcumsumrolling_meanrolling_maxtop_kbottom_k
Filtering and Boolean
mapfilterwherereducemaskrandomsampleshuffle
Set-like and Comparison
unionintersectiondifferencesortedsort_byis_sortedequalsis_uniquehas_duplicatesuniqueduplicatesfrequencypeaks
Shape, Strings, and Debug
shapendimjoinsplitinspectsummarylazy
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.2.0.tar.gz.
File metadata
- Download URL: smartarr-0.2.0.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4f0f7db7ef19b74fddeae92fe18de63ba694973e42e7335bdeea063021cecdb
|
|
| MD5 |
fa705bcd5c27806c424128813c6d347a
|
|
| BLAKE2b-256 |
9477580ab480f4d6285ea3406514d6eeb7faf4d9ae564ece01f9f99ce782fe47
|
File details
Details for the file smartarr-0.2.0-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: smartarr-0.2.0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 22.6 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 |
fad22505e19f42d9e9d26e0a2d8840335b877af9bb98672e7b9c5341131c4891
|
|
| MD5 |
52d6fee372c8ee03fc55ada5d82913d5
|
|
| BLAKE2b-256 |
72092225986ff1f96355c856e8fbd1a5130bd941da060aa61706fd940bf93b17
|