Skip to main content

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 f then d
  • 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 early
    • wide: wider integer types first
  • Bounds: min_type/max_type let you constrain the search (e.g., up to i only).
  • Floats: allow_float_downgrade=False skips f when magnitudes exceed float32.
  • no_float=True forces integers only; with strict=True this raises on floats.

Notes

  • Non‑numeric input returns a list (text is not coerced to array('u')).
  • Integer preference order: B, b, H, h, I, i, Q, q.
  • Float preference order: f then d (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

Metadata

Release files for toarray 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for toarray 0.3.2
File Size Uploaded
toarray-0.3.2.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for toarray 0.3.2
File Interpreter ABI Platform
toarray-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.4 kB

Release files / toarray-0.3.2.tar.gz

Download URL toarray-0.3.2.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a976fb4325913c8f6f344cb3feda6a782ab1403a5fc14620adad9ced4e9a746e
BLAKE2b-256 checksum
How to use checksums
d499889572a9ee297a77cdf89ea0ad4a803c25e647e4880d3eaf24ec5d44286a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.18

Release files / toarray-0.3.2-py3-none-any.whl

Download URL toarray-0.3.2-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7436bfd1a740b7626e2479beaf6ddc85dee74f17160e3d8eca06f9c467179e51
BLAKE2b-256 checksum
How to use checksums
fc1ddc65fa917e9efbfeee40b79463b934b81d9bdde3fa50536ca638f6cfa79c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.18

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page