Skip to main content

Logger and object inspector for Python

Project description

Python PyPI version

vb-console

A custom Python object inspector with style.

Installation and Usage

$ pip install vb-console

By default, console output is disabled. You can enable output via setting the ENABLE_CONSOLE environment variable. Launch Python reply:

$ ENABLE_CONSOLE=1 python

then;

Python 3.7.3 (default, Jul  6 2019, 07:47:04) 
[Clang 11.0.0 (clang-1100.0.20.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from console import console
>>> console = console(source=__name__) 
>>> console('Hello World')
[__main__]*****************************************************************************
('Hello World',)
***************************************************************************************

>>> console.dir([1,2,3])
[__main__ : instance of list | <class 'list'>]*****************************************
{   'internal_methods': [   '__add__', '__class__', '__contains__', '__delattr__', 
                            '__delitem__', '__dir__', '__doc__', '__eq__', '__format__',
                            '__ge__', '__getattribute__', '__getitem__', '__gt__', 
                            '__hash__', '__iadd__', '__imul__', '__init__', 
                            '__init_subclass__', '__iter__', '__le__', '__len__', 
                            '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', 
                            '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', 
                            '__setattr__', '__setitem__', '__sizeof__', '__str__', 
                            '__subclasshook__'],
    'public_attributes': [  'append', 'clear', 'copy', 'count', 'extend', 'index', 
                            'insert', 'pop', 'remove', 'reverse', 'sort']}
***************************************************************************************

You have few options for console output:

  • console(object)
  • console(object, object, object, ...)
  • console.dir(object)

You can custom console instance;

from console import console

# default/basic
console = console(source=__name__)

# set screen width and indentation
console = console(
    source=__name__,
    indent=8,
    width=64,
)

# set color mode
console = console(
    source=__name__,
    color='yellow',
)

# inline color
console('hello', 'world', [1, 2, 3], color='red')

Valid colors are:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • default

Defaults are;

  • width is 79
  • indent is 4
  • seperator_char is *
  • color is yellow

Example of complex object inspection:

class MyClass:
    klass_var1 = 1
    klass_var2 = 2

    def __init__(self):
        self.name = 'Name'

    def start(self):
        return 'method'

    @property
    def admin(self):
        return True

    @staticmethod
    def statik():
        return 'Static'

    @classmethod
    def klass_method(cls):
        return 'kls'

mc = MyClass()

console.dir(MyClass)

Output:

[__main__ : MyClass | <class 'type'>]*******************************************
{   'class_methods': ['klass_method'],
    'internal_methods': [   '__class__', '__delattr__', '__dict__', '__dir__',
                            '__doc__', '__eq__', '__format__', '__ge__',
                            '__getattribute__', '__gt__', '__hash__',
                            '__init_subclass__', '__le__', '__lt__',
                            '__module__', '__ne__', '__new__', '__reduce__',
                            '__reduce_ex__', '__repr__', '__setattr__',
                            '__sizeof__', '__str__', '__subclasshook__',
                            '__weakref__'],
    'property_list': ['admin'],
    'public_attributes': ['klass_var1', 'klass_var2'],
    'public_methods': ['__init__', 'start'],
    'static_methods': ['statik']}
*******************************************************************************

More;

console.dir(mc)

Output:

In [6]: console.dir(mc)                                                         
[__main__ : instance of MyClass | <class '__main__.MyClass'>]*******************
{   'instance_attributes': ['name'],
    'internal_methods': [   '__class__', '__delattr__', '__dict__', '__dir__',
                            '__doc__', '__eq__', '__format__', '__ge__',
                            '__getattribute__', '__gt__', '__hash__',
                            '__init__', '__init_subclass__', '__le__', '__lt__',
                            '__module__', '__ne__', '__new__', '__reduce__',
                            '__reduce_ex__', '__repr__', '__setattr__',
                            '__sizeof__', '__str__', '__subclasshook__',
                            '__weakref__'],
    'public_attributes': [   'admin', 'klass_method', 'klass_var1',
                             'klass_var2', 'start', 'statik']}
********************************************************************************

Standard objects:

console.dir(dict)

Output:

[__main__ : dict | <class 'type'>]**********************************************
{   'internal_methods': [   '__class__', '__contains__', '__delattr__',
                            '__delitem__', '__dir__', '__doc__', '__eq__',
                            '__format__', '__ge__', '__getattribute__',
                            '__getitem__', '__gt__', '__hash__', '__init__',
                            '__init_subclass__', '__iter__', '__le__',
                            '__len__', '__lt__', '__ne__', '__new__',
                            '__reduce__', '__reduce_ex__', '__repr__',
                            '__setattr__', '__setitem__', '__sizeof__',
                            '__str__', '__subclasshook__'],
    'public_attributes': [   'clear', 'copy', 'fromkeys', 'get', 'items',
                             'keys', 'pop', 'popitem', 'setdefault', 'update',
                             'values']}
********************************************************************************

Using with Django

console also checks for Django’s DEBUG settings value: settings.DEBUG. Example usage for Django project;

from django.views.generic.base import TemplateView

from console import console

console = console(source=__name__)


class IndexView(TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        kwargs = super().get_context_data(**kwargs)
        console('kwargs', kwargs)
        console.log(kwargs)
        return kwargs

License

This project is licensed under MIT


Contributer(s)


Contribute

All PR’s are welcome!

  1. fork (https://github.com/vbyazilim/django-vb-console/fork)
  2. Create your branch (git checkout -b my-features)
  3. commit yours (git commit -am 'added killer options')
  4. push your branch (git push origin my-features)
  5. Than create a new Pull Request!

Change Log

2019-10-03

  • Fix some of the code
  • Add ENABLE_CONSOLE environment variable
  • Update README

2019-09-29

  • Init repo!

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

vb-console-0.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

vb_console-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file vb-console-0.1.1.tar.gz.

File metadata

  • Download URL: vb-console-0.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for vb-console-0.1.1.tar.gz
Algorithm Hash digest
SHA256 be97959fbb8b77f547793fac61a143027d9b1f42ab4567e67f52cd8da496e259
MD5 574bae63e15961de6a3abe996de614d0
BLAKE2b-256 fcdd2b60ab47f7181452ba9a9fd774ba434a3823cf69f0a57d2479405ddf8405

See more details on using hashes here.

File details

Details for the file vb_console-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: vb_console-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for vb_console-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d65108826fedf87cf02715a18670426fd859a03049f74e2a62bebdec0e52bf6
MD5 c764ad68837f0e3708fc70b5093b0369
BLAKE2b-256 625cc1318d6fd48e3c7748ca741fff6764cb52e9a39375dcf0f3e47b48968f9a

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