|
| 1 | +# API Reference |
| 2 | + |
| 3 | +## `ptk(obj)` — callable shorthand |
| 4 | + |
| 5 | +The module itself is callable. `ptk(obj)` is identical to `ptk.minimize(obj)`. |
| 6 | + |
| 7 | +```python |
| 8 | +import ptk |
| 9 | + |
| 10 | +ptk({"user": {"id": 1, "name": "Alice", "bio": None}}) |
| 11 | +# → '{"user":{"id":1,"name":"Alice"}}' |
| 12 | +``` |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## `ptk.minimize(obj, *, aggressive=False, content_type=None, **kw) → str` |
| 17 | + |
| 18 | +Compresses `obj` and returns a string. Never raises on valid Python objects. |
| 19 | + |
| 20 | +### Parameters |
| 21 | + |
| 22 | +| Parameter | Type | Default | Description | |
| 23 | +| --- | --- | --- | --- | |
| 24 | +| `obj` | `Any` | — | Any Python object | |
| 25 | +| `aggressive` | `bool` | `False` | Maximum compression: strips timestamps, sigs-only for code, errors-only for logs | |
| 26 | +| `content_type` | `str \| None` | `None` | Override auto-detection: `"dict"`, `"list"`, `"code"`, `"log"`, `"diff"`, `"text"` | |
| 27 | +| `strip_nulls` | `bool` | `True` | For dicts/lists: strip `None`, `""`, `[]`, `{}` | |
| 28 | +| `format` | `str` | `"json"` | Dict output format: `"json"`, `"kv"`, `"tabular"` | |
| 29 | +| `mode` | `str` | `"clean"` | Code output mode: `"clean"` or `"signatures"` | |
| 30 | +| `errors_only` | `bool` | `False` | For logs: keep only errors and stack traces | |
| 31 | + |
| 32 | +### Examples |
| 33 | + |
| 34 | +```python |
| 35 | +import ptk |
| 36 | + |
| 37 | +# ── dict/list ──────────────────────────────────────────────── |
| 38 | +ptk.minimize(api_response) |
| 39 | +# Strips None, "", [], {} recursively. Returns compact JSON. |
| 40 | + |
| 41 | +ptk.minimize(records, format="tabular") |
| 42 | +# Uniform list of dicts → TSV-style table. 60–70% savings. |
| 43 | + |
| 44 | +ptk.minimize(response, strip_nulls=False) |
| 45 | +# Preserve nulls when they carry semantic meaning. |
| 46 | + |
| 47 | +# ── code ───────────────────────────────────────────────────── |
| 48 | +ptk.minimize(source_code) |
| 49 | +# Strips comments (preserves # noqa, # type: ignore, TODO). |
| 50 | +# Collapses docstrings to one line. |
| 51 | + |
| 52 | +ptk.minimize(source_code, mode="signatures") |
| 53 | +# Returns function/class signatures only. Up to 89% savings. |
| 54 | + |
| 55 | +ptk.minimize(source_code, aggressive=True) |
| 56 | +# Combines signature extraction with maximum noise removal. |
| 57 | + |
| 58 | +# ── logs ───────────────────────────────────────────────────── |
| 59 | +ptk.minimize(ci_log) |
| 60 | +# Collapses duplicate lines with counts. Preserves stack traces. |
| 61 | + |
| 62 | +ptk.minimize(ci_log, errors_only=True) |
| 63 | +# Keeps only ERROR/CRITICAL lines and their stack traces. |
| 64 | + |
| 65 | +ptk.minimize(ci_log, aggressive=True) |
| 66 | +# errors_only + timestamp stripping + line dedup. |
| 67 | + |
| 68 | +# ── diffs ──────────────────────────────────────────────────── |
| 69 | +ptk.minimize(git_diff) |
| 70 | +# Folds unchanged context (@@...@@ blocks). Strips git noise: |
| 71 | +# index lines, old/new mode, binary indicators. |
| 72 | + |
| 73 | +# ── text ───────────────────────────────────────────────────── |
| 74 | +ptk.minimize(prose) |
| 75 | +# Abbreviates verbose words: implementation→impl, configuration→config. |
| 76 | +# Removes filler phrases and stopwords. |
| 77 | + |
| 78 | +# ── force type ─────────────────────────────────────────────── |
| 79 | +ptk.minimize(text, content_type="code") |
| 80 | +# Treats any string as code regardless of content. |
| 81 | +``` |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## `ptk.stats(obj, **kw) → dict` |
| 86 | + |
| 87 | +Same interface as `minimize`. Returns a dict with the compressed output plus token counts. |
| 88 | + |
| 89 | +### Return value |
| 90 | + |
| 91 | +```python |
| 92 | +{ |
| 93 | + "output": str, # the compressed string |
| 94 | + "original_tokens": int, # token count before compression |
| 95 | + "minimized_tokens": int, # token count after compression |
| 96 | + "savings_pct": float, # percentage saved, e.g. 45.4 |
| 97 | + "content_type": str, # detected or forced type |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +`original_tokens` and `minimized_tokens` use `tiktoken` (`cl100k_base`) when installed. Without it, they fall back to `len(text) // 4`. |
| 102 | + |
| 103 | +### Example |
| 104 | + |
| 105 | +```python |
| 106 | +import ptk |
| 107 | + |
| 108 | +result = ptk.stats(api_response) |
| 109 | +print(f"Saved {result['savings_pct']:.1f}% ({result['original_tokens']} → {result['minimized_tokens']} tokens)") |
| 110 | +# → Saved 45.4% (1450 → 792 tokens) |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## `ptk.detect_type(obj) → str` |
| 116 | + |
| 117 | +Returns the detected content type without compressing. |
| 118 | + |
| 119 | +```python |
| 120 | +ptk.detect_type({"key": "value"}) # → "dict" |
| 121 | +ptk.detect_type([1, 2, 3]) # → "list" |
| 122 | +ptk.detect_type("def foo():\n pass") # → "code" |
| 123 | +ptk.detect_type("ERROR: connection refused\nTraceback...") # → "log" |
| 124 | +ptk.detect_type("@@ -1,3 +1,4 @@\n+new line") # → "diff" |
| 125 | +ptk.detect_type("Some prose text.") # → "text" |
| 126 | +``` |
| 127 | + |
| 128 | +Detection is fast: O(1) for non-strings (checks Python type), 2KB scan for strings. |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Content type strategies |
| 133 | + |
| 134 | +| Type | Trigger | Key savings | |
| 135 | +| --- | --- | --- | |
| 136 | +| `dict` | `isinstance(obj, dict)` | Null stripping, key shortening, tabular encoding | |
| 137 | +| `list` | `isinstance(obj, list)` | Schema-once tabular, dedup with counts, sampling | |
| 138 | +| `code` | `def `, `class `, `import ` in first 2KB | Comment strip (pragma-safe), docstring collapse, sig extraction | |
| 139 | +| `log` | `ERROR`, `WARNING`, `Traceback` in first 2KB | Line dedup with counts, error filter, stack trace preservation | |
| 140 | +| `diff` | `@@` or `+++ ` / `--- ` in first 2KB | Context folding, git noise strip | |
| 141 | +| `text` | fallback for all strings | Word abbreviation, filler removal, stopword removal | |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## Error handling guarantees |
| 146 | + |
| 147 | +- `minimize()` **never raises** on valid Python objects. `RecursionError`, `ValueError`, `TypeError`, `OverflowError` inside a minimizer all fall back to `str(obj)`. |
| 148 | +- `minimize()` **never mutates** the input. Verified by deepcopy comparison in the test suite. |
| 149 | +- The module is **thread-safe**. Minimizers are stateless singletons. |
0 commit comments