Skip to main content

Unpacking, spreading, or splatting positional arguments and keyword arguments in Python

Project description

unpacking

PyPI Tests License

Unpacking, spreading, or splatting positional arguments and keyword arguments in Python.

This library provides functional tools as classes that give you versions of your original functions which use unpacking expressions (Python reference, PEP 448) for the function calls underneath.

Table of Contents

Why Use Unpacking?

  • Multiprocessing: Easily map functions over lists of mixed argument structures
  • Dynamic function calls: Handle variable argument formats cleanly without manual unpacking
  • Functional programming: Create reusable argument-unpacking patterns
  • Code simplification: Reduce boilerplate when working with argument collections

Installation

pip install -U unpacking

Quick Start

from unpacking import unpacking

def your_function(x, y, z=None):
    return f"{x} + {y} + {z}"

# Works with both lists and dicts automatically
result1 = unpacking(your_function)([1, 2, 3])          # Uses *args
result2 = unpacking(your_function)({"x": 1, "y": 2})   # Uses **kwargs

Examples

Basic Example

from unpacking import starred, doublestarred, unpacking

def add(x, y):
    return x + y

args = [1, 2]
kwargs = {"x": 1, "y": 2}

# Traditional unpacking
print(add(*args))                   # 3
print(add(**kwargs))                # 3

# Using unpacking library
print(starred(add)(args))           # 3
print(doublestarred(add)(kwargs))   # 3

# `unpacking` automatically detects the appropriate unpacking method
print(unpacking(add)(args))         # 3
print(unpacking(add)(kwargs))       # 3

Partial Argument Matching

Handle cases where you have more arguments than the function needs:

from unpacking import starredpart, doublestarredpart, unpackingpart

def add(x, y):
    return x + y

args_excess = [1, 2, 3]  # Extra argument ignored
kwargs_excess = {"x": 1, "y": 2, "z": 3}  # Extra keyword ignored

print(starredpart(add)(args_excess))         # 3
print(doublestarredpart(add)(kwargs_excess)) # 3
print(unpackingpart(add)(args_excess))       # 3
print(unpackingpart(add)(kwargs_excess))     # 3

Multiprocessing Examples

Simple Example

from concurrent.futures import ProcessPoolExecutor
from unpacking import unpacking

def add(x, y):
    return x + y

args_list = [[1, 2], [3, 4]]
kwargs_list = [{"x": 1, "y": 2}, {"x": 3, "y": 4}]

with ProcessPoolExecutor(2) as executor:
    print(tuple(executor.map(unpacking(add), args_list)))    # (3, 7)
    print(tuple(executor.map(unpacking(add), kwargs_list)))  # (3, 7)

Data Processing Example

from concurrent.futures import ProcessPoolExecutor
from unpacking import unpacking

def process_data(file_path, format_type, compression=None):
    """Process a data file with specified format and optional compression."""
    # Simulate processing logic
    result = f"Processed {file_path} as {format_type}"
    if compression:
        result += f" with {compression} compression"
    return result

# Mixed argument formats - some positional, some keyword, some partial
tasks = [
    ["data1.csv", "csv", "gzip"],                                    # All positional
    ["data2.json", "json"],                                          # Partial positional
    {"file_path": "data3.xml", "format_type": "xml"},                # Keyword only
    {"file_path": "data4.parquet", "format_type": "parquet", "compression": "snappy"}  # All keyword
]

with ProcessPoolExecutor() as executor:
    results = list(executor.map(unpacking(process_data), tasks))
    for result in results:
        print(result)

Common Patterns

Working with Configuration Objects

from unpacking import unpacking

def create_connection(host, port, username, password=None, timeout=30):
    return f"Connected to {host}:{port} as {username}"

configs = [
    {"host": "localhost", "port": 5432, "username": "admin"},
    {"host": "remote.db", "port": 3306, "username": "user", "password": "secret"},
]

connections = [unpacking(create_connection)(config) for config in configs]

Chaining with Other Functional Tools

from functools import partial
from unpacking import unpacking

def add_and_multiply(x, y, multiplier=1):
    return (x + y) * multiplier

# Create a specialized function
add_and_double = partial(unpacking(add_and_multiply), multiplier=2)

args_list = [[2, 3], [4, 5], [1, 6]]
results = list(map(add_and_multiply, args_list))  # [10, 18, 14]

API Reference

starred(func) -> callable

Returns a function that calls func(*args) when given an iterable.

Parameters:

  • func: The function to wrap

Returns: A new function that unpacks positional arguments from an iterable

Example:

starred_func = starred(your_function)
result = starred_func([arg1, arg2, arg3])  # Equivalent to your_function(*[arg1, arg2, arg3])

doublestarred(func) -> callable

Returns a function that calls func(**kwargs) when given a mapping.

Parameters:

  • func: The function to wrap

Returns: A new function that unpacks keyword arguments from a mapping

Example:

doublestarred_func = doublestarred(your_function)
result = doublestarred_func({"x": 1, "y": 2})  # Equivalent to your_function(**{"x": 1, "y": 2})

unpacking(func) -> callable

Automatically detects whether to use *args or **kwargs unpacking based on the argument type.

Parameters:

  • func: The function to wrap

Returns: A new function that unpacks arguments appropriately

Behavior:

  • For sequences (list, tuple): uses *args unpacking
  • For mappings (dict): uses **kwargs unpacking

Example:

unpacking_func = unpacking(your_function)
result1 = unpacking_func([1, 2, 3])          # Uses *args
result2 = unpacking_func({"x": 1, "y": 2})   # Uses **kwargs

starredpart(func) -> callable

Like starred(), but only passes as many positional arguments as the function accepts.

Parameters:

  • func: The function to wrap

Returns: A new function that unpacks only the needed positional arguments

doublestarredpart(func) -> callable

Like doublestarred(), but only passes keyword arguments that the function accepts.

Parameters:

  • func: The function to wrap

Returns: A new function that unpacks only the needed keyword arguments

unpackingpart(func) -> callable

Like unpacking(), but only passes as many arguments as the function needs (works with both positional and keyword arguments).

Parameters:

  • func: The function to wrap

Returns: A new function that unpacks only the needed arguments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unpacking-1!0.1.4.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unpacking-1!0.1.4-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file unpacking-1!0.1.4.tar.gz.

File metadata

  • Download URL: unpacking-1!0.1.4.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for unpacking-1!0.1.4.tar.gz
Algorithm Hash digest
SHA256 cc938f9ced0de150594680658cc6966d772e5b5bf356d31e9ed10a1b54564f32
MD5 fd63d961b7c494ce351de3ee36f3f2d8
BLAKE2b-256 2cdfa6af838f261dc7b10e0c088f1931b016febe5c69b69b8fcd454b5eab72ab

See more details on using hashes here.

File details

Details for the file unpacking-1!0.1.4-py3-none-any.whl.

File metadata

  • Download URL: unpacking-1!0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for unpacking-1!0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a77111cc2301dcb5d2b0ea9ea0609a72e84831ec87717bb198c174031a12be95
MD5 30395b6af0d37eb6c9eb759921725928
BLAKE2b-256 5522b8cbed3366662afa756f071ed68bf778b3831a7e7171df93aac263dfe481

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page