Skip to main content

A JFDI approach that hydrates a Python class to allow dot notation access to the data structures.

Project description

PyHydrate

license pypi ci downloads versions

Easily access your JSON, YAML, TOML, dicts, and lists with dot notation.

PyHydrate provides a simple way to access nested data structures without worrying about .get() methods, defaults, or array slicing. It handles errors gracefully when accessing data elements that may not exist, with automatic key normalization and type inference.

Installation

pip install pyhydrate

Dependencies: PyHydrate automatically handles TOML support:

  • Python 3.11+: Uses built-in tomllib
  • Python < 3.11: Automatically installs tomli for TOML support

Quick Start

from pyhydrate import PyHydrate

# Works with nested dictionaries
data = {
    "user-info": {
        "firstName": "John",
        "contact_details": {
            "email": "john@example.com",
            "phone": "555-0123"
        }
    }
}

py_data = PyHydrate(data)

# Access with dot notation - keys are automatically normalized
print(py_data.user_info.first_name())  # "John"
print(py_data.user_info.contact_details.email())  # "john@example.com"

# Graceful handling of missing data
print(py_data.user_info.missing_field())  # None

Features

Automatic Format Detection

Load data from JSON, YAML, or TOML strings - format is detected automatically:

# JSON string
json_config = '{"database": {"host": "localhost", "port": 5432}}'
config = PyHydrate(json_config)

# TOML string  
toml_config = '''
[database]
host = "localhost"
port = 5432
'''
config = PyHydrate(toml_config)

# YAML string
yaml_config = '''
database:
  host: localhost
  port: 5432
'''
config = PyHydrate(yaml_config)

print(config.database.host())  # "localhost" (works with all formats)

File Loading

Load data directly from files:

# Supports .json, .yaml, .yml, and .toml files
config = PyHydrate(path="config.json")
settings = PyHydrate(path="settings.yaml") 
project = PyHydrate(path="pyproject.toml")

Key Normalization

Automatically converts different key formats to snake_case:

data = {
    "firstName": "John",
    "last-name": "Doe", 
    "Email Address": "john@example.com"
}

py_data = PyHydrate(data)
print(py_data.first_name())      # "John"
print(py_data.last_name())       # "Doe"  
print(py_data.email_address())   # "john@example.com"

Multiple Output Formats

py_data = PyHydrate({"user": {"name": "John", "age": 30}})

# Different output formats
print(py_data.user())           # Returns the cleaned Python object
print(py_data.user('json'))     # Returns JSON string
print(py_data.user('yaml'))     # Returns YAML string  
print(py_data.user('toml'))     # Returns TOML string
print(py_data.user('type'))     # Returns Python type
print(py_data.user('element'))  # Returns {"dict": {...}}

Array Access

Handle lists and nested arrays easily:

data = {
    "users": [
        {"name": "John", "age": 30},
        {"name": "Jane", "age": 25}
    ]
}

py_data = PyHydrate(data)
print(py_data.users[0].name())  # "John"
print(py_data.users[1].age())   # 25

Debug Mode

Get detailed access logging:

data = {"level1": {"level2": {"value": "test"}}}
py_data = PyHydrate(data, debug=True)

print(py_data.level1.level2.value())
# Debug output shows the access path and depth

Error Handling

PyHydrate uses graceful error handling - invalid access returns None instead of raising exceptions:

from pyhydrate import PyHydrate

# Invalid access returns None instead of failing
data = PyHydrate({"valid": "data"})
result = data.invalid.deeply.nested.access()  # Returns None, no exception
print(result)  # None

# Works with arrays too
data = PyHydrate({"items": [1, 2, 3]})
result = data.items[10]()  # Index out of range - returns None
print(result)  # None

For advanced error handling with warnings:

import warnings
from pyhydrate import PyHydrate, PyHydrateWarning

# This will generate a PyHydrateWarning for invalid API usage
data = PyHydrate({"test": "value"})
result = data("invalid_format")  # APIUsageWarning: Call type 'invalid_format' not supported
print(result)  # None

# Filter warnings to suppress them
warnings.filterwarnings("ignore", category=PyHydrateWarning)
result = data("invalid_format")  # Same call, but no warning shown
print(result)  # None (still works, just silent)

Type Conversion

Convert PyHydrate objects to Python primitives:

data = PyHydrate({"count": "42", "price": "19.99", "active": "true"})

# Use Python's built-in type conversion
count = int(data.count())      # 42
price = float(data.price())    # 19.99
is_active = bool(data.active()) # True

License

This project is licensed under the MIT License - see the LICENSE file for details.

Demo

See a comprehensive demonstration of all PyHydrate features:

python demo.py

This interactive demo showcases:

  • Complex data structures with mixed key formats
  • All output formats (JSON, YAML, TOML, element, type)
  • Array access and negative indexing
  • String format detection and file loading
  • Graceful error handling and warning system
  • Magic methods and type conversion
  • Lazy loading performance with actual proof
  • Complete feature overview

Contributing

For development setup, testing guidelines, and contribution instructions, see CONTRIBUTING.md.

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

pyhydrate-1.0.9.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

pyhydrate-1.0.9-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

File details

Details for the file pyhydrate-1.0.9.tar.gz.

File metadata

  • Download URL: pyhydrate-1.0.9.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for pyhydrate-1.0.9.tar.gz
Algorithm Hash digest
SHA256 550c8f6dc732d551c15c09c687f2e1a8c3125b2c1e01bbffdc5c68cb36993a0a
MD5 2258d6f541629e4238460a79beb866ca
BLAKE2b-256 cba4dc7212c4d6cc20e2d375fe7486d02fdc31391e2874aceaff3f2202f6ffd8

See more details on using hashes here.

File details

Details for the file pyhydrate-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: pyhydrate-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 26.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for pyhydrate-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 8d172e312eac0c07871f893149e59f3ee96b46bcc238bac3750fc0475e316121
MD5 19681ac1238628406f123cda0ef6cb2a
BLAKE2b-256 acd7fdc23f336ad3520076c5325290d4e92b47387171ff37206e52553242acd0

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