🔍 Code Extractor

function wrap_tuple_streams

Maturity: 43

Fills in None values in a tuple with corresponding dimensioned stream values based on matching key dimension names.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1766 - 1780
Complexity:
moderate

Purpose

This function is used to substitute None values in a tuple with values from stream objects when the position corresponds to a key dimension (kdim) name that matches a stream content key. It's designed for data streaming scenarios where tuple keys need to be populated with dynamic stream values, likely in a visualization or data processing pipeline context (HoloViews-related based on kdims terminology).

Source Code

def wrap_tuple_streams(unwrapped, kdims, streams):
    """
    Fills in tuple keys with dimensioned stream values as appropriate.
    """
    param_groups = [(s.contents.keys(), s) for s in streams]
    pairs = [(name,s)  for (group, s) in param_groups for name in group]
    substituted = []
    for pos,el in enumerate(wrap_tuple(unwrapped)):
        if el is None and pos < len(kdims):
            matches = [(name,s) for (name,s) in pairs if name==kdims[pos].name]
            if len(matches) == 1:
                (name, stream) = matches[0]
                el = stream.contents[name]
        substituted.append(el)
    return tuple(substituted)

Parameters

Name Type Default Kind
unwrapped - - positional_or_keyword
kdims - - positional_or_keyword
streams - - positional_or_keyword

Parameter Details

unwrapped: A tuple or tuple-like object that may contain None values to be filled. This is the input tuple that needs stream value substitution.

kdims: A list or sequence of key dimension objects, each having a 'name' attribute. These dimensions define the expected keys at each position in the tuple. Used to match positions with stream content keys.

streams: A list or sequence of stream objects, each having a 'contents' attribute that is a dictionary. The contents dictionary maps dimension names to their current values, which are used to fill None positions in the unwrapped tuple.

Return Value

Returns a tuple with the same length as the input from wrap_tuple(unwrapped). None values at positions corresponding to kdims are replaced with matching stream content values if exactly one match is found. Non-None values and unmatched positions remain unchanged.

Usage Example

# Assuming kdims and stream objects are defined in a HoloViews context
# Example with mock objects:
class MockDim:
    def __init__(self, name):
        self.name = name

class MockStream:
    def __init__(self, contents):
        self.contents = contents

# Define key dimensions
kdims = [MockDim('x'), MockDim('y'), MockDim('z')]

# Define streams with values
streams = [
    MockStream({'x': 10, 'y': 20}),
    MockStream({'z': 30})
]

# Unwrapped tuple with None values to be filled
unwrapped = (None, None, None, 'fixed_value')

# Note: wrap_tuple function must be available in scope
def wrap_tuple(x):
    return x if isinstance(x, tuple) else (x,)

result = wrap_tuple_streams(unwrapped, kdims, streams)
# Result: (10, 20, 30, 'fixed_value')
# None values at positions 0, 1, 2 are filled with stream values for 'x', 'y', 'z'

Best Practices

  • Ensure that kdims objects have a 'name' attribute that matches keys in stream contents dictionaries
  • The function expects exactly one matching stream content for each kdim name; multiple matches will not substitute the None value
  • The wrap_tuple function must be defined in the same scope or imported before using this function
  • Stream objects must have a 'contents' attribute that is a dictionary-like object with keys() method
  • Only None values at positions less than len(kdims) will be considered for substitution
  • Non-None values in the unwrapped tuple are preserved regardless of stream contents

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function drop_streams 64.2% similar

    Filters out dimensioned stream parameters from key dimensions (kdims) and their corresponding keys, returning cleaned dimensions and keys.

    From: /tf/active/vicechatdev/patches/util.py
  • function dimensioned_streams 57.2% similar

    Filters and returns streams from a DynamicMap that have parameters matching the DynamicMap's key dimensions.

    From: /tf/active/vicechatdev/patches/util.py
  • function unbound_dimensions 54.0% similar

    Filters a list of dimensions (kdims) to return only those that have not been associated with any of the provided streams.

    From: /tf/active/vicechatdev/patches/util.py
  • function wrap_tuple 52.2% similar

    A utility function that ensures the input is wrapped in a tuple, leaving existing tuples unchanged and wrapping non-tuple values in a single-element tuple.

    From: /tf/active/vicechatdev/patches/util.py
  • function dimensionless_contents 50.5% similar

    Filters stream parameters to return only those that are not associated with any of the provided key dimensions.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse