Skip to main content

A smart formatting library for JSON, XML, YAML, and text with OpenAI integration

Project description

vibe-pretty-print

A smart Python library for formatting JSON, XML, YAML, and plain text with OpenAI-powered enhancement.

Features

  • Multi-format support: JSON, XML, YAML, and plain text formatting
  • Validation: Validates JSON, XML, and YAML before formatting
  • OpenAI integration: Uses OpenAI GPT for enhanced formatting and readability
  • Color output: Provides colored output for better readability
  • Fallback handling: Gracefully falls back to standard formatting if OpenAI fails
  • Type-safe: Full type hints and error handling

Installation

pip install vibe-pretty-print

Quick Start

Setup API Key (Recommended)

Export your OpenAI API key as an environment variable:

export VIBE_PP_API_KEY="your-openai-api-key-here"

Then use the library without hardcoding your API key:

from vibe_pprint import vibe_pprint

# Format JSON
json_text = '{"name":"John","age":30,"city":"New York"}'
formatted_json = vibe_pprint(text=json_text, format_type="json")
print(formatted_json)

# Format YAML
yaml_text = "name: John\nage: 30\ncity: New York"
formatted_yaml = vibe_pprint(text=yaml_text, format_type="yaml")
print(formatted_yaml)

# Format XML
xml_text = "<person><name>John</name><age>30</age></person>"
formatted_xml = vibe_pprint(text=xml_text, format_type="xml")
print(formatted_xml)

# Format plain text
text = "Hello World! This is a test."
formatted_text = vibe_pprint(text=text, format_type="text")
print(formatted_text)

Alternative: Hardcoded API Key (Not Recommended)

If you prefer, you can still pass the API key directly (less secure):

from vibe_pprint import vibe_pprint

# Only use this approach for quick testing
formatted = vibe_pprint(
    api_key="your-openai-api-key", 
    text='{"test": "data"}', 
    format_type="json"
)

API Reference

vibe_pprint(text, format_type="text", api_key=None)

Parameters:

  • text (str): The content to format (JSON, XML, YAML, or plain text)
  • format_type (str): Type of content - "json", "xml", "yaml", or "text" (default: "text")
  • api_key (str, optional): Your OpenAI API key. If not provided, will use VIBE_PP_API_KEY environment variable.

Returns:

  • str: Pretty-printed/formatted string

Raises:

  • ValidationError: If the input format is invalid
  • OpenAIError: If OpenAI API calls fail
  • ValueError: If format_type is invalid or inputs are empty

Usage Examples

JSON Formatting

from vibe_pprint import vibe_pprint

# Valid JSON
json_data = '{"users":[{"name":"Alice","age":25},{"name":"Bob","age":30}]}'
result = vibe_pprint(json_data, "json")
print(result)
# Output:
# {
#   "users": [
#     {
#       "age": 25,
#       "name": "Alice"
#     },
#     {
#       "age": 30,
#       "name": "Bob"
#     }
#   ]
# }

# Invalid JSON - will raise ValidationError
try:
    vibe_pprint('{"invalid": json}', "json")
except ValidationError as e:
    print(f"JSON validation failed: {e}")

YAML Formatting

yaml_data = """
users:
  - name: Alice
    age: 25
    skills:
      - Python
      - JavaScript
  - name: Bob
    age: 30
    skills:
      - Java
      - SQL
"""
result = vibe_pprint(yaml_data, "yaml")
print(result)

XML Formatting

xml_data = "<book><title>Python Guide</title><author>John Doe</author><year>2024</year></book>"
result = vibe_pprint(xml_data, "xml")
print(result)

Plain Text Formatting

text = "# Configuration\nhost: localhost\nport: 8080\nurl: https://example.com"
result = vibe_pprint(text, "text")
print(result)
# Output with color highlighting for comments, URLs, and numbers

Error Handling

The library provides specific exceptions for different error scenarios:

from vibe_pprint import ValidationError, OpenAIError

try:
    result = vibe_pprint(invalid_json, "json")
except ValidationError as e:
    print(f"Input validation failed: {e}")
except OpenAIError as e:
    print(f"OpenAI API error: {e}")
except ValueError as e:
    print(f"Invalid input: {e}")

Requirements

  • Python 3.7+
  • requests
  • PyYAML
  • colorama
  • lxml

Development

Install development dependencies

pip install -e ".[dev]"

Run tests

pytest

Code formatting

black vibe_pprint/

Type checking

mypy vibe_pprint/

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Run the test suite
  6. Submit a pull request

Support

For issues and questions, please use the GitHub Issues page.

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

vibe_pretty_print-0.1.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

vibe_pretty_print-0.1.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file vibe_pretty_print-0.1.3.tar.gz.

File metadata

  • Download URL: vibe_pretty_print-0.1.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for vibe_pretty_print-0.1.3.tar.gz
Algorithm Hash digest
SHA256 97b1e31623e7a8cae6d436d6f1ab31d76c76ead7ba94d1104c15140697dea394
MD5 bdedda193275cb29ad55a8f162e2d7b0
BLAKE2b-256 6b73c1bbda0c9207c4c94c5b238cc8d64480e8802dcb821e8a23c301a285a3f1

See more details on using hashes here.

File details

Details for the file vibe_pretty_print-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for vibe_pretty_print-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6eb99acfe187005ba4011447731947bf2d6488bb9bba6fd43fe8c831b61cb348
MD5 e93c85317f20892c08fbec90daed605d
BLAKE2b-256 1c1de47609f1f1b00050582982ad72abf3c56032b3f2fc50842ebbf45bc72162

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