Dark magics about variable names in python
Overall
score
90%
Build a simple configuration registry that automatically tracks the names of configuration objects when they are created.
Create a configuration registry system with the following capabilities:
Implement a Config class that:
name property that returns the captured variable namevalues property that returns the configuration dictionaryImplement a ConfigRegistry class that:
Config instancesregister() method that accepts a Config instance and stores it using its captured name as the keyget(name) method that retrieves a registered config by its namelist_configs() method that returns a list of all registered config namesdatabase_config = Config({"host": "localhost", "port": 5432}) should automatically capture "database_config" as its name @test@generates
class Config:
"""A configuration object that captures its variable name."""
def __init__(self, values: dict):
"""Initialize with configuration values."""
pass
@property
def name(self) -> str:
"""Return the captured variable name."""
pass
@property
def values(self) -> dict:
"""Return the configuration values."""
pass
class ConfigRegistry:
"""Registry for managing named configuration objects."""
def __init__(self):
"""Initialize empty registry."""
pass
def register(self, config: Config) -> None:
"""Register a config object using its captured name."""
pass
def get(self, name: str) -> Config:
"""Retrieve a registered config by name."""
pass
def list_configs(self) -> list[str]:
"""Return list of all registered config names."""
passProvides variable name introspection capabilities.
Install with Tessl CLI
npx tessl i tessl/pypi-varnameevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10