Skip to main content

Basic diagnostic facilities, for Python

Project description

Diagnosticism.Python

License PyPI version versions Python package Last Commit

Diagnosticism library, for Python

Table of Contents

Introduction

Diagnosticism is a standalone library of simple components for aiding in diagnostics for Python projects. It contains versions of components seen in the other Diagnosticisms - see below - though there is not a 1-to-1 correspondence between any of them.

Installation & usage

Install via pip or pip3, as in:

$ pip3 install diagnosticism

Use via import:

import diagnosticism

When using the simple logging facilities, we find it convenient to import as follows:

import diagnosticism as d
import diagnosticism.severity as sev

that may then be used as:

d.log(sev.INFO, "hello")

Python version compatibility

Diagnosticism.Python is intended to run on Python 2.7 and Python 3.8+. GitHub Actions exercises Python 2.7 and Python 3.8–3.13.

Requirement Applies to
Python 2.7 or 3.8+ Core contingent reporting, logging, tracing, and severity APIs
Python 3.9+ @tracefunc and @asynctracefunc decorators

The public API surface is listed in diagnosticism.__all__.

Components

Diagnosticism.Python provides components in the following categories:

  • Contingent Reporting
  • Diagnostic Logging
  • Tracing

NOTE: for the moment, the Diagnostic Logging facilities emit to the standard error stream, via the Contingent Reporting API. In the near future this will be changed to work with more sophisticated logging libraries, including the standard logging facilities and the (as yet to be release) Pantheios.Python.

Classes

The following classes are defined:

DOOMGram

DOOMGram - Decimal Order-Of-Magnitude frequency histoGRAM - is a class for recording order-of-magnitude of operation timings, and then rendering the results in a text-based histogram format, as in:

DOOMScope

DOOMScope is a context-manager for use with DOOMGram for measuring scoped chunks of logic.

The example program examples/doomgram.py illustrates these two types in combination

# examples/doomgram.py

def main():

    dg = DOOMGram()

    for _ in range(1000):

        r = random.uniform(1, 1000)

        d = r / 1_000_000

        with DOOMScope(dg):

            time.sleep(d)

    print("dg:", dg)

. . .

and produces output such as:

dg: ___abcc_____

which indicates the following breakdown of operation - in this case a random sleep() - timings:

Time period(s) Number of events in that period
Nanoseconds no records
Microseconds 1-9 in 1µs (a); 10-99 in 10µs (b); 100-899 in 100µs (c)
Milliseconds 100-999 in 1ms (c)
Seconds no records

Constants

Diagnostic Logging API

The following constants are defined:

Constant API Purpose
STOCK_TRAILING_PROMPT Contingent Reporting The stock trailing prompt, which is defined as "use --help for usage".

Contingent Reporting API

The following constants are defined:

Constant API Purpose
VIOLATION Diagnostic Logging Severity level suitable for use when logging that a design violation has occurred.
ALERT Diagnostic Logging Severity level suitable for use when logging that a fatal program failure has occurred.
CRITICAL Diagnostic Logging Severity level suitable for use when logging that a critical failure has occurred.
FAILURE Diagnostic Logging Severity level suitable for use when logging that a failure has occurred.
WARNING Diagnostic Logging Severity level suitable for use when issuing a warning.
NOTICE Diagnostic Logging Severity level suitable for use when logging an important normative condition.
INFORMATIONAL Diagnostic Logging Severity level suitable for use when logging a normative condition.
DEBUG0 Diagnostic Logging The highest debug severity level.
DEBUG1 Diagnostic Logging The second highest debug severity level.
DEBUG2 Diagnostic Logging The third highest debug severity level.
DEBUG3 Diagnostic Logging The fourth highest debug severity level.
DEBUG4 Diagnostic Logging The fifth highest debug severity level.
DEBUG5 Diagnostic Logging The sixth highest debug severity level.
TRACE Diagnostic Logging Severity level suitable at which trace statements are issued.
BENCHMARK Diagnostic Logging Severity level suitable at which benchmark statements are issued.
DEBUG Diagnostics Logging Alias for DEBUG5
FAIL Diagnostics Logging Alias for FAILURE
WARN Diagnostics Logging Alias for WARNING
INFO Diagnostics Logging Alias for INFORMATIONAL

NOTE: all the severity level constants are for use with the log() function (as well as for setting/getting with set_log_filter() and is_severity_logged()).

Context Managers

The following context managers are defined:

Decorators

The following decorators are defined:

asynctracefunc

Decorate that provides the equivalent functionality as @tracefunc for async functions and methods.

tracefunc

Decorator to be applied to functions and methods to save the need to call d.trace(), for example rather than:

def start():
	d.trace()
	. . .

def submit_work(name, job, priority=-1):
	d.trace()
	. . .

you can, more cleanly, write:

@d.tracefunc
def start():
	. . .

@d.tracefunc
def submit_work(name, job, priority=-1):
	. . .

Functions

Contingent Reporting API

The following functions are defined:

Function Purpose
abort() Issues a contingent report (via conrep()) and then terminating the process.
report() Issues a message string as a contingent report, by defaulting writing output to sys.stderr.
set_default_trailing_prompt() Sets the default trailing prompt.
warn() An analogue to Ruby's warn(), this issues the given message(s) to (by default0 the standard error stream and, if logging is enabled, also log()s at WARNING severity level.

Debugging API

Function Purpose
file() Obtains the file in which the calling function is defined.
fileline() Obtains the file+line in which the calling function is defined.
filelinefunc() Obtains the file+line+function in which the calling function is defined.
func() Obtains the function in which the calling function is defined.
line() Obtains the line on which the calling function is defined.

Diagnostic Logging API

Function Purpose
enable_logging() Enables/disables logging, whether from a constant or from a named environment variable.
is_logging_enabled() Indicates whether logging is enabled.
is_severity_logged() Indicates whether a log statement at a given severity will be logged.
log() Submits a diagnostic log message at a given severity (which will be emitted in the case that is_logging_enabled(sev)) would return True).
set_log_filter() Sets a logging filter - either a threshold severity, or a `dict`` mapping severity levels to enablement flags - that allows fine-grained control of which levels are emitted.

Tracing API

Function Purpose
dbg() Similar to Rust's dbg!() macro, can be passed any number of normal and keyword arguments and traces their name, type, and value.
dbgfl() Like dbg(), but include fileline() information in the TRACE statement produced.
enable_tracing() Enables/disables tracing, whether from a constant or from a named environment variable.
is_tracing_enabled() Indicates whether tracing is enabled.
trace() Traces the name and signature of the calling function, including the values of all its arguments.

Examples

Examples are provided in the examples directory, along with a markdown description for each. A detailed list TOC of them is provided in EXAMPLES.md.

Project Information

Where to get help

GitHub Page

Contribution guidelines

Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/Diagnosticism.Python.

Dependencies

Related projects

License

Diagnosticism.Python is released under the 3-clause BSD license. See LICENSE for details.

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

diagnosticism-0.15.3.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diagnosticism-0.15.3-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file diagnosticism-0.15.3.tar.gz.

File metadata

  • Download URL: diagnosticism-0.15.3.tar.gz
  • Upload date:
  • Size: 30.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for diagnosticism-0.15.3.tar.gz
Algorithm Hash digest
SHA256 3a8c52c2df63311d14f8a7877cc102ddf7c209fdef28d742526230c5d4480e83
MD5 2ee492ae12fad9de55c5f490c416f74a
BLAKE2b-256 a57500adb82ebf67da9c392efc3da072a9b716526d376499a592d496796d1f4a

See more details on using hashes here.

File details

Details for the file diagnosticism-0.15.3-py3-none-any.whl.

File metadata

  • Download URL: diagnosticism-0.15.3-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for diagnosticism-0.15.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6e18f438438816feeac2287363ec49a0c32e4a187e848f0e8c950f7a56715b58
MD5 e41aa50cc4ae0ccb9fbf612c99cdee97
BLAKE2b-256 eae05667974fe87e23a46934c9461e6c3b6ab9f8d641f1ad3be5e0d5c8662e20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page