CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-fastcore

Python supercharged for fastai development

56

1.36x
Overview
Eval results
Files

task.mdevals/scenario-1/

Configuration Manager with Nested Access

Build a flexible configuration manager that supports nested attribute access, delegation to a default configuration, and automatic attribute storage from initialization parameters.

Requirements

Create a configuration manager system with the following components:

Configuration Class

Implement a Config class that:

  • Accepts keyword arguments during initialization and stores them as instance attributes automatically
  • Supports accessing nested configuration values using dot-notation strings (e.g., "database.host" to access nested values)
  • Provides a method get_nested(path) that retrieves values from nested attributes using a dot-separated path string
  • Provides a method has_nested(path) that checks if nested attributes exist using a dot-separated path string

Configuration with Defaults

Implement a ConfigWithDefaults class that:

  • Takes a user configuration object and a default configuration object during initialization
  • Delegates attribute access to the default configuration when an attribute is not found in the user configuration
  • Allows checking multiple attributes at once to see if they exist (either in user config or defaults)
  • Provides a method get_attrs(attr_list) that retrieves multiple attributes at once, returning them as a tuple

Test Cases

  • When Config is initialized with host="localhost" and port=5432, accessing config.host returns "localhost" and config.port returns 5432 @test

  • When a nested dict structure is stored in Config (e.g., db={"host": "localhost", "port": 5432}), calling get_nested("db.host") returns "localhost" @test

  • When checking if a nested path exists using has_nested("db.port") on a config with nested structure, it returns True if the path exists and False otherwise @test

  • When ConfigWithDefaults is initialized with user config having host="custom" and default config having host="localhost" and timeout=30, accessing config.host returns "custom" and config.timeout returns 30 @test

  • When calling get_attrs(["host", "timeout"]) on a ConfigWithDefaults instance, it returns a tuple with both values, pulling from user config or defaults as appropriate @test

Implementation

@generates

API

class Config:
    """Configuration class with automatic attribute storage and nested access."""

    def __init__(self, **kwargs):
        """Initialize with keyword arguments stored as attributes."""
        pass

    def get_nested(self, path: str):
        """Get a nested attribute using dot notation (e.g., 'db.host')."""
        pass

    def has_nested(self, path: str) -> bool:
        """Check if a nested attribute exists using dot notation."""
        pass


class ConfigWithDefaults:
    """Configuration that delegates to defaults when attributes are not found."""

    def __init__(self, user_config, default_config):
        """Initialize with user config and default config for delegation."""
        pass

    def get_attrs(self, attr_list: list) -> tuple:
        """Get multiple attributes at once, returning them as a tuple."""
        pass

Dependencies { .dependencies }

fastcore { .dependency }

Provides utilities for attribute access, delegation, and functional programming.

Install with Tessl CLI

npx tessl i tessl/pypi-fastcore

tile.json