function stream_name_mapping
Creates a bidirectional mapping dictionary between stream parameter names and their renamed versions, with optional parameter exclusion and reverse mapping capability.
/tf/active/vicechatdev/patches/util.py
1663 - 1686
moderate
Purpose
This function facilitates parameter name translation in streaming contexts by building a dictionary that maps original parameter names to their renamed equivalents (or vice versa). It handles both Params objects (with special handling for parameter ownership) and regular stream objects with param attributes. This is useful for maintaining compatibility when parameter names change or need to be aliased in data streaming pipelines.
Source Code
def stream_name_mapping(stream, exclude_params=['name'], reverse=False):
"""
Return a complete dictionary mapping between stream parameter names
to their applicable renames, excluding parameters listed in
exclude_params.
If reverse is True, the mapping is from the renamed strings to the
original stream parameter names.
"""
from ..streams import Params
if isinstance(stream, Params):
mapping = {}
for p in stream.parameters:
if isinstance(p, str):
mapping[p] = stream._rename.get(p, p)
else:
mapping[p.name] = stream._rename.get((p.owner, p.name), p.name)
else:
filtered = [k for k in stream.param if k not in exclude_params]
mapping = {k: stream._rename.get(k, k) for k in filtered}
if reverse:
return {v: k for k,v in mapping.items()}
else:
return mapping
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
stream |
- | - | positional_or_keyword |
exclude_params |
- | ['name'] | positional_or_keyword |
reverse |
- | False | positional_or_keyword |
Parameter Details
stream: A stream object or Params instance containing parameters to map. Can be either a Params object (from ..streams) with a parameters attribute and _rename dictionary, or any object with a param attribute and _rename dictionary. The _rename dictionary contains the mapping rules for parameter renaming.
exclude_params: A list of parameter names to exclude from the mapping. Defaults to ['name']. These parameters will not appear in the returned mapping dictionary, allowing selective filtering of parameters that should not be renamed or mapped.
reverse: Boolean flag that controls the direction of the mapping. When False (default), maps from original parameter names to renamed versions. When True, reverses the mapping to go from renamed strings back to original parameter names.
Return Value
Returns a dictionary where keys and values depend on the reverse parameter. If reverse=False, returns {original_param_name: renamed_param_name} for all non-excluded parameters. If reverse=True, returns {renamed_param_name: original_param_name}. If a parameter has no rename defined in stream._rename, it maps to itself. For Params objects, handles both string parameters and parameter objects with owner and name attributes.
Dependencies
param
Required Imports
from ..streams import Params
Conditional/Optional Imports
These imports are only needed under specific conditions:
from ..streams import Params
Condition: Required when stream is a Params instance to check isinstance and access parameters attribute
Required (conditional)Usage Example
# Example with a hypothetical stream object
class MyStream:
def __init__(self):
self.param = ['x', 'y', 'name', 'value']
self._rename = {'x': 'x_coord', 'y': 'y_coord'}
stream = MyStream()
# Get forward mapping (original -> renamed)
forward_map = stream_name_mapping(stream)
# Result: {'x': 'x_coord', 'y': 'y_coord', 'value': 'value'}
# Note: 'name' is excluded by default
# Get reverse mapping (renamed -> original)
reverse_map = stream_name_mapping(stream, reverse=True)
# Result: {'x_coord': 'x', 'y_coord': 'y', 'value': 'value'}
# Include all parameters
all_params_map = stream_name_mapping(stream, exclude_params=[])
# Result: {'x': 'x_coord', 'y': 'y_coord', 'name': 'name', 'value': 'value'}
Best Practices
- Ensure the stream object has a _rename attribute defined before calling this function to avoid AttributeError
- Use exclude_params to filter out parameters that should not be renamed or exposed in the mapping
- When working with Params objects, be aware that parameters can be either strings or objects with owner and name attributes
- Use reverse=True when you need to translate from renamed parameters back to original names, useful for reverse lookups
- The function returns identity mappings (param: param) for parameters without explicit renames in _rename dictionary
- For Params objects, the _rename dictionary can use tuples (owner, name) as keys for more specific parameter identification
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function rename_stream_kwargs 83.1% similar
-
function stream_parameters 63.0% similar
-
function dimensioned_streams 52.1% similar
-
function drop_streams 44.6% similar
-
function dynamicmap_memoization 44.3% similar