Skip to content

Commit 1fbedbf

Browse files
authored
tweak python package (#96)
1 parent d89c3a8 commit 1fbedbf

File tree

7 files changed

+107
-33
lines changed

7 files changed

+107
-33
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,10 @@ jobs:
492492

493493
- name: run tests
494494
run: |
495-
cd crates/jiter-python
496495
python3 -m pip install -U pip -r tests/requirements.txt
497496
python3 -m pip install jiter --no-index --no-deps --find-links dist --force-reinstall
498497
python3 -m pytest
499-
498+
working-directory: crates/jiter-python
500499

501500
# https://github.com/marketplace/actions/alls-green#why used for branch protection checks
502501
check:

crates/jiter-python/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ extension-module = ["pyo3/extension-module"]
1515
[lib]
1616
name = "jiter_python"
1717
crate-type = ["cdylib", "rlib"]
18+
19+
[lints.clippy]
20+
dbg_macro = "deny"
21+
print_stdout = "deny"
22+
print_stderr = "deny"
23+
# in general we lint against the pedantic group, but we will whitelist
24+
# certain lints which we don't want to enforce (for now)
25+
pedantic = { level = "deny", priority = -1 }
26+
missing_errors_doc = "allow"
27+
must_use_candidate = "allow"

crates/jiter-python/README.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
# jiter
22

3+
[![CI](https://github.com/pydantic/jiter/workflows/CI/badge.svg?event=push)](https://github.com/pydantic/jiter/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
4+
[![pypi](https://img.shields.io/pypi/v/jiter.svg)](https://pypi.python.org/pypi/jiter)
5+
[![versions](https://img.shields.io/pypi/pyversions/jiter.svg)](https://github.com/pydantic/jiter)
6+
[![license](https://img.shields.io/github/license/pydantic/jiter.svg)](https://github.com/pydantic/jiter/blob/main/LICENSE)
7+
38
This is a standalone version of the JSON parser used in `pydantic-core`. The recommendation is to only use this package directly if you do not use `pydantic`.
49

510
The API is extremely minimal:
611

712
```python
813
def from_json(
9-
data: bytes,
14+
json_data: bytes,
15+
/,
1016
*,
1117
allow_inf_nan: bool = True,
12-
cache_strings: Literal[True, False, 'all', 'keys', 'none'] = True,
18+
cache_strings: Literal[True, False, "all", "keys", "none"] = True,
1319
allow_partial: bool = False,
1420
catch_duplicate_keys: bool = False,
1521
) -> Any:
1622
"""
17-
Parse input bytes into a JSON string.
18-
19-
allow_inf_nan: if True, to allow Infinity and NaN as values in the JSON
20-
cache_strings: cache Python strings to improve performance at the cost of some memory usage
21-
- True / 'all' - cache all strings
22-
- 'keys' - cache only object keys
23-
- 'none' - cache nothing
24-
allow_partial: if True, return parsed content when reaching EOF without closing objects and arrays
25-
catch_duplicate_keys: if True, raise an exception if objects contain the same key multiple times
23+
Parse input bytes into a JSON object.
24+
25+
Arguments:
26+
json_data: The JSON data to parse
27+
allow_inf_nan: Whether to allow infinity (`Infinity` an `-Infinity`) and `NaN` values to float fields.
28+
Defaults to True.
29+
cache_strings: cache Python strings to improve performance at the cost of some memory usage
30+
- True / 'all' - cache all strings
31+
- 'keys' - cache only object keys
32+
- False / 'none' - cache nothing
33+
allow_partial: if True, return parsed content when reaching EOF without closing objects and arrays
34+
catch_duplicate_keys: if True, raise an exception if objects contain the same key multiple times
35+
36+
Returns:
37+
Python object built from the JSON input.
2638
"""
27-
...
2839

2940
def cache_clear() -> None:
30-
"""Clear the string cache"""
31-
...
41+
"""
42+
Reset the string cache.
43+
"""
3244

3345
def cache_usage() -> int:
34-
"""Get number of strings in the cache"""
35-
...
46+
"""
47+
get the size of the string cache.
48+
49+
Returns:
50+
Size of the string cache in bytes.
51+
"""
3652
```

crates/jiter-python/jiter.pyi

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
from typing import Any, Literal
22

33
def from_json(
4-
data: bytes,
4+
json_data: bytes,
5+
/,
56
*,
67
allow_inf_nan: bool = True,
78
cache_strings: Literal[True, False, "all", "keys", "none"] = True,
89
allow_partial: bool = False,
910
catch_duplicate_keys: bool = False,
10-
) -> Any: ...
11-
def cache_clear() -> None: ...
12-
def cache_usage() -> int: ...
11+
) -> Any:
12+
"""
13+
Parse input bytes into a JSON object.
14+
15+
Arguments:
16+
json_data: The JSON data to parse
17+
allow_inf_nan: Whether to allow infinity (`Infinity` an `-Infinity`) and `NaN` values to float fields.
18+
Defaults to True.
19+
cache_strings: cache Python strings to improve performance at the cost of some memory usage
20+
- True / 'all' - cache all strings
21+
- 'keys' - cache only object keys
22+
- False / 'none' - cache nothing
23+
allow_partial: if True, return parsed content when reaching EOF without closing objects and arrays
24+
catch_duplicate_keys: if True, raise an exception if objects contain the same key multiple times
25+
26+
Returns:
27+
Python object built from the JSON input.
28+
"""
29+
30+
def cache_clear() -> None:
31+
"""
32+
Reset the string cache.
33+
"""
34+
35+
def cache_usage() -> int:
36+
"""
37+
get the size of the string cache.
38+
39+
Returns:
40+
Size of the string cache in bytes.
41+
"""

crates/jiter-python/pyproject.toml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,35 @@ build-backend = "maturin"
44

55
[project]
66
name = "jiter"
7+
description = "Fast iterable JSON parser."
78
requires-python = ">=3.8"
89
authors = [
910
{name = "Samuel Colvin", email = "[email protected]"}
1011
]
11-
dynamic = [
12-
"description",
13-
"license",
14-
"readme",
15-
"version"
12+
license = "MIT"
13+
readme = "README.md"
14+
classifiers = [
15+
"Development Status :: 4 - Beta",
16+
"Programming Language :: Python",
17+
"Programming Language :: Python :: 3",
18+
"Programming Language :: Python :: 3 :: Only",
19+
"Programming Language :: Python :: 3.8",
20+
"Programming Language :: Python :: 3.9",
21+
"Programming Language :: Python :: 3.10",
22+
"Programming Language :: Python :: 3.11",
23+
"Programming Language :: Python :: 3.12",
24+
"Intended Audience :: Developers",
25+
"Intended Audience :: Information Technology",
26+
"Intended Audience :: System Administrators",
27+
"License :: OSI Approved :: MIT License",
28+
"Operating System :: Unix",
29+
"Operating System :: POSIX :: Linux",
30+
"Environment :: Console",
31+
"Environment :: MacOS X",
32+
"Topic :: File Formats :: JSON",
33+
"Framework :: Pydantic :: 2",
1634
]
35+
dynamic = ["version"]
1736

1837
[tool.maturin]
1938
module-name = "jiter"

crates/jiter-python/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use jiter::{map_json_error, python_parse, StringCacheMode};
66

77
#[pyfunction(
88
signature = (
9-
data,
9+
json_data,
10+
/,
1011
*,
1112
allow_inf_nan=true,
1213
cache_strings=StringCacheMode::All,
@@ -16,22 +17,21 @@ use jiter::{map_json_error, python_parse, StringCacheMode};
1617
)]
1718
pub fn from_json<'py>(
1819
py: Python<'py>,
19-
data: &[u8],
20+
json_data: &[u8],
2021
allow_inf_nan: bool,
2122
cache_strings: StringCacheMode,
2223
allow_partial: bool,
2324
catch_duplicate_keys: bool,
2425
) -> PyResult<Bound<'py, PyAny>> {
25-
let json_bytes = data;
2626
python_parse(
2727
py,
28-
json_bytes,
28+
json_data,
2929
allow_inf_nan,
3030
cache_strings,
3131
allow_partial,
3232
catch_duplicate_keys,
3333
)
34-
.map_err(|e| map_json_error(json_bytes, &e))
34+
.map_err(|e| map_json_error(json_data, &e))
3535
}
3636

3737
pub fn get_jiter_version() -> &'static str {
@@ -50,7 +50,7 @@ pub fn get_jiter_version() -> &'static str {
5050

5151
#[pyfunction]
5252
pub fn cache_clear(py: Python<'_>) {
53-
jiter::cache_clear(py)
53+
jiter::cache_clear(py);
5454
}
5555

5656
#[pyfunction]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pytest
2+
pytest-pretty
23
dirty_equals

0 commit comments

Comments
 (0)