Skip to content

Pretty print Python objects with style in full color and proper indentation. Port of Ruby's Awesome Print.

License

Notifications You must be signed in to change notification settings

tkbky/amazing_printer

Repository files navigation

Amazing Printer

Build Status codecov image

It's an attempt to port the Ruby's awesome_print to Python.

the-pitch

Install

pip install amazing-printer

Usage

from amazing_printer import ap

ap(object, options = {})

# Options:
# sort_keys - boolean, set to sort the dictionary keys, default is False.
# indent - integer, set the width of indentation, default is 4 spaces.
# multiple_lines - boolean, set to print the fields for dict or list in multiple lines, default is True.
# color - dict, set the color for different literals, default is
# {
#     'str': 'yellow',
#     'list': 'blue',
#     'tuple': 'green',
#     'none': 'red',
# }
# supported colors: 'yellow', 'blue', 'green', 'red', 'black', 'magenta', 'cyan', 'white'

Global Configuration

There are 2 ways to configure amazing printer globally.

  1. Inside the code.

    from amazing_printer import configure
    
    options = {
        "indent": 8,
    }
    
    configure(options)
  2. From a config file.

    Name the config file as .amazing_printer.yml and place it inside the directory the python console is invoked or just your home directory (~).

    Example of the config file.

    sort_keys: false
    indent: 4
    index: true
    multiple_lines: true
    color:
      str: "red"
      list: "green"
      tuple: "blue"
      none: "yellow"

Contributing

  1. Fork & clone the repository
$ git clone [email protected]:<your-github-handle>/amazing_printer.git
$ cd amazing_printer
  1. Create a virtual environment and activate it' This project uses Pipenv. Follow the instruction here to install.
$ pipenv shell --three
  1. Install dependencies
# Install dependencies for development purpose.
$ pipenv install --dev
  1. Make your awesome changes

  2. Run test and coverage

# Run test
(env) $ pytest
# Run test with coverage report
(env) $ pytest --cov=amazing_printer amazing_printer/tests/
# Run specific test
(env) $ pytest -k "TestFormatter"
  1. Run test for multiple environments Make sure you have installed multiple versions of Python. Recommended way of managing Python version using Pyenv
(env) $ tox
  1. Make your pull request

License

Copyright (c) 2019 Kher Yee, Ting

Released under the MIT license. See LICENSE file for details.