function rename_stream_kwargs
Maps parameter names in a kwargs dictionary to their renamed equivalents for a given stream object, with optional reverse mapping from renamed back to original names.
/tf/active/vicechatdev/patches/util.py
1688 - 1705
simple
Purpose
This function facilitates parameter name translation for stream objects by mapping between original and renamed parameter names. It's used in systems where parameters may be renamed for consistency or compatibility, allowing bidirectional translation. The function validates that all keys can be mapped and raises KeyError if any key lacks a corresponding mapping.
Source Code
def rename_stream_kwargs(stream, kwargs, reverse=False):
"""
Given a stream and a kwargs dictionary of parameter values, map to
the corresponding dictionary where the keys are substituted with the
appropriately renamed string.
If reverse, the output will be a dictionary using the original
parameter names given a dictionary using the renamed equivalents.
"""
mapped_kwargs = {}
mapping = stream_name_mapping(stream, reverse=reverse)
for k,v in kwargs.items():
if k not in mapping:
msg = 'Could not map key {key} {direction} renamed equivalent'
direction = 'from' if reverse else 'to'
raise KeyError(msg.format(key=repr(k), direction=direction))
mapped_kwargs[mapping[k]] = v
return mapped_kwargs
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
stream |
- | - | positional_or_keyword |
kwargs |
- | - | positional_or_keyword |
reverse |
- | False | positional_or_keyword |
Parameter Details
stream: A stream object that has associated parameter name mappings. This object is passed to stream_name_mapping() to retrieve the mapping dictionary between original and renamed parameter names.
kwargs: A dictionary of parameter names (keys) and their values. When reverse=False, keys should be original parameter names. When reverse=True, keys should be renamed parameter names.
reverse: Boolean flag (default: False). When False, maps from original parameter names to renamed equivalents. When True, maps from renamed parameter names back to original names.
Return Value
Returns a dictionary (mapped_kwargs) with the same values as the input kwargs, but with keys translated according to the mapping. Keys are either renamed (when reverse=False) or restored to original names (when reverse=True). Raises KeyError if any key in kwargs cannot be found in the mapping.
Dependencies
stream_name_mapping
Required Imports
No standard library imports required for this function itself
Requires stream_name_mapping function to be available in the same module or imported
Usage Example
# Assuming stream_name_mapping is defined and returns appropriate mappings
# Example with a hypothetical stream object
# Forward mapping (original -> renamed)
original_params = {'old_name': 10, 'another_param': 'value'}
renamed_params = rename_stream_kwargs(my_stream, original_params, reverse=False)
# Result: {'new_name': 10, 'renamed_param': 'value'}
# Reverse mapping (renamed -> original)
renamed_params = {'new_name': 10, 'renamed_param': 'value'}
original_params = rename_stream_kwargs(my_stream, renamed_params, reverse=True)
# Result: {'old_name': 10, 'another_param': 'value'}
# Error handling
try:
invalid_params = {'nonexistent_key': 5}
result = rename_stream_kwargs(my_stream, invalid_params)
except KeyError as e:
print(f"Mapping error: {e}")
Best Practices
- Always ensure the stream object is properly initialized and compatible with stream_name_mapping() before calling this function
- Handle KeyError exceptions when using this function, as it will raise an error for unmapped keys
- Use reverse=True when you need to convert renamed parameters back to their original names
- Validate that all required parameters are present in kwargs before calling this function to avoid partial mappings
- The function does not modify the input kwargs dictionary; it creates and returns a new dictionary
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function stream_name_mapping 83.1% similar
-
function stream_parameters 59.6% similar
-
function validate_dynamic_argspec 49.1% similar
-
function wrap_tuple_streams 49.0% similar
-
function resolve_dependent_kwargs 48.7% similar