Skip to main content

Form a dictionary to a convenient shape for output or writing to a file.

Project description

DictShaper

The module for convenient viewing of dictionary with the all necessary indents.

This module extends the standard dict class, so you can use all its properties and methods. Over all of this DictShaper adds the new method shape(). You can also give a name for your dictionary by the name= param.

your_dictionary.shape(name='any_name')

You can also add a path to a file for writing the dictionary there, using the write_to= param. It will be writing in a convenient view, like in an example below.

your_dictionary.shape(name='any_name', write_to='any_path')

If you set as a value 1 or True in write_to= param then the dictionary will be writing to the end of a current file.

EXAMPLES

We will work with the dictionary below called 'some dictionary'.

some_dictionary = {'Level-1 el-1': [0, 1, 2, 3, 4], 'Level-1 el-2': {'Level-2 el-1': 1, 'Level-2 el-2': 2}, 'Level-1 el-3': 'Some string', 'Level-1 el-4': ('Tuple', 1, ['a', 'b']), 'Level-1 el-5': {'Level-2 el-3': {'Level-3 el-1': 'https://some-site.com/page1?par=120&another=500', 'Level-3 el-2': (9, 125, 87), 'Level-3 el-3': 'Very very very very very very very very very very long string.'}, 'Level-2 el-4': 2}, 'Level-1 el-6': {}, 'Level-1 el-7': 'The end of the dictionary!'}

First you need to import this module with the following command

from dictshaper.shaper import DictShaper

For outputting the dict without a name to a console

Enter following commands

  1. some_dictionary = DictShaper(some_dictionary)
  2. print(some_dictionary.shape())
  • You also can add the 'name=' param

Output

{
    'Level-1 el-1': [0, 1, 2, 3, 4],
    'Level-1 el-2': {
        'Level-2 el-1': 1,
        'Level-2 el-2': 2,
    },
    'Level-1 el-3': 'Some string',
    'Level-1 el-4': ('Tuple', 1, ['a', 'b']),
    'Level-1 el-5': {
        'Level-2 el-3': {
            'Level-3 el-1': 'https://some-site.com/page1?par=120&another=500',
            'Level-3 el-2': (9, 125, 87),
            'Level-3 el-3': 'Very very very very very very very very very very long string.',
        },
        'Level-2 el-4': 2,
    },
    'Level-1 el-6': {},
    'Level-1 el-7': 'The end of the dictionary!',
}

For writing the dict with a name to a file

  1. some_dictionary = DictShaper(some_dictionary)
  2. some_dictionary.shape(name='shaped_dict', write_to=True)
  3. shaped_dict = {
  4.     'Level-1 el-1': [0, 1, 2, 3, 4],
  5.     'Level-1 el-2': {
  6.         'Level-2 el-1': 1,
  7.         'Level-2 el-2': 2,
  8.     },
  9.     'Level-1 el-3': 'Some string',
  10.     'Level-1 el-4': ('Tuple', 1, ['a', 'b']),
  11.     'Level-1 el-5': {
  12.         'Level-2 el-3': {
  13.             'Level-3 el-1': 'https://some-site.com/page1?par=120&another=500',
  14.             'Level-3 el-2': (9, 125, 87),
  15.             'Level-3 el-3': 'Very very very very very very very very very very long string.',
  16.         },
  17.         'Level-2 el-4': 2,
  18.     },
  19.     'Level-1 el-6': {},
  20.     'Level-1 el-7': 'The end of the dictionary!',
  21. }

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

dictshaper-0.0.4.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

dictshaper-0.0.4-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

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