Dark magics about variable names in python
Overall
score
90%
Build a utility that creates configuration dictionaries from variable names and their values, useful for logging and debugging scenarios.
Create a Python module that provides a function to build configuration dictionaries. The function should accept multiple variables as arguments and return a dictionary where the keys are the string names of those variables.
Implement a function build_config that:
Implement a function build_config_with_prefix that:
prefix (string) that defaults to "cfg"timeout becomes key "app_timeout"Implement a function get_full_path that:
obj.attr.subattr)server.config.port, returns "server.config.port"@generates
def build_config(*args) -> dict:
"""
Build a configuration dictionary from variables.
Args:
*args: Variables to include in the configuration
Returns:
Dictionary with variable names as keys and their values
"""
pass
def build_config_with_prefix(*args, prefix: str = "cfg") -> dict:
"""
Build a configuration dictionary with prefixed keys.
Args:
*args: Variables to include in the configuration
prefix: String prefix for all keys (default: "cfg")
Returns:
Dictionary with prefixed variable names as keys and their values
"""
pass
def get_full_path(arg) -> str:
"""
Get the full attribute path of a variable or attribute chain.
Args:
arg: A variable or attribute chain
Returns:
The full path as a string
"""
passx=10 and y=20, build_config(x, y) returns {"x": 10, "y": 20} @testname="test", build_config(name) returns {"name": "test"} @testhost="localhost" and port=8080 with prefix "server", build_config_with_prefix(host, port, prefix="server") returns {"server_host": "localhost", "server_port": 8080} @testserver with nested attribute server.config.port = 3000, get_full_path(server.config.port) returns "server.config.port" @testProvides functionality for retrieving variable names at runtime.
@satisfied-by
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