🔍 Code Extractor

function wrap_tuple

Maturity: 32

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.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1658 - 1660
Complexity:
simple

Purpose

This function provides a consistent way to normalize inputs that may or may not be tuples. It's commonly used in APIs where a parameter can accept either a single value or multiple values (as a tuple), ensuring downstream code can always treat the result as a tuple. This simplifies handling of variable-length arguments and makes code more robust when dealing with flexible input types.

Source Code

def wrap_tuple(unwrapped):
    """ Wraps any non-tuple types in a tuple """
    return (unwrapped if isinstance(unwrapped, tuple) else (unwrapped,))

Parameters

Name Type Default Kind
unwrapped - - positional_or_keyword

Parameter Details

unwrapped: Any Python object that needs to be ensured as a tuple. Can be of any type including primitives (int, str, etc.), collections (list, dict, set), or already a tuple. No type constraints - accepts any value.

Return Value

Returns a tuple. If the input is already a tuple, returns it unchanged. If the input is any other type (including None, primitives, lists, etc.), returns a new tuple containing that single value as its only element. For example: 5 becomes (5,), 'text' becomes ('text',), [1,2] becomes ([1,2],), and (1,2) remains (1,2).

Usage Example

# Basic usage examples
result1 = wrap_tuple(5)
print(result1)  # Output: (5,)

result2 = wrap_tuple('hello')
print(result2)  # Output: ('hello',)

result3 = wrap_tuple((1, 2, 3))
print(result3)  # Output: (1, 2, 3)

result4 = wrap_tuple([1, 2, 3])
print(result4)  # Output: ([1, 2, 3],)

result5 = wrap_tuple(None)
print(result5)  # Output: (None,)

# Practical use case: normalizing function arguments
def process_items(items):
    items = wrap_tuple(items)
    for item in items:
        print(f"Processing: {item}")

process_items('single')  # Works with single value
process_items(('a', 'b', 'c'))  # Works with tuple

Best Practices

  • Use this function when you need to accept both single values and tuples as input, ensuring consistent tuple handling downstream
  • Be aware that lists, sets, and other iterables will be wrapped as single-element tuples, not converted to tuples of their elements
  • This function does not recursively wrap nested structures - only the top-level value is wrapped
  • Consider using this pattern in API design where parameters should accept flexible input types
  • Note that empty tuples () will remain empty tuples, while None will become (None,)
  • This is a pure function with no side effects, making it safe to use in any context

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function wrap_tuple_streams 52.2% similar

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

    From: /tf/active/vicechatdev/patches/util.py
  • function transaction 43.8% similar

    A decorator function that wraps another function to provide database transaction management capabilities, currently implemented as a placeholder for future transaction handling.

    From: /tf/active/vicechatdev/CDocs/controllers/__init__.py
  • function lzip 43.5% similar

    A convenience wrapper around Python's built-in zip function that returns a list instead of an iterator.

    From: /tf/active/vicechatdev/patches/util.py
  • function unique_zip 41.2% similar

    Returns a unique list of tuples created by zipping multiple iterables together, removing any duplicate tuples while preserving order.

    From: /tf/active/vicechatdev/patches/util.py
  • function numpy_scalar_to_python 40.3% similar

    Converts NumPy scalar types to their equivalent native Python types (float or int), returning the original value if it's not a NumPy numeric scalar.

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