Small Python package for converting iterables to arrays.
Project description
toarray
Small Python package for converting iterables to the smallest fitting array.array type.
Status
- Supports Python 3.9–3.13
- License: MIT
Description
toarray.get_array(iterable) inspects the values and returns the smallest suitable
array.array when possible, otherwise it returns a plain list.
Rules of thumb:
- Non-numeric values → returns
list - Integers → prefers
B,b,H,h,I,i,Q,q(in that order) - Floats → tries
fthend - Empty iterables → returns
[]
Note: Unicode arrays (array('u')) are deprecated/removed in modern Python and are not used.
Install
pip install toarray
# optional extras
pip install toarray[numpy]
pip install toarray[arrow]
Usage
from toarray import get_array, select_array, analyze_array, stream_array
get_array([0, 1, 255]).typecode # 'B'
get_array([-1, 0, 1]).typecode # 'b'
get_array([1e-3, 2.5]).typecode # 'f'
get_array(["a", "b"]) # ['a', 'b']
get_array([]) # []
# Policy-based selection (control bounds and strategy)
arr = select_array(
[0, 1, 256],
policy='smallest', # 'smallest' | 'balanced' | 'wide'
prefer_signed=False,
min_type=None, # e.g. 'h', 'i', 'q', 'f', 'd'
max_type=None,
)
# Metadata
info = analyze_array([1, 2, 3])
info.typecode, info.min, info.max # ('B', 1.0, 3.0)
# Streaming
for chunk in stream_array(range(1000), chunk_size=256):
pass
# Optional extras
# NumPy (pip install toarray[numpy])
from toarray import to_numpy
arr_np = to_numpy([0,1,2], dtype='min') # dtype: uint8
arr_np_exp = to_numpy([0,1,2], dtype='float64') # dtype: float64
# PyArrow (pip install toarray[arrow])
from toarray import to_arrow
arr_pa = to_arrow([0,1,2], type='min') # pa.array([0, 1, 2], type=uint8)
arr_pa_chunked = to_arrow(range(10), type='float64', chunk_size=4) # pa.ChunkedArray
Selection policies and strict mode
- Policies adjust the candidate order searched for a fit:
smallest: smallest footprint first (default)balanced: mildly prefers signed widths earlywide: wider integer types first
- Bounds:
min_type/max_typelet you constrain the search (e.g., up toionly). - Floats:
allow_float_downgrade=Falseskipsfwhen magnitudes exceed float32. no_float=Trueforces integers only; withstrict=Truethis raises on floats.
Notes
- Non‑numeric input returns a
list(text is not coerced toarray('u')). - Integer preference order:
B,b,H,h,I,i,Q,q. - Float preference order:
fthend(subject to range and policy).
Development
This project uses pytest, ruff, and black.
pip install -e .[dev]
ruff check .
black --check .
pytest
Authors
Odos Matthews — odosmatthews@gmail.com
License
MIT — see LICENSE.md for details
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 toarray-0.3.2.tar.gz.
File metadata
- Download URL: toarray-0.3.2.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a976fb4325913c8f6f344cb3feda6a782ab1403a5fc14620adad9ced4e9a746e
|
|
| MD5 |
bbda514504e928c8567ff5e13225d7a7
|
|
| BLAKE2b-256 |
d499889572a9ee297a77cdf89ea0ad4a803c25e647e4880d3eaf24ec5d44286a
|
File details
Details for the file toarray-0.3.2-py3-none-any.whl.
File metadata
- Download URL: toarray-0.3.2-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7436bfd1a740b7626e2479beaf6ddc85dee74f17160e3d8eca06f9c467179e51
|
|
| MD5 |
f24bc03dc72ad640272acbe6b69fb494
|
|
| BLAKE2b-256 |
fc1ddc65fa917e9efbfeee40b79463b934b81d9bdde3fa50536ca638f6cfa79c
|