Skip to main content

arbol -- Arborescent Printouts in Python

Project description

arbol | Python Package for Arborescent Printouts in Python

s Do you have a script, a command line tool, or some workflow in Python that has lots of 'print' statements, and you can't make sense of it once it adds up to hundreds of lines on the console? Sounds familiar?
arbol organises your stdout prints in a hierarchy that follows the structure of your code. Use a simple context manager to define the hierarchy and the 'aprint' command instead of print, and voila. Finally, when the optional dependencies are installed, the printed tree and text are colored with an exquisitely crafted combination of colors, making it even more visually appealing.

If you are wondering, 'arbol' means 'tree' in spanish.

Why not use a more traditional Python logging? We have made the choice of sticking to a plain and simple scheme that matches the usage of 'print' statements.

Features

You can use 'aprint' replacement for the built-in 'print'. You create a new 'node' in the tree with the 'asection' contact manager. Moreover, arbol measures the elapsed time for each node of the tree and displays that conveniently. You have several configuration flags in the Arbol class to tune things. If yo want to capture print statements originating from 3rd party code, you can use the 'acapture' context manager to capture stdout (and stderr) -- tis is a bit experimental and is best used sparingly. The best documentation is simply the demo below...

Installation

Install with pip:

pip install arbol

Optional dependencies

If you want colors, install the ansicolors package:

pip install ansicolors

To make sure that you get the colors on all operating systems (particularly Windows), install the colorama package:

pip install colorama

Note: both colorama and ansicolors are optional -- arbol will work fine without it installed.

Example

Here is a simple and self-explanatory example:

from arbol import Arbol, aprint, section, asection, acapture

import arbol

# for colors, install the ansicolors package: 'pip install ansicolors',
# and for windows install the colorama package: 'pip install colorama'

# You can limit the tree depth:
Arbol.max_depth = 4

# use aprint (=arbol print) instead of the standard print
aprint('Test')

# You can decorate functions:
@section('function')
def fun(x):
    if x >= 0:
        with asection('recursive call to f'):
            aprint(f"f(x)+1={fun(x - 1)}")

# The context manager let's you go down one level in the tree
with asection('a section'):
    aprint('a line')
    aprint('another line')
    aprint('we are done \n or are we? \n someone gotta check!')

    with asection('a subsection'):
        aprint('another line')
        aprint('we are done')

    # works through function calls and the like...
    fun(2)

    # You can capture stdout if you want, usefull when a 3rd party library has printouts that you want to capture...
    with acapture():
        print("No escape is possible")
        aprint("Even this works...\n")
        # Don't push it.. sections will not work right now...

    # You can deactivate the elapsed time measurement and printing:
    Arbol.elapsed_time = False
    fun(100)

aprint('demo is finished...')

# You can also turn off all output with one switch:
Arbol.enable_output = False
aprint('you will not see that')

And this is how it looks like:

example

Roadmap

Some ideas we might consider from serious to highly speculative:

  • More color styles to choose from
  • Intercept stdout from C code so that printouts from libraries called from python are formatted too, unclear is that's possible.
  • Generate tree automatically by inspecting stack?
  • Interoperability with logging package?
  • How to handle multiple threads/processes? Right now, the printouts get inter;leaved stochastically which is messy and incomprehensible. One idea is to capture all outputs from each thread, hold onto these outputs until the thread is done (perhaps via a dedicated 'holding' context manager), and ouput it all in order and separately. Probably doable.

Contributions

Pull requests highly welcome!

Authors

Loic A. Royer (@loicaroyer) Ahmet Can Solak (@_ahmetcansolak)

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

arbol-2021.5.18.992.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

arbol-2021.5.18.992-py2.py3-none-any.whl (8.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file arbol-2021.5.18.992.tar.gz.

File metadata

  • Download URL: arbol-2021.5.18.992.tar.gz
  • Upload date:
  • Size: 9.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.4

File hashes

Hashes for arbol-2021.5.18.992.tar.gz
Algorithm Hash digest
SHA256 2f81ba748d80c1e04e02ea4107569055279ba70374b1ca2e38031b099c65f95d
MD5 baf79ea21948bbd2eee175d344dbc749
BLAKE2b-256 e4d487fd3a68fe86787281c3e2221beaa94148f4e4b34ee2c537dd07e2eabccd

See more details on using hashes here.

File details

Details for the file arbol-2021.5.18.992-py2.py3-none-any.whl.

File metadata

  • Download URL: arbol-2021.5.18.992-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 2, 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.4

File hashes

Hashes for arbol-2021.5.18.992-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4d848336480086738c7360a814140c52aba442a3c92957675b33981462254516
MD5 c96c07b560fd05b9d2a9e3fc7a8428df
BLAKE2b-256 1dc6a1c8271d1403bed2c5db7d79b798e944f82ecb64e1d32bdebc85ba35a8ed

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