4. CUtils module

class PythonExtensionsCollection.Utils.CUtils.CTypePrint[source]

Bases: object

TypePrint(oData=None, bHexFormat=False)[source]
PythonExtensionsCollection.Utils.CUtils.PrettyPrint(oData=None, hOutputFile=None, bToConsole=True, nIndent=0, sPrefix=None, bHexFormat=False)[source]

Function:

PrettyPrint

Wrapper function to create and use a CTypePrint object. This wrapper function is responsible for printing out the content to console and to a file (depending on input parameter).

The content itself is prepared by the method TypePrint of class CTypePrint. This happens PrettyPrint internally.

The idea behind the PrettyPrint function is to resolve also the content of composite data types and provide for every parameter inside:

  • the type

  • the total number of elements inside (e.g. the number of keys inside a dictionary)

  • the counter number of the current element

  • the value

Example call:

PrettyPrint(oData) (with oData is a Python variable of any type)

The output can e.g. look like this:

[DICT] (3/1) > {K1} [STR]  :  'Val1'
[DICT] (3/2) > {K2} [LIST] (4/1) > [INT]  :  1
[DICT] (3/2) > {K2} [LIST] (4/2) > [STR]  :  'A'
[DICT] (3/2) > {K2} [LIST] (4/3) > [INT]  :  2
[DICT] (3/2) > {K2} [LIST] (4/4) > [TUPLE] (2/1) > [INT]  :  9
[DICT] (3/2) > {K2} [LIST] (4/4) > [TUPLE] (2/2) > [STR]  :  'Z'
[DICT] (3/3) > {K3} [INT]  :  5

Every line of output has to be interpreted strictly from left to right.

For example the meaning of the fifth line of output

[DICT] (3/2) > {K2} [LIST] (4/4) > [TUPLE] (2/1) > [INT]  :  9

is:

  • The type of input parameter (oData) is dict

  • The dictionary contains 3 keys

  • The current line gives information about the second key of the dictionary

  • The name of the second key is ‘K2’

  • The value of the second key is of type list

  • The list contains 4 elements

  • The current line gives information about the fourth element of the list

  • The fourth element of the list is of type tuple

  • The tuple contains 2 elements

  • The current line gives information about the first element of the tuple

  • The first element of the tuple is of type int and has the value 9

Types are encapsulated in square brackets, counter in round brackets and key names are encapsulated in curly brackets.

Args:

oData (any Python data type)

A variable of any Python data type.

hOutputFile (handle to a file opened for writing or appending; optional; default None)

If handle is not None the content is written to this file, otherwise not.

bToConsole (bool; optional; default: True)

If True the content is written to console, otherwise not.

nIndent (int; optional; default: 0)

Sets the number of additional blanks at the beginning of every line of output (indentation).

sPrefix (str; optional; default: None)

Sets a prefix string that is added at the beginning of every line of output.

bHexFormat (bool; optional; default: False)

If True the output is printed in hexadecimal format (but strings only).

Returns:

listOutLines (list)

List of lines containing the prepared output