Skip to main content

title: charded description: Type-safe, generic string/bytes container with charset detection

[ref: #charded]

charded

charded is a small Python library for working with str and bytes as a single, type-safe container. It detects charsets automatically, converts lazily between text and bytes, and proxies the familiar str API so the wrapper feels invisible.

It gives you four public entry points:

  • varchar — a generic str | bytes container.
  • to_text — convert str or bytes to text.
  • to_bytes — convert str or bytes to bytes.
  • to_ascii — decode bytes to text with a default ASCII charset.

[ref: #installation]

Installation

Install with uv:

uv add charded

Or with any PEP 517-compatible tool:

pip install charded

[ref: #str]

Wrapping text and bytes with varchar

varchar[T] is parameterized by T, which is either str or bytes. Pass any str or bytes object and read it back through str(), bytes(), .text, or .bytes.

from zx.str import varchar

s = varchar(b"hello")
assert str(s) == "hello"
assert bytes(s) == b"hello"
assert s.text == "hello"
assert s.bytes == b"hello"

t = varchar("héllo")
assert str(t) == "héllo"
assert bytes(t) == b"h\xc3\xa9llo"

[ref: #charset-detection]

Charset detection

varchar detects the content charset using charset-normalizer, internal heuristics, and an adaptive binary-offset scan. Access .charset to see the result.

from zx.str import varchar

assert varchar(b"hello").charset == "ascii"
assert varchar(b"hello\x00world").charset == "bin"

The detection order is:

  1. Explicit charset= argument if provided.
  2. Result from charset-normalizer.
  3. Internal heuristic scan.
  4. Fallback to ASCII for bytes or a compatible charset for text.

[ref: #mime-detection]

MIME type detection

varchar.mime uses python-magic to sniff the content type and description. It returns None when detection fails.

from zx.str import varchar

mime = varchar(b'{"a": 1}').mime
assert mime is not None
assert mime["type"] == "application/json"

[ref: #string-api]

Using varchar as a string

varchar proxies common str operations, so you can slice, iterate, compare, and call most str methods directly.

from zx.str import varchar

s = varchar(b"hello world")

assert len(s) == 11
assert s[0:5] == "hello"
assert s.startswith("hello")
assert s.split() == ["hello", "world"]
assert s.upper() == "HELLO WORLD"
assert "world" in s

[ref: #tokenize]

Tokenizing with varchar.tokenize

varchar.tokenize splits the string using a regular expression. Capturing groups are not allowed; use non-capturing groups (?:...) instead.

from zx.str import varchar

s = varchar("foo-bar_baz, qux")
assert s.tokenize(r"[a-z]+") == ("foo", "bar", "baz", "qux")

[ref: #utility-functions]

Utility functions

to_text, to_bytes, and to_ascii perform quick conversions without creating a persistent varchar instance.

from zx.str import to_ascii, to_bytes, to_text

assert to_text(b"hello") == "hello"
assert to_bytes("hello") == b"hello"
assert to_ascii(b"hello") == "hello"

[ref: #type-safety]

Type safety and aliases

varchar is a true generic. Use varchar[bytes] or varchar[str] to constrain input and let type checkers catch mismatches at compile time.

from zx.str import varchar


def process(data: varchar[bytes]) -> str:
    return data.text.upper()


assert process(varchar(b"hello")) == "HELLO"

Convenience aliases are available in zx.str.varchar:

from zx.str.varchar import StrBytes, StrText

raw: StrBytes = varchar(b"hello")
text: StrText = varchar(raw.text)

assert text.text == "hello"

[ref: #full-example]

Full example

from zx.str import varchar, to_bytes, to_text

raw = b"h\xc3\xa9llo"

s = varchar(raw)
assert str(s) == "héllo"
assert bytes(s) == raw
assert s.charset is not None
assert s.startswith("hél")

# Quick conversions without wrapping
assert to_text(raw) == "héllo"
assert to_bytes("héllo") == raw

Metadata

Release files for charded 2.0.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 charded 2.0.2
File Size Uploaded Provenance Actions
charded-2.0.2.tar.gz 17.5 kB Without provenance Details for charded-2.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 25.4 kB

Release files / charded-2.0.2.tar.gz

Download URL charded-2.0.2.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
45a4a18f3d52ee0178ad91f8e979a2d56240a4b43a9f139fc07084adc6dc79ed
BLAKE2b-256 checksum
How to use checksums
f99f890c7ca55f9d5dd8c9fb27859ac910f67703a6da793401fdcb20f25ebdb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / charded-2.0.2-py3-none-any.whl

Download URL charded-2.0.2-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2fb63e34400a0e0d747888821ffd1074b30a20bb1b2ec264e06b4c924872c5e
BLAKE2b-256 checksum
How to use checksums
4653adc64bfafe2f4c5d0d78fae8862e00723590f5ab9fb5ec222e1f6f786620
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for charded 2.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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