Last active
December 26, 2022 00:25
-
-
Save Julian-Nash/83dc19ecfc698ae07d1b5fd73fba4541 to your computer and use it in GitHub Desktop.
Print colored objects in the console depending on their type. Useful for development & debugging requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
import json | |
def cprint(x=None): | |
""" | |
Pass any object into cprint to have it printed to the console in color! | |
json = yellow (json is pretty printed by default) | |
Python collections [list, dict, tuple] = green | |
Integers & floats = magenta | |
Other = red | |
:param x: | |
:return: None | |
""" | |
if x == None: | |
click.secho("No data was passed to cprint", fg="red", bold=True) | |
return None | |
# json | |
if type(x) == str: | |
try: | |
json_test = json.loads(x) | |
pretty_json = json.dumps(json_test, indent=2) | |
click.secho('\n{}'.format(pretty_json), fg='yellow', bold=True) | |
except ValueError: | |
click.secho('\n{}'.format(x), fg='cyan', bold=True) | |
# list | |
elif type(x) == list: | |
click.secho('\n{}'.format(x), fg='green', bold=True) | |
# dict | |
elif type(x) == dict: | |
click.secho('\n{}'.format(x), fg='green', bold=True) | |
# tuple | |
elif type(x) == tuple: | |
click.secho('\n{}'.format(x), fg='green', bold=True) | |
# int | |
elif type(x) == int: | |
click.secho('\n{}'.format(x), fg='magenta', bold=True) | |
# float | |
elif type(x) == float: | |
click.secho('\n{}'.format(x), fg='magenta', bold=True) | |
# other | |
else: | |
click.secho('\n{}'.format(x), fg='red', bold=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Console color printer
Quick & dirty function I've been using in Flask development.
Print colored objects in the console depending on their type. Useful for development & debugging requests.
Uses click by Armin Ronacher -> @mitsuhiko