Skip to main content

An easy way to make beautiful prints of nested data in python

Project description

print-nested-data-python

Beautiful prints for nested data (iterables) in python.

How to use it?

Installation

Install it using pip install beautiful-prints

Usage

You can either use beautifulPrint to directly write the formatted string to the current system output (console) or generateBeautifulString to get the formatted string and mess around with it:

beautifullyFormatedString = generateBeautifulString(yourNestedData)

Output it directly:

beautifulPrint(yourNestedData)

Both functions have the optional parameters maxItemsPerLine and indent. The former to set the maximum items per line (default = 5) and the latter to set the indent (default = 4).

What's the concept?

The concept is to print nested data in a json like format, but different ;D

A quick example:

nestedData = {"key1": [1, 2, 3, 4], "key2": [1, 2, 3, 4]}

beautiful output using beautifulPrint:

{
    "key1": [1, 2, 3, 4], 
    "key2": [1, 2, 3, 4]
}

Now this isn't quite json like, right? Json formatted using json.dumps, it would look like this:

{
    "key1": [
        1, 
        2, 
        3, 
        4
    ], 
    "key2": [
        1, 
        2, 
        3, 
        4
    ]
}

Yes. This is because the items from the nested data are only printed into the next line if either of their sub data is equally a nested data type or the length of it exceeds the maximum items count per line.

Currently supported nested data types

  • lists
  • tuples
  • dicts
  • sets
  • frozensets

Note: There won't be raised an error if the used data type is not listed. It'll work, but it will probably be ugly. Additionally, any not iterable sub data type such as numpy.int64 is supported.

Examples

An example using a Dict containing strings as keys and lists out of tuples, sets and frozensets as values

exampleDict = {
    "key1": [
        [
            ("key_1_list1_tuple1_1", "key_1_list1_tuple1_2"),
            {"key_1_list1_tuple2_1", "key_1_list1_tuple2_2"},  # Here we have a set ;)
            frozenset({"key_1_list1_tuple3_1", "key_1_list1_tuple3_2"}),  # And here a frozenset ;)
            ("key_1_list1_tuple4_1", "key_1_list1_tuple4_2"),
            ("key_1_list1_tuple5_1", "key_1_list1_tuple5_2")
        ],
        [
            ("key_1_list2_tuple1_1", "key_1_list2_tuple1_2"),
            ("key_1_list2_tuple2_1", "key_1_list2_tuple2_2"),
            ("key_1_list2_tuple3_1", "key_1_list2_tuple3_2"),
            ("key_1_list2_tuple4_1", "key_1_list2_tuple4_2"),
            ("key_1_list2_tuple5_1", "key_1_list2_tuple5_2")
        ]
    ],
    "key2": [
        (
            "key_2_list1_tuple1_1",
            "key_2_list1_tuple1_2",
            "key_2_list1_tuple1_3",
            "key_2_list1_tuple1_4",
            "key_2_list1_tuple1_5",
            "key_2_list1_tuple1_6"
        ),
        ("key_2_list1_tuple2_1", "key_2_list1_tuple2_2"),
        ("key_2_list1_tuple3_1", "key_2_list1_tuple3_2"),
        ("key_2_list1_tuple4_1", "key_2_list1_tuple4_2"),
        ("key_2_list1_tuple5_1", "key_2_list1_tuple5_2")
    ]
}

Output without formatting:

{'key1': [[('key_1_list1_tuple1_1', 'key_1_list1_tuple1_2'), {'key_1_list1_tuple2_2', 'key_1_list1_tuple2_1'}, frozenset({'key_1_list1_tuple3_1', 'key_1_list1_tuple3_2'}), ('key_1_list1_tuple4_1', 'key_1_list1_tuple4_2'), ('key_1_list1_tuple5_1', 'key_1_list1_tuple5_2')], [('key_1_list2_tuple1_1', 'key_1_list2_tuple1_2'), ('key_1_list2_tuple2_1', 'key_1_list2_tuple2_2'), ('key_1_list2_tuple3_1', 'key_1_list2_tuple3_2'), ('key_1_list2_tuple4_1', 'key_1_list2_tuple4_2'), ('key_1_list2_tuple5_1', 'key_1_list2_tuple5_2')]], 'key2': [('key_2_list1_tuple1_1', 'key_2_list1_tuple1_2', 'key_2_list1_tuple1_3', 'key_2_list1_tuple1_4', 'key_2_list1_tuple1_5', 'key_2_list1_tuple1_6'), ('key_2_list1_tuple2_1', 'key_2_list1_tuple2_2'), ('key_2_list1_tuple3_1', 'key_2_list1_tuple3_2'), ('key_2_list1_tuple4_1', 'key_2_list1_tuple4_2'), ('key_2_list1_tuple5_1', 'key_2_list1_tuple5_2')]}

Output using beautifulPrint:

{
     "key1": [
        [
            ('key_1_list1_tuple1_1', 'key_1_list1_tuple1_2'),
            {'key_1_list1_tuple2_2', 'key_1_list1_tuple2_1'},
            frozenset({'key_1_list1_tuple3_1', 'key_1_list1_tuple3_2'}),
            ('key_1_list1_tuple4_1', 'key_1_list1_tuple4_2'),
            ('key_1_list1_tuple5_1', 'key_1_list1_tuple5_2')
        ],
        [
            ('key_1_list2_tuple1_1', 'key_1_list2_tuple1_2'),
            ('key_1_list2_tuple2_1', 'key_1_list2_tuple2_2'),
            ('key_1_list2_tuple3_1', 'key_1_list2_tuple3_2'),
            ('key_1_list2_tuple4_1', 'key_1_list2_tuple4_2'),
            ('key_1_list2_tuple5_1', 'key_1_list2_tuple5_2')
        ]
    ],
     "key2": [
        (
            "key_2_list1_tuple1_1",
            "key_2_list1_tuple1_2",
            "key_2_list1_tuple1_3",
            "key_2_list1_tuple1_4",
            "key_2_list1_tuple1_5",
            "key_2_list1_tuple1_6"
        ),
        ('key_2_list1_tuple2_1', 'key_2_list1_tuple2_2'),
        ('key_2_list1_tuple3_1', 'key_2_list1_tuple3_2'),
        ('key_2_list1_tuple4_1', 'key_2_list1_tuple4_2'),
        ('key_2_list1_tuple5_1', 'key_2_list1_tuple5_2')
    ]
}

Now, what about pprint? Well, see for your self:

{'key1': [[('key_1_list1_tuple1_1', 'key_1_list1_tuple1_2'),
           {'key_1_list1_tuple2_2', 'key_1_list1_tuple2_1'},
           frozenset({'key_1_list1_tuple3_1', 'key_1_list1_tuple3_2'}),
           ('key_1_list1_tuple4_1', 'key_1_list1_tuple4_2'),
           ('key_1_list1_tuple5_1', 'key_1_list1_tuple5_2')],
          [('key_1_list2_tuple1_1', 'key_1_list2_tuple1_2'),
           ('key_1_list2_tuple2_1', 'key_1_list2_tuple2_2'),
           ('key_1_list2_tuple3_1', 'key_1_list2_tuple3_2'),
           ('key_1_list2_tuple4_1', 'key_1_list2_tuple4_2'),
           ('key_1_list2_tuple5_1', 'key_1_list2_tuple5_2')]],
 'key2': [('key_2_list1_tuple1_1',
           'key_2_list1_tuple1_2',
           'key_2_list1_tuple1_3',
           'key_2_list1_tuple1_4',
           'key_2_list1_tuple1_5',
           'key_2_list1_tuple1_6'),
          ('key_2_list1_tuple2_1', 'key_2_list1_tuple2_2'),
          ('key_2_list1_tuple3_1', 'key_2_list1_tuple3_2'),
          ('key_2_list1_tuple4_1', 'key_2_list1_tuple4_2'),
          ('key_2_list1_tuple5_1', 'key_2_list1_tuple5_2')]}

Above is the output when using pprint from the pprint module without changing any default values and passing parameters. beautifulPrint simply works by default and is easy...

And what about json.dumps? Well if you don't need tuples or sets or frozensets or any 'special' data type such as numpy.int64 you can use it, though have a look under [A quick example](#A quick example:) and decide for yourself what you find more beautiful. ¯_(ツ)_/¯

'json.dumps' won't work with this example :(

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

beautiful-prints-0.1.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

beautiful_prints-0.1.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file beautiful-prints-0.1.3.tar.gz.

File metadata

  • Download URL: beautiful-prints-0.1.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.1

File hashes

Hashes for beautiful-prints-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5dd68490d92a411d7626aeb829cb32d1e55e130dc7aff7837148486adcf05aa1
MD5 a8cbcda5f2fbbe01c663dc3c10677cf4
BLAKE2b-256 cd4803ebdb99720ce455b67a7213a1107fd5a4de8c3569a3e867ae94353ef4c4

See more details on using hashes here.

File details

Details for the file beautiful_prints-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: beautiful_prints-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.1

File hashes

Hashes for beautiful_prints-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 592acccb4aca29cf1eda7b56dbda2fe5f421360a77924a60dd7601d37440940d
MD5 9e1ec07459ad355dbdce97018cb6a22b
BLAKE2b-256 0c2a9f3576fcae37afceb0d419980b2aa5ab6b582a708788bcbfca50db894afa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page