Skip to content

Commit

Permalink
Moving pp to be a FieldType method.
Browse files Browse the repository at this point in the history
Makes much more sense here as it already works for other types.
  • Loading branch information
scott-griffiths committed Nov 1, 2024
1 parent 67351a3 commit aab638b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
18 changes: 17 additions & 1 deletion bitformat/_fieldtype.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
"""
Expand Down
14 changes: 0 additions & 14 deletions bitformat/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 1 addition & 1 deletion bitformat/_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(')')
Expand Down

0 comments on commit aab638b

Please sign in to comment.