Skip to content

Commit

Permalink
Rename indent_depth to max_indent_level
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Nov 27, 2024
1 parent 75045d1 commit b31f897
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 45 deletions.
7 changes: 4 additions & 3 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ jsonyx 2.0.0 (unreleased)
- Added the ``jsonyx`` application
- Added ``mapping_type`` and ``seq_type`` to :class:`jsonyx.Decoder`,
:func:`jsonyx.load`, :func:`jsonyx.loads` and :func:`jsonyx.read`
- Added ``commas``, ``indent_depth``, ``indent_leaves``, ``mapping_types``,
``seq_types`` and ``quoted_keys`` to :class:`jsonyx.Encoder`,
:func:`jsonyx.dump`, :func:`jsonyx.dumps` and :func:`jsonyx.write`
- Added ``commas``, ``indent_leaves``, ``mapping_types``,
``max_indent_level``, ``seq_types`` and ``quoted_keys`` to
:class:`jsonyx.Encoder`, :func:`jsonyx.dump`, :func:`jsonyx.dumps` and
:func:`jsonyx.write`
- Added ``python -m jsonyx diff`` and ``python -m jsonyx patch``
- Added ``--indent-leaves`` and its alias ``-l`` to ``python -m jsonyx format``
- Added ``--unquoted-keys`` and its alias ``-q`` to ``python -m jsonyx format``
Expand Down
6 changes: 3 additions & 3 deletions docs/source/get-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Compact encoding
Pretty printing
^^^^^^^^^^^^^^^

.. versionchanged:: 2.0 Added ``indent_depth`` and ``indent_leaves``.
.. versionchanged:: 2.0 Added ``indent_leaves`` and ``max_indent_level``.

>>> import jsonyx as json
>>> json.dump({"foo": [1, 2, 3], "bar": {"a": 1, "b": 2, "c": 3}}, indent=4)
Expand All @@ -139,8 +139,8 @@ Pretty printing
}

.. tip:: Use ``ensure_ascii=True`` to escape non-ASCII characters,
``indent_depth=1`` to indent up to level 1, ``indent_leaves=True`` to
indent everything and ``sort_keys=True`` to sort the keys of objects.
``indent_leaves=True`` to indent everything, ``max_indent_level=1`` to
indent up to level 1, and ``sort_keys=True`` to sort the keys of objects.

.. seealso:: The built-in :mod:`pprint` module for pretty-printing arbitrary
Python data structures.
Expand Down
30 changes: 15 additions & 15 deletions src/jsonyx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ def write(
end: str = "\n",
ensure_ascii: bool = False,
indent: int | str | None = None,
indent_depth: int | None = None,
indent_leaves: bool = False,
mapping_types: type | tuple[type, ...] = (),
max_indent_level: int | None = None,
quoted_keys: bool = True,
separators: tuple[str, str] = (", ", ": "),
seq_types: type | tuple[type, ...] = (),
Expand All @@ -239,8 +239,8 @@ def write(
.. versionchanged:: 2.0
- Added ``commas``, ``indent_depth``, ``indent_leaves``,
``mapping_types``, ``seq_types`` and ``quoted_keys``.
- Added ``commas``, ``indent_leaves``, ``mapping_types``,
``max_indent_level``, ``seq_types`` and ``quoted_keys``.
- Made :class:`tuple` JSON serializable.
- Merged ``item_separator`` and ``key_separator`` as ``separators``.
Expand All @@ -251,10 +251,10 @@ def write(
:param end: the string to append at the end
:param ensure_ascii: escape non-ASCII characters
:param indent: the number of spaces or string to indent with
:param indent_depth: the depth up to which to indent
:param indent_leaves: indent leaf objects and arrays
:param mapping_types: an additional mapping type or tuple of additional
mapping types
:param max_indent_level: the level up to which to indent
:param quoted_keys: quote keys which are identifiers
:param separators: the item and key separator
:param seq_types: an additional sequence type or tuple of additional
Expand Down Expand Up @@ -288,9 +288,9 @@ def write(
end=end,
ensure_ascii=ensure_ascii,
indent=indent,
indent_depth=indent_depth,
indent_leaves=indent_leaves,
mapping_types=mapping_types,
max_indent_level=max_indent_level,
quoted_keys=quoted_keys,
separators=separators,
seq_types=seq_types,
Expand All @@ -308,9 +308,9 @@ def dump(
end: str = "\n",
ensure_ascii: bool = False,
indent: int | str | None = None,
indent_depth: int | None = None,
indent_leaves: bool = False,
mapping_types: type | tuple[type, ...] = (),
max_indent_level: int | None = None,
quoted_keys: bool = True,
separators: tuple[str, str] = (", ", ": "),
seq_types: type | tuple[type, ...] = (),
Expand All @@ -321,8 +321,8 @@ def dump(
.. versionchanged:: 2.0
- Added ``commas``, ``indent_depth``, ``indent_leaves``,
``mapping_types``, ``seq_types`` and ``quoted_keys``.
- Added ``commas``, ``indent_leaves``, ``mapping_types``,
``max_indent_level``, ``seq_types`` and ``quoted_keys``.
- Made :class:`tuple` JSON serializable.
- Merged ``item_separator`` and ``key_separator`` as ``separators``.
Expand All @@ -333,10 +333,10 @@ def dump(
:param end: the string to append at the end
:param ensure_ascii: escape non-ASCII characters
:param indent: the number of spaces or string to indent with
:param indent_depth: the depth up to which to indent
:param indent_leaves: indent leaf objects and arrays
:param mapping_types: an additional mapping type or tuple of additional
mapping types
:param max_indent_level: the level up to which to indent
:param quoted_keys: quote keys which are identifiers
:param separators: the item and key separator
:param seq_types: an additional sequence type or tuple of additional
Expand Down Expand Up @@ -369,9 +369,9 @@ def dump(
end=end,
ensure_ascii=ensure_ascii,
indent=indent,
indent_depth=indent_depth,
indent_leaves=indent_leaves,
mapping_types=mapping_types,
max_indent_level=max_indent_level,
quoted_keys=quoted_keys,
separators=separators,
seq_types=seq_types,
Expand All @@ -388,9 +388,9 @@ def dumps(
end: str = "\n",
ensure_ascii: bool = False,
indent: int | str | None = None,
indent_depth: int | None = None,
indent_leaves: bool = False,
mapping_types: type | tuple[type, ...] = (),
max_indent_level: int | None = None,
quoted_keys: bool = True,
separators: tuple[str, str] = (", ", ": "),
seq_types: type | tuple[type, ...] = (),
Expand All @@ -401,8 +401,8 @@ def dumps(
.. versionchanged:: 2.0
- Added ``commas``, ``indent_depth``, ``indent_leaves``,
``mapping_types``, ``seq_types`` and ``quoted_keys``.
- Added ``commas``, ``indent_leaves``, ``mapping_types``,
``max_indent_level``, ``seq_types`` and ``quoted_keys``.
- Made :class:`tuple` JSON serializable.
- Merged ``item_separator`` and ``key_separator`` as ``separators``.
Expand All @@ -412,10 +412,10 @@ def dumps(
:param end: the string to append at the end
:param ensure_ascii: escape non-ASCII characters
:param indent: the number of spaces or string to indent with
:param indent_depth: the depth up to which to indent
:param indent_leaves: indent leaf objects and arrays
:param mapping_types: an additional mapping type or tuple of additional
mapping types
:param max_indent_level: the level up to which to indent
:param quoted_keys: quote keys which are identifiers
:param separators: the item and key separator
:param seq_types: an additional sequence type or tuple of additional
Expand Down Expand Up @@ -444,9 +444,9 @@ def dumps(
end=end,
ensure_ascii=ensure_ascii,
indent=indent,
indent_depth=indent_depth,
indent_leaves=indent_leaves,
mapping_types=mapping_types,
max_indent_level=max_indent_level,
quoted_keys=quoted_keys,
separators=separators,
seq_types=seq_types,
Expand Down
2 changes: 1 addition & 1 deletion src/jsonyx/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# Copyright (C) 2024 Nice Zombies
"""A command line utility to manipulate JSON files."""
# TODO(Nice Zombies): add --indent-depth
# TODO(Nice Zombies): add --max-indent-level
from __future__ import annotations

__all__: list[str] = ["main"]
Expand Down
20 changes: 10 additions & 10 deletions src/jsonyx/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def make_encoder(
item_separator: str,
long_item_separator: str,
key_separator: str,
indent_depth: int,
max_indent_level: int,
allow_nan_and_infinity: bool, # noqa: FBT001
allow_surrogates: bool, # noqa: FBT001
ensure_ascii: bool, # noqa: FBT001
Expand Down Expand Up @@ -148,7 +148,7 @@ def write_sequence(
markers[markerid] = seq
write("[")
current_indent: str = old_indent
if indent is None or indent_level >= indent_depth or (
if indent is None or indent_level >= max_indent_level or (
not indent_leaves and all(value is None or isinstance(
value, (Decimal, float, int, str),
) for value in seq)
Expand Down Expand Up @@ -197,7 +197,7 @@ def write_mapping(
markers[markerid] = mapping
write("{")
current_indent: str = old_indent
if indent is None or indent_level >= indent_depth or (
if indent is None or indent_level >= max_indent_level or (
not indent_leaves and all(value is None or isinstance(
value, (Decimal, float, int, str),
) for value in mapping.values())
Expand Down Expand Up @@ -295,8 +295,8 @@ class Encoder:
.. versionchanged:: 2.0
- Added ``commas``, ``indent_depth``, ``indent_leaves``,
``mapping_types``, ``seq_types`` and ``quoted_keys``.
- Added ``commas``, ``indent_leaves``, ``mapping_types``,
``max_indent_level``, ``seq_types`` and ``quoted_keys``.
- Made :class:`tuple` JSON serializable.
- Merged ``item_separator`` and ``key_separator`` as ``separators``.
Expand All @@ -305,10 +305,10 @@ class Encoder:
:param end: the string to append at the end
:param ensure_ascii: escape non-ASCII characters
:param indent: the number of spaces or string to indent with
:param indent_depth: the depth up to which to indent
:param indent_leaves: indent leaf objects and arrays
:param mapping_types: an additional mapping type or tuple of additional
mapping types
:param max_indent_level: the level up to which to indent
:param quoted_keys: quote keys which are identifiers
:param separators: the item and key separator
:param seq_types: an additional sequence type or tuple of additional
Expand All @@ -330,9 +330,9 @@ def __init__(
end: str = "\n",
ensure_ascii: bool = False,
indent: int | str | None = None,
indent_depth: int | None = None,
indent_leaves: bool = False,
mapping_types: type | tuple[type, ...] = (),
max_indent_level: int | None = None,
quoted_keys: bool = True,
separators: tuple[str, str] = (", ", ": "),
seq_types: type | tuple[type, ...] = (),
Expand All @@ -350,12 +350,12 @@ def __init__(
if indent is not None and isinstance(indent, int):
indent *= " "

if indent_depth is None:
indent_depth = sys.maxsize
if max_indent_level is None:
max_indent_level = sys.maxsize

self._encoder: _EncodeFunc[object] = make_encoder(
indent, mapping_types, seq_types, end, item_separator,
long_item_separator, key_separator, indent_depth,
long_item_separator, key_separator, max_indent_level,
"nan_and_infinity" in allow, allow_surrogates, ensure_ascii,
indent_leaves, quoted_keys, sort_keys, commas and trailing_comma,
)
Expand Down
14 changes: 7 additions & 7 deletions src/jsonyx/_speedups.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct _PyEncoderObject {
PyObject *item_separator;
PyObject *long_item_separator;
PyObject *key_separator;
Py_ssize_t indent_depth;
Py_ssize_t max_indent_level;
int allow_nan_and_infinity;
int allow_surrogates;
int ensure_ascii;
Expand Down Expand Up @@ -1282,22 +1282,22 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"indent", "mapping_types", "seq_types", "end",
"item_separator", "long_item_separator",
"key_separator", "indent_depth",
"key_separator", "max_indent_level",
"allow_nan_and_infinity", "allow_surrogates",
"ensure_ascii", "indent_leaves", "quoted_keys",
"sort_keys", "trailing_comma", NULL};

PyEncoderObject *s;
PyObject *indent, *mapping_types, *seq_types;
PyObject *end, *item_separator, *long_item_separator, *key_separator;
Py_ssize_t indent_depth;
Py_ssize_t max_indent_level;
int allow_nan_and_infinity, allow_surrogates, ensure_ascii, indent_leaves;
int quoted_keys, sort_keys, trailing_comma;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOUUUUnppppppp:make_encoder", kwlist,
&indent, &mapping_types, &seq_types,
&end, &item_separator, &long_item_separator, &key_separator,
&indent_depth,
&max_indent_level,
&allow_nan_and_infinity, &allow_surrogates, &ensure_ascii,
&indent_leaves, &quoted_keys, &sort_keys, &trailing_comma))
return NULL;
Expand All @@ -1322,7 +1322,7 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
s->item_separator = item_separator;
s->long_item_separator = long_item_separator;
s->key_separator = key_separator;
s->indent_depth = indent_depth;
s->max_indent_level = max_indent_level;
s->allow_nan_and_infinity = allow_nan_and_infinity;
s->allow_surrogates = allow_surrogates;
s->ensure_ascii = ensure_ascii;
Expand Down Expand Up @@ -1726,7 +1726,7 @@ encoder_listencode_mapping(PyEncoderObject *s, PyObject *markers, _PyUnicodeWrit
goto bail;

int indented;
if (s->indent == Py_None || indent_level >= s->indent_depth) {
if (s->indent == Py_None || indent_level >= s->max_indent_level) {
indented = false;
}
else if (s->indent_leaves) {
Expand Down Expand Up @@ -1863,7 +1863,7 @@ encoder_listencode_sequence(PyEncoderObject *s, PyObject *markers, _PyUnicodeWri
goto bail;

int indented;
if (s->indent == Py_None || indent_level >= s->indent_depth) {
if (s->indent == Py_None || indent_level >= s->max_indent_level) {
indented = false;
}
else if (s->indent_leaves) {
Expand Down
12 changes: 6 additions & 6 deletions src/jsonyx/test/test_dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ def test_list_indent_leaves(
assert s == f"[\n{expected}1,\n{expected}2,\n{expected}3\n]"


def test_list_indent_depth(json: ModuleType) -> None:
"""Test list indent with indent_depth and indent_leaves."""
def test_list_max_indent_level(json: ModuleType) -> None:
"""Test list indent with max_indent_level and indent_leaves."""
assert json.dumps(
[[1, 2, 3]], end="", indent=1, indent_depth=1, indent_leaves=True,
[[1, 2, 3]], end="", indent=1, indent_leaves=True, max_indent_level=1,
) == "[\n [1, 2, 3]\n]"


Expand Down Expand Up @@ -418,11 +418,11 @@ def test_dict_indent_leaves(
)


def test_dict_indent_depth(json: ModuleType) -> None:
"""Test dict indent with indent_depth and indent_leaves."""
def test_dict_max_indent_level(json: ModuleType) -> None:
"""Test dict indent with max_indent_level and indent_leaves."""
obj: dict[str, object] = {"": {"a": 1, "b": 2, "c": 3}}
assert json.dumps(
obj, end="", indent=1, indent_depth=1, indent_leaves=True,
obj, end="", indent=1, indent_leaves=True, max_indent_level=1,
) == '{\n "": {"a": 1, "b": 2, "c": 3}\n}'


Expand Down

0 comments on commit b31f897

Please sign in to comment.