Skip to content

Commit e95a1ff

Browse files
committed
fix: address review comments on PR #57
- Update utils.py module docstring to include JSON helpers - Fix circular import of encode in utils.py - Remove unused imports and print statements in tests - Fix formatting in tests using ruff
1 parent 230c684 commit e95a1ff

2 files changed

Lines changed: 14 additions & 28 deletions

File tree

src/toon_format/utils.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
# Copyright (c) 2025 TOON Format Organization
22
# SPDX-License-Identifier: MIT
3-
"""Token analysis utilities for TOON format.
3+
"""Utilities for TOON format.
44
5-
This module provides utilities for counting tokens and comparing
6-
token efficiency between JSON and TOON formats. Useful for:
7-
- Estimating API costs (tokens are the primary cost driver)
8-
- Optimizing prompt sizes for LLM context windows
9-
- Benchmarking TOON's token efficiency
5+
This module provides utilities for:
6+
- Token analysis and efficiency comparison between JSON and TOON formats
7+
- JSON integration and null value handling
8+
- Estimating API costs and optimizing prompt sizes
109
1110
Functions:
1211
count_tokens: Count tokens in a text string
1312
estimate_savings: Compare JSON vs TOON token counts
1413
compare_formats: Generate formatted comparison table
14+
loads: Parse JSON string into Python objects (alias for json.loads)
15+
encode_json: Encode a JSON string directly into TOON format
1516
1617
Requirements:
1718
tiktoken: Install with `uv add tiktoken` or `uv add toon_format[benchmark]`
18-
19-
Example:
20-
>>> import toon_format
21-
>>> data = {"name": "Alice", "age": 30}
22-
>>> result = toon_format.estimate_savings(data)
23-
>>> print(f"TOON saves {result['savings_percent']:.1f}% tokens")
2419
"""
2520

2621
import functools
2722
import json
2823
from typing import Any, Dict
2924

30-
# Import encode from parent package (defined in __init__.py before this module is imported)
31-
# __init__.py defines encode() before importing utils, so this is safe
32-
from . import encode
25+
from .encoder import encode
3326

3427
__all__ = ["count_tokens", "estimate_savings", "compare_formats", "encode_json", "loads"]
3528

tests/test_json_integration.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
1-
import json
2-
from toon_format import encode_json, loads, encode
1+
from toon_format import encode_json, loads
2+
33

44
def test_loads_null_to_none():
55
json_str = '{"abc": null, "xyz": 123}'
66
data = loads(json_str)
77
assert data["abc"] is None
88
assert data["xyz"] == 123
9-
print("test_loads_null_to_none passed")
9+
1010

1111
def test_encode_json_integration():
1212
json_str = '{"abc": null, "xyz": null}'
1313
# This should automatically handle null -> None -> TOON null
1414
toon_output = encode_json(json_str)
1515
expected = "abc: null\nxyz: null"
1616
assert toon_output.strip() == expected
17-
print("test_encode_json_integration passed")
17+
1818

1919
def test_complex_json_integration():
20-
json_str = '''
20+
json_str = """
2121
{
2222
"status": "success",
2323
"data": {
2424
"user": null,
2525
"items": [1, null, 3]
2626
}
2727
}
28-
'''
28+
"""
2929
toon_output = encode_json(json_str)
3030
assert "user: null" in toon_output
3131
# Check for null in items array (can be inline "1,null,3" or list "- null")
3232
assert "null" in toon_output
33-
print("test_complex_json_integration passed")
34-
35-
if __name__ == "__main__":
36-
test_loads_null_to_none()
37-
test_encode_json_integration()
38-
test_complex_json_integration()
39-
print("All JSON integration tests passed!")

0 commit comments

Comments
 (0)