Skip to main content

A utility for safely printing with optional color formatting and logging to file.

Project description

Safe Print Logging Utilities

Table of Contents

  1. Overview
  2. Installation
  3. Usage
    1. safe_print
    2. error_info
    3. replace_invalid_utf8_characters
    4. Additional Usage Notes
  4. Contributors

Overview

The Safe Print Logging Utilities is a Python script providing enhanced printing functionality with colorful formatting, logging capability, and handling of UTF-8 encoding issues across various data types.

The Utilities define three main functions:

  • safe_print: Augments print functionality.
  • error_info: Logs detailed error information.
  • replace_invalid_utf8_characters: Mitigates issues with invalid UTF-8 characters.

Installation

Requirements

  • Python 3.x
  • colorama package

Installation Steps

  1. Ensure Python is installed on your machine.
  2. Install the colorama package using pip:
    pip install colorama
    
  3. Integrate the script into your project.

Installing with pip

Since the package is available on PyPI, you can install it using pip with the following command:

pip install safe_print_utils

Usage

1. safe_print

Description

Prints data with colorful formatting and logging options.

Parameters

  • data: Data to print, formatted/indented for dicts and lists.
  • child_process_label: (Optional) Label identifying a child process.
  • label_color: Color for child_process_label (Default: "RED").
  • prefix: (Optional) Custom output prefix.
  • prefix_color: Prefix color (Default: "GREEN").
  • text_color: (Optional) Main text color.
  • highlight: Flag to highlight text with yellow background and black text (Default: False).
  • secondary_highlight: Flag to highlight text with black background and yellow text (Default: False).
  • file_path: (Optional) File path for logging (Default: "").
  • file_lines_limit: Max lines in log file, older lines are removed (Default: 10000).
  • show_time: Flag to prefix output with the time (Default: True).
  • error: Flag to make text color red (Default: False).

Example

safe_print("Hello, World!", prefix="Info", prefix_color="GREEN")

2. error_info

Description

Prints and optionally logs detailed error information, including line number and traceback.

Parameters

  • error: Caught exception object.
  • file_path: (Optional) Log file path (Default: "").
  • file_lines_limit: Max lines in log file, older lines are removed (Default: 10000).

Example

try:
    1/0
except Exception as e:
    error_info(e)

3. replace_invalid_utf8_characters

Description

Replaces invalid UTF-8 characters within various data types.

Parameters

  • input_data: Data containing potential non-UTF-8 encoded characters.
  • replacement_character: Character replacing invalid UTF-8 characters (Default: ' ').

Example

cleaned_data = replace_invalid_utf8_characters("some_data_with_invalid_utf8")

Additional Usage Notes

  • Ensure text/log file encoding compatibility with UTF-8.
  • Ensure correct and writable file path/directory when logging with safe_print.

Contributors

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

safe_print_utils-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

safe_print_utils-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file safe_print_utils-0.1.1.tar.gz.

File metadata

  • Download URL: safe_print_utils-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for safe_print_utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 edb580c3f8232ceaf75307967f77ac18e96c64463a81c05eef237ec540dc61ef
MD5 6b45a31496e1b5ec73b796f1ed221126
BLAKE2b-256 c54876d43f71b63d2ca9f1650a6407611cbd85fc927af7a349988d82f914383a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safe_print_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 332a457d52d1fd85d16b6c26c3391784a03946cab517e7e9d59ae5d74479f516
MD5 6e55a6f871227c5661a80c19c4c8df12
BLAKE2b-256 ca5867f97d98ab622ebd8dae6187028253d630b607e7544fa711840058fdc6c9

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