Metadata-Version: 2.4
Name: terset
Version: 0.2.1
Summary: Encode JSON records as compact, typed tables.
Author: TrueWear, LLC
License-Expression: MIT
Project-URL: Homepage, https://terset.dev
Project-URL: Source, https://github.com/TrueWear/terset
Keywords: json,llm,prompt,tokens,table,encoding
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Terset for Python

Terset writes JSON records as compact, typed tables using format v2. Python 3.10 or newer.
Zero runtime dependencies.

## Install

Coming soon on PyPI. This package is not published yet.

```sh
pip install terset
```

## Use

```python
from terset import decode, encode, rules_for

data = [{"id": "1", "name": "Ada"}, {"id": "2", "name": "Ben"}]
text = encode(data)
print(text)
print(decode(text) == data)
```

Output (the gaps are tabs):

```text
[2]
id	name
1	Ada
2	Ben
True
```

`encode(value)` accepts a dictionary or list at the root. Values can be
strings, finite numbers, booleans, `None`, dictionaries with string keys,
and lists. It returns canonical Terset text. Put `rules_for(text)` before the data
when a reader needs the format explained. It validates canonical text and returns
the base rules, plus rules for keyed tables and dotted headers when present.
The exported `RULES` keeps all rules for any text. `decode(text)` returns the same
JSON value and rejects text the encoder would not produce.

Lists of at least two dictionaries become tables when the header and rows
save UTF-8 bytes. Named tables start with `name[N]:`. Root tables start
with `[N]` on its own line. Compact JSON lists have no count.
Missing fields, empty strings and `None` stay distinct.

A map in a direct root field can use `name{N}:` when the complete named
block is strictly shorter than `name: JSON`. Its first column is `@key`,
which holds each entry's key and is not a field. Root maps stay fields.
Uniform nested objects with identical ordered simple keys expand into
dotted headers, recursively. Literal dotted field names are quoted.
Other nested values stay compact JSON. The decoder rejects count mismatches
and accepts only the current canonical format.
See the full [format v2 specification](https://github.com/TrueWear/terset/blob/main/packages/terset/SPEC.md).

## Python details

- The format uses JavaScript binary64 numbers. `1.0` writes as `1`.
  Decoded integer literals within `[-2**53, 2**53]` become Python `int`
  values. Tokens with a fraction or exponent, and integers outside this
  range, become `float` values.
  Python integers must be exactly representable as binary64 numbers;
  otherwise encoding raises `TypeError`. This avoids silently rounding
  integers such as `9007199254740993`.
- Numeric negative zero, infinities, NaN, cycles, non-JSON types and depth
  above 500 raise `TypeError`. Use plain built-in dictionaries and lists.
- Dictionary order is preserved, with JavaScript's array-index keys first
  in numeric order. For example, `"2"` precedes `"10"`; `"01"` keeps its
  insertion position among other keys.
- Unicode stays unchanged, including normalization forms and lone
  surrogates. Explicit UTF-16 surrogate pairs become Python astral code
  points. Keys that become identical through this conversion are rejected.
- Format validation errors raise `ValueError`, including non-finite numbers
  and excessive depth. Passing a non-string to `decode` raises `TypeError`.

`encode` uses the covariant `JsonInputRoot` and `JsonInputValue` aliases,
based on read-only `Mapping` and `Sequence` interfaces. This lets typed
lists and dictionaries pass without widening their element types. Runtime
validation still requires plain built-in lists and dictionaries.
`decode` returns concrete `JsonRoot`, `JsonObject` and `JsonValue` types.
The package includes `py.typed`.

## Limits

The format rejects numeric negative zero and nesting deeper than 500.
With deep data and an already deep caller stack,
`encode`, `decode` and `rules_for` can raise `RecursionError`.
This depends on the available stack depth, not the Python version.

## Local tests

From the repository root, without installing the package:

```sh
python3 -B -m unittest discover -s packages/terset-py/tests -v
```

Tests read the shared cases generated by the TypeScript encoder and check
exact output, decoding, the rules string, and invalid inputs. To regenerate
the cases from the repository root with Node 24:

```sh
node packages/terset/tests/generate-cases.ts
```

## License

MIT. Copyright (c) 2026 TrueWear, LLC. The license text is included in `LICENSE`.
