Skip to main content

Tiny python debugging utility like node.js debug module

Project description

py-ndebug

Tiny python debugging utility modeled after visionmedia's node.js debug module and the python 3 print function. This is started off as a branch of https://github.com/mjgil/pydebug

Installation

$ pip install ndebug

Usage

With create you simply invoke the exported function to generate your debug function, passing it a name which will determine if a noop function is returned, or custom decorated function that emulates the python 3 print function but, by default, prints to sys.stderr. A unique color is selected per-function for visibility.

Example test.py:

import ndebug
import time

# 
# ndebug.create(name)
#
# once you have initialized the debug module you can call it as shown below
#
# debug(*objects, sep=' ', end='\n', file=sys.stderr, flush=False)
#

debug = ndebug.create("test")
debug("hi there2")

debug2 = ndebug.create("test2")
debug2("awesome")

debug({"awesome": True}, True, 1, "test")
debug([], {1,2,3}, {"awesome": True}, True, 1, "test")

time.sleep(0.1)
debug('should be milliseconds now')

time.sleep(1)
debug('should be seconds now')

The DEBUG environment variable is then used to enable these based on space or comma-delimited names. Here are some examples:

$ DEBUG=* python test_ndebug.py
$ DEBUG=test python test_ndebug.py
$ DEBUG=test2 python test_ndebug.py
$ DEBUG=test* python test_ndebug.py

Microsecond diff

When actively developing an application it can be useful to see when the time spent between one debug() call and the next. Suppose for example you invoke debug() before requesting a resource, and after as well, the "+NNNus" will show you how much time was spent between calls.

When File is not a TTY

When the file is not a TTY, to_utc_string() is called which mimicks the default behavior of the Javascript Date#toUTCString() method used in the node.js debug module, making it more useful for logging the debug information as shown below

$ DEBUG=* python test_ndebug.py 2>&1 | grep test
$ DEBUG=* python test_ndebug.py 2>&1 | grep test2

Conventions

If you're using this in one or more of your libraries, you should use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you should prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser".

Wildcards

The "*" character may be used as a wildcard. Suppose for example your library has debuggers named "connect:bodyParser", "connect:compress", "connect:session", instead of listing all three with DEBUG=connect:bodyParser,connect.compress,connect:session, you may simply do DEBUG=connect:*, or to run everything using this module simply use DEBUG=*.

You can also exclude specific debuggers by prefixing them with a "-" character or whitespace. For example:

$ DEBUG=examples:*,-examples:test1 python -m examples

License

The MIT License (MIT)

Copyright (c) 2013 Malcom Gilbert Copyright (c) 2019 Peter Magnusson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

ndebug-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

ndebug-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file ndebug-0.1.0.tar.gz.

File metadata

  • Download URL: ndebug-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for ndebug-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac5b75810b80f9504f30068523533e35a99b004ec02ad209e8ef3a3088601471
MD5 7cdb1ce5954c5ab71460674a342b4661
BLAKE2b-256 3bab16a3c7b8bec439a16311b126b5d4579bcaf0c4cb3b11a1d1c0c03ed6071c

See more details on using hashes here.

File details

Details for the file ndebug-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ndebug-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for ndebug-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d7a9ead463cc9be2fdf3f2da95d51bf17d1db578fb9cc26cd16af5976351c71
MD5 3f44872392f9a94a0b395d7af6a1a5f9
BLAKE2b-256 256356cd614640c5b3fbf529d2d05c025ac4d8ac4ce79305d5457bbf92710bc3

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