DictDumper package

Module Contents

Stream formatted output dumper.

dictdumper is an open source Python program works as a stream formatted output dumper. Currently, it supports following formats:

  • Dumper

    Abstract base class of all dumpers.

  • JSON

    Dump JavaScript object notation (JSON) format file.

  • PLIST

    Dump Apple property list (PLIST) format file.

  • Tree

    Dump tree-view text (TXT) format file.

  • XML

    Dump extensible markup language (XML) file; this is an abstract base class

  • VueJS

    Dump JavaScript file using Vue.js framework; this class is deprecated due to grammar error.

    Deprecated since version 0.8.0.

class dictdumper.JSON(fname, **kwargs)

Bases: dictdumper.dumper.Dumper

Dump JavaScript object notation (JSON) format file.

>>> dumper = JSON(file_name)
>>> dumper(content_dict_1, name=content_name_1)
>>> dumper(content_dict_2, name=content_name_2)
............
Variables
  • _file (str) – output file name

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

  • _tctr (int) – tab level counter

  • _hsrt (str) – _HEADER_START

  • _hend (str) – _HEADER_END

  • _vctr (DefaultDict[int, int]) – value counter dict

Note

Terminology:

object    ::=  "{}" | ("{" members "}")
members   ::=  pair | (pair "," members)
pair      ::=  string ":" value
array     ::=  "[]" | ("[" elements "]")
elements  ::=  value | (value "," elements)
value     ::=  string | number | object
                | array | true | false | null
property kind

str: File format of current dumper.

__init__(fname, **kwargs)

Initialise dumper.

Parameters
  • fname (str) – output file name

  • **kwargs – addition keyword arguments for initialisation

_encode_value(o)

Check content type 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 JSON serialisable data.

_append_value(value, file, name)

Call this function to write contents.

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

  • file (file object) – output file

  • name (str) – name of current content block

_append_object(value, file)

Call this function to write object contents.

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

  • file (file object) – output file

_append_array(value, file)

Call this function to write array contents.

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

  • file (file object) – output file

_append_string(value, file)

Call this function to write string contents.

Parameters
  • value (str) – content to be dumped

  • file (file object) – output file

_append_date(value, file)

Call this function to write date contents.

Parameters
  • value (Union[datetime.date, datetime.datetime, datetime.time]) – content to be dumped

  • file (file object) – output file

_append_number(value, file)

Call this function to write number contents.

Parameters
  • value (Union[int, float]) – content to be dumped

  • file (file object) – output file

_append_bool(value, file)

Call this function to write bool contents.

Parameters
  • value (bool) – content to be dumped

  • file (file object) – output file

_append_null(value, file)

Call this function to write null contents.

Parameters
  • value (None) – content to be dumped

  • file (file object) – output file

class dictdumper.PLIST(fname, **kwargs)

Bases: dictdumper.xml.XML

Dump Apple property list (PLIST) format file.

>>> dumper = PLIST(file_name)
>>> dumper(content_dict_1, name=content_name_1)
>>> dumper(content_dict_2, name=content_name_2)
............
Variables
  • _file (str) – output file name

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

  • _tctr (int) – tab level counter

  • _hsrt (str) – start string (_HEADER_START)

  • _hend (str) – end string (_HEADER_END)

Note

Terminology:

value    ::=  array | dict | string | data
                | date | integer | real | bool
array    ::=  "<array>" value* "</array>"
dict     ::=  "<dict>" ("<key>" str "</key>" value)* "</dict>"
string   ::=  "<string>" str "</string>"
data     ::=  "<data>" bytes "</data>"
date     ::=  "<date>" datetime "</date>"
integer  ::=  "<integer>" int "</integer>"
real     ::=  "<real>" float "</real>"
bool     ::=  "<true/>" | "<false/>"
property kind

str: File format of current dumper.

_encode_value(o)

Check content type 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, None, memoryview, tuple, set, frozenset to PLIST serialisable data.

_append_value(value, file, name)

Call this function to write contents.

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

  • file (file object) – output file

  • name (str) – name of current content block

_append_dict(value, file)

Call this function to write dict contents.

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

  • file (file object) – output file

_append_array(value, file)

Call this function to write array contents.

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

  • file (file object) – output file

_append_string(value, file)

Call this function to write string contents.

Parameters
  • value (str) – content to be dumped

  • file (file object) – output file

_append_data(value, file)

Call this function to write data contents.

Parameters
  • value (bytes) – content to be dumped

  • file (file object) – output file

_append_date(value, file)

Call this function to write date contents.

Parameters
  • value (Union[datetime.date, datetime.datetime]) – content to be dumped

  • file (file object) – output file

_append_integer(value, file)

Call this function to write integer contents.

Parameters
  • value (int) – content to be dumped

  • file (file object) – output file

_append_real(value, file)

Call this function to write real contents.

Parameters
  • value (float) – content to be dumped

  • file (file object) – output file

_append_bool(value, file)

Call this function to write bool contents.

Parameters
  • value (bool) – content to be dumped

  • file (file object) – output file

class dictdumper.Tree(fname, **kwargs)

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, file pointer) – indicates start of appending point

  • _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
property kind

str: File format of current dumper.

static check_newline(value)

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)

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)

Call this function to write contents.

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

  • file (file object) – output file

  • name (str) – name of current content block

_append_branch(value, file)

Call this function to write branch contents.

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

  • file (file object) – output file

_append_array(value, file)

Call this function to write array contents.

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

  • file (file object) – output file

_append_string(value, file)

Call this function to write string contents.

Parameters
  • value (str) – content to be dumped

  • file (file object) – output file

_append_bytes(value, file)

Call this function to write bytes contents.

Parameters
  • value (bytes) – content to be dumped

  • file (file object) – output file

_append_date(value, file)

Call this function to write date contents.

Parameters
  • value (Union[datetime.date, datetime.datetime, datetime.time]) – content to be dumped

  • file (file object) – output file

_append_number(value, file)

Call this function to write number contents.

Parameters
  • value (Union[int, float, complex]) – content to be dumped

  • file (file object) – output file

_append_bool(value, file)

Call this function to write bool contents.

Parameters
  • value (bool) – content to be dumped

  • file (file object) – output file

_append_none(value, file)

Call this function to write none contents.

Parameters
  • value (None) – content to be dumped

  • file (file object) – output file