Skip to main content

A Python utilities package.

Project description

Pythils

A Python utilities package.

Installation

pip install pythils

Configuration Management

The pythils.config module provides tools for managing hierarchical configuration with advanced features.

Features

  • ConfigDict: A dictionary-like object with nested key support
  • ConfigRef: References to other configuration values
  • Wildcard Configuration: Support for default values with * and . wildcards
  • Multiple Sources: Load configuration from environment variables, files (JSON, YAML, Python)
  • Nested Access: Navigate through configuration hierarchies

Usage Examples

from pythils.config import ConfigDict, ConfigRef

# Create a configuration
config = ConfigDict({
    'app': {
        'name': 'My App',
        'version': '1.0.0'
    },
    'database': {
        'host': 'localhost',
        'port': 5432
    },
    # Reference another configuration value
    'app_id': ConfigRef('app.name'),
    'features': {
        # Provide defaults with wildcards
        '*': {
            'enabled': True
        },
        'api': {
            '.': ConfigRef('app'), # Default with . wildcard
            'name': 'My App API', # Override default from . wildcard
            'base_url': 'https://example.com/api/'
        },
        'webui': {
            'enabled': False # Override default from * wildcard
        }
    }
})

# Access configuration values
print(config.get('app.name'))  # My App
print(config.get('database.port'))  # 5432
print(config.get('app_id'))  # My App (resolved reference)
print(config.get('feature.enabled'))  # True (from wildcard)

# Nested configuration access
db_config = config['database']
print(db_config.get('host'))  # localhost
print(db_config.get('..app.name'))  # My App (parent/root navigation)

# Configuration references
config = ConfigDict({
    'base_url': 'https://api.example.com',
    'endpoints': {
        'users': ConfigRef('base_url') + '/users',
        'auth': ConfigRef('.users') + '/auth'  # relative reference
    }
})

print(config.get('endpoints.auth'))  # https://api.example.com/users/auth

# Load from environment variables
env_config = ConfigDict.from_env('MYAPP_')

# Load from file
file_config = ConfigDict.from_file('config.json')

Dynamic Interfaces

The pythils.interface module provides tools for creating interfaces with multiple implementations that can be dynamically loaded.

Features

  • DynamicInterface: Abstract base class for creating interfaces
  • Implementation Discovery: Automatically discover and load implementations
  • Configuration-based Instantiation: Create instances of implementations from configuration dictionaries
  • Flexible Selection: Choose implementations by name or from configuration

Usage Examples

from pythils.interface import DynamicInterface

# Define an interface
class Database(DynamicInterface):
    __implementation_package__ = "myapp.db.implementations"
    __default_implementation__ = "postgres"
    
    def connect(self):
        """Connect to the database"""
        pass
        
    def query(self, sql):
        """Execute a query"""
        pass

# Create an instance from configuration
config = {
    "implementation": "sqlite",
    "sqlite": {
        "database": "app.db",
        "timeout": 30
    }
}

# Create instance from configuration
db = Database.create_instance(config)

# Create instance by name
postgres_db = Database.create_instance(implementation="postgres", host="localhost", port=5432)
# or
postgres_db = Database.get_implementation("postgres")(host="localhost", port=5432)

# Get available implementations
implementations = Database.get_implementations()
print(implementations)  # ['sqlite', 'postgres', 'mysql']

Development

Running Tests

python -m unittest discover -s tests

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

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

pythils-0.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

pythils-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pythils-0.1.0.tar.gz.

File metadata

  • Download URL: pythils-0.1.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for pythils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c184ecd401c5d4039063eb499d78bdf8b5db46ab80db0e9f59623b8c42302fb8
MD5 b877b4c1394feb3f9d1107ff930da5cd
BLAKE2b-256 6dcc6a0919a1ba64037223f21f26a89e5154f31694f4ad53b84cbbce136e606a

See more details on using hashes here.

File details

Details for the file pythils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pythils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for pythils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef38fbed43a6c28ab75b8f5253cbb6db58f36d2efb4d92162131bcbdfe3a7bd2
MD5 b8f2efea1a9c9316c6a20fa04e82502a
BLAKE2b-256 3f254df5cba1fced9c76f7e6ef48934ac9f401a62e124e7ad370dd516e61e1cf

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