Tree-View Dumper

dictdumper.tree contains Tree only, which dumpers a tree-view text (TXT) format file. Usage sample is described as below.

>>> dumper = Tree(filename)
>>> dumper(content_dict_1, name=contentname_1)
>>> dumper(content_dict_2, name=contentname_2)
............

Dumper class

class dictdumper.tree.Tree(fname, **kwargs)[source]

Bases: dictdumper.dumper.Dumper

Dump a tree-view text (TXT) format file.

>>> dumper = Tree(filename)
>>> dumper(content_dict_1, name=contentname_1)
>>> dumper(content_dict_2, name=contentname_2)
............
Variables
  • _file (str) – output file name

  • _sptr (int) – indicates start of appending point (file pointer)

  • _tctr (int) – tab level counter

  • _hsrt (str) – start string (_HEADER_START)

  • _hend (str) – end string (_HEADER_END)

  • _bctx (List[str]) – blank branch (indentation) context record

  • _nctr (int) – branch number counter

Note

Terminology:

value   ::=  branch | array | string | number | bool | N/A
string
|-- string
|     |-- string -> value
|     |-- string
|     |     |-- string -> value
|     |     |-- string -> value
|     |-- string -> value
|     |-- string -> value
|           |-- string -> value
|           |-- string -> value
|-- string -> value, value, value
|-- string -> True
|-- string -> False
|-- string -> N/A
|-- string -> value
|-- string -> value
__type__ = ((<class 'str'>, 'string'), (<class 'bool'>, 'bool'), (<class 'dict'>, 'branch'), (<class 'NoneType'>, 'none'), (<class 'datetime.date'>, 'date'), (<class 'datetime.datetime'>, 'date'), (<class 'datetime.time'>, 'date'), (<class 'int'>, 'number'), (<class 'float'>, 'number'), (<class 'complex'>, 'number'), (<class 'bytes'>, 'bytes'), (<class 'list'>, 'array'))

Type codes.

Type

Tuple[Tuple[type, str]]

_tctr = 1

Tab level counter.

Type

int

Tab level counter.

Type

int

_hsrt = ''

Tree-view head string.

_hend = ''

Tree-view tail string.

_nctr = 0

Branch number counter.

Type

int

_bctx = []

Blank branch (indentation) context record.

Type

List[str]

property kind

File format of current dumper.

Return type

Literal[‘txt’]

static check_newline(value)[source]

Check if newline is needed.

Parameters

value (Union[Dict[str, Any], AnyStr]) – value to check if new line is needed

Returns

if newline is needed

Return type

bool

Notes

Newline is needed if

  1. value is a dict

  2. value is string (str) and its length is greater than 32 distinct characters

  3. value is bytestring (bytes) and the length of its hex representation is greater than 40 distinct characters

_encode_value(o)[source]

Convert content for function call.

Parameters

o (Any) – object to convert

Returns

the converted object

Return type

Any

See also

The function is a direct wrapper for object_hook().

Notes

The function will by default converts bytearray, memoryview, tuple, set, frozenset to tree-view represetable data.

_append_value(value, file, name)[source]

Call this function to write contents.

Parameters
  • value (Dict[str, Any]) – content to be dumped

  • file (io.TextIOWrapper) – output file

  • name (str) – name of current content block

_append_branch(value, file)[source]

Call this function to write branch contents.

Parameters
_append_array(value, file)[source]

Call this function to write array contents.

Parameters
  • value (List[Any]) – content to be dumped

  • file (io.TextIOWrapper) – output file

_append_string(value, file)[source]

Call this function to write string contents.

Parameters
_append_bytes(value, file)[source]

Call this function to write bytes contents.

Parameters
_append_date(value, file)[source]

Call this function to write date contents.

Parameters
_append_number(value, file)[source]

Call this function to write number contents.

Parameters
_append_bool(value, file)[source]

Call this function to write bool contents.

Parameters
_append_none(value, file)[source]

Call this function to write none contents.

Parameters

Internal utilities

dictdumper.tree.indent(ctx, branch=True)[source]

Indentation context.

Parameters
  • ctx (List[str]) – indentation context

  • branch (bool) – if True push the branch template (_TEMP_BRANCH) to the context, else push the spaces template (_TEMP_SPACES)

Yields

None – temporarily push a template to the context

dictdumper.tree._HEADER_START = ''

Tree-view head string.

dictdumper.tree._HEADER_END = ''

Tree-view tail string.

dictdumper.tree._TEMP_BRANCH = '  |   '

Branch template

dictdumper.tree._TEMP_SPACES = '      '

Spaces template