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.2.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.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vibe_pretty_print-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2be048e1f3d026cbce9fe1d7d9f55461aef19fb16df111bf9fd8e98c396612d0
MD5 a22afbce08013fb45229da7fc2f56a94
BLAKE2b-256 bba8f7b8148a3596e261b18a6dc08ee5032f09b13e7502e3ed6c8da8337217e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vibe_pretty_print-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ecab85a06a80dfc9e4bb1179645ac315725c03d5bf1c5d8daa1fda2048e374e6
MD5 95bb324396c3d3c4dc6208dccf54d3b9
BLAKE2b-256 a60bf3b42ab39d63c8f22007728e342a600773d53c175468fa72beb733d693a6

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