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('features.api.enabled'))  # True (from wildcard)
print(config.get('features.api.version'))  # '1.0.0' (from dot-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.1.tar.gz (15.4 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.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pythils-0.1.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pythils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f1a041b3d917f5b930c3d3cc7877e59111e585b0f84ef9f2874100a6cda9766d
MD5 45b7b60d16335775fae04626b5dd2a9c
BLAKE2b-256 a34d47f678be9c49281b18967ada92c57842f92ab63132acad4c2921892b9b09

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pythils-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pythils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4f15c78ccb22a49b6b83af3fef27aded08d95500192f2cdee7bc21e379253216
MD5 07f97e63f2633555ebb3fdae927ecbe6
BLAKE2b-256 bca55a924b06d4a28bf2c116e860391eab41306b4cf8fd0d763cc5fbf658b625

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