ETA444 / datasafari

DataSafari simplifies complex data science tasks into straightforward, powerful one-liners.
https://datasafari.dev
GNU General Public License v3.0
2 stars 0 forks source link

Implement new filter util: filter_kwargs() #19

Closed ETA444 closed 7 months ago

ETA444 commented 9 months ago

Description:


Method Functionality Idea:

The filter_kwargs function is used to filter keyword arguments (kwargs) to include only those that are valid for a specified method. It operates based on a dictionary mapping methods to their valid keyword arguments.

How it operates:

Given a method name (method), a dictionary of keyword arguments (kwargs), and a dictionary mapping method names to valid keyword arguments (valid_kwargs_dict), this function filters the keyword arguments to include only those that are valid for the specified method.

Parameters:

Returns:

Examples:

valid_kwargs = {'method1': ['param1', 'param2'], 'method2': ['param3']}
all_kwargs = {'param1': 10, 'param2': 20, 'param3': 30}
filtered_kwargs = filter_kwargs('method1', all_kwargs, valid_kwargs)
print(filtered_kwargs)  # Output: {'param1': 10, 'param2': 20}

Note:

This function is particularly useful in situations where a function or method accepts a wide variety of keyword arguments, and you want to ensure that only relevant keyword arguments are passed through, based on the specific method or operation being performed.

ETA444 commented 7 months ago

Implementation Summary:

The function filter_kwargs() filters keyword arguments (kwargs) to include only those that are valid for a specified method. This is based on a dictionary mapping methods to their valid keyword arguments.

Code Breakdown:

  1. Defining the Dictionary:
valid_kwargs = {
    'head': ['n'],
    'describe': ['percentiles', 'include', 'exclude'],
    'info': ['verbose', 'max_cols', 'memory_usage', 'show_counts']
}
  1. Defining the Function:
def filter_kwargs(method: str, kwargs: dict, valid_kwargs_dict: dict) -> dict:
  1. Filtering Keyword Arguments:
    return {k: v for k, v in kwargs.items() if k in valid_kwargs_dict.get(method, [])}

See the Full Function:

The full implementation can be found in the datasafari repository.