From aab638b08068c07c303bccf17b9b9d68c0050f0b Mon Sep 17 00:00:00 2001 From: Scott Griffiths Date: Fri, 1 Nov 2024 08:59:24 +0000 Subject: [PATCH] Moving pp to be a FieldType method. Makes much more sense here as it already works for other types. --- bitformat/_fieldtype.py | 18 +++++++++++++++++- bitformat/_format.py | 14 -------------- bitformat/_repeat.py | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bitformat/_fieldtype.py b/bitformat/_fieldtype.py index 37a69d8..05a9461 100644 --- a/bitformat/_fieldtype.py +++ b/bitformat/_fieldtype.py @@ -1,9 +1,11 @@ from __future__ import annotations import abc +import sys + from bitformat import Bits from ._bits import BitsType from ._common import final, Indenter -from typing import Any, Sequence +from typing import Any, Sequence, TextIO import keyword from ._options import Options @@ -103,6 +105,20 @@ def __repr__(self) -> str: """ return self._repr() + def pp(self, stream: TextIO = sys.stdout, indent: int | None = None, depth: int | None = None) -> None: + """ + Pretty-print the fieldtype to a stream (or stdout by default). + + :param stream: The stream to write to. + :type stream: TextIO + :param indent: The number of spaces for each level of indentation. Defaults to Options().indent_size which defaults to 4. + :type indent: int + :param depth: The maximum depth to print, or None for no limit. + :type depth: int or None + """ + stream.write(self._str(Indenter(indent_size=indent, max_depth=depth))) + stream.write('\n') + @classmethod def from_string(cls, s: str) -> FieldType: """ diff --git a/bitformat/_format.py b/bitformat/_format.py index 1597151..465a6a2 100644 --- a/bitformat/_format.py +++ b/bitformat/_format.py @@ -315,17 +315,3 @@ def extend(self, values: Iterable) -> None: """ for value in values: self.__iadd__(value) - - def pp(self, stream: TextIO = sys.stdout, indent: int | None = None, depth: int | None = None) -> None: - """ - Pretty-print the format to a stream. - - :param stream: The stream to write to. - :type stream: TextIO - :param indent: The number of spaces for each level of indentation. Defaults to Options().indent_size which defaults to 4. - :type indent: int - :param depth: The maximum depth to print, or None for no limit. - :type depth: int or None - """ - stream.write(self._str(Indenter(indent_size=indent, max_depth=depth))) - stream.write('\n') \ No newline at end of file diff --git a/bitformat/_repeat.py b/bitformat/_repeat.py index 5035053..2b0a05a 100644 --- a/bitformat/_repeat.py +++ b/bitformat/_repeat.py @@ -46,7 +46,7 @@ def from_string(cls, s: str) -> Repeat: def _str(self, indent: Indenter) -> str: # TODO: name is not handled yet. count_str = str(self.count) - s = indent(f"Repeat({count_str},") + s = indent(f"Repeat({count_str},\n") with indent: s += self.field._str(indent) s += indent(')')