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 charded 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 charded 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 charded 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 charded 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 charded 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 charded 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 charded import VarChar


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


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

Convenience aliases are available in charded.varchar:

from charded.varchar import StrBytes, StrText

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

assert text.text == "hello"

[ref: #full-example]

Full example

from charded 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

charded-1.1.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

charded-1.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file charded-1.1.0.tar.gz.

File metadata

  • Download URL: charded-1.1.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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}

File hashes

Hashes for charded-1.1.0.tar.gz
Algorithm Hash digest
SHA256 222ef96289119a5c255cb520267d31d1b02246c4206874328f2c0b6dcb1b0672
MD5 ac6c747dbbaa78ac79f63bb3eacc36ec
BLAKE2b-256 20aca6f3639f3f269638eebb63757e36b77a6fdf04478eaca91b4d13e04d8761

See more details on using hashes here.

File details

Details for the file charded-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: charded-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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}

File hashes

Hashes for charded-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 149ec51883da8c13355fd677c0e5ccd8dc4a283c206857981e47002af8dafa62
MD5 199b85afc297d67fc08c831d15e04e91
BLAKE2b-256 29317986a8cde45716e19f870df368b0a8c1108bc6c942cef5f4f3646a7aa397

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.0

2 files

1.1.1

2 files

This release

1.1.0 This release

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.1.0

2 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