Skip to content

Commit

Permalink
Add get_param_names helper function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mansenfranzen committed Apr 26, 2019
1 parent fbcd573 commit a05738f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/pywrangler/util/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"""

from typing import Callable
import inspect
from typing import Callable, List

from pywrangler.util.types import T_STR_OPT_MUL


def cached_property(method: Callable) -> property:
Expand Down Expand Up @@ -36,3 +39,32 @@ def get_prop_value(obj):
return value

return property(get_prop_value, doc=docstring)


def get_param_names(func: Callable,
ignore: T_STR_OPT_MUL = None) -> List[str]:
"""Retrieve all parameter names for given function.
Parameters
----------
func: Callable
Function for which parameter names should be retrieved.
ignore: iterable, None, optional
Parameter names to be ignored. For example, `self` for `__init__`
functions.
Returns
-------
param_names: list
List of parameter names.
"""

ignore = ignore or []

signature = inspect.signature(func)
parameters = signature.parameters.values()

param_names = [x.name for x in parameters if x.name not in ignore]

return param_names
5 changes: 4 additions & 1 deletion src/pywrangler/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

from typing import Iterable, Union

TYPE_COLUMNS = Union[str, Iterable[str], None]
T_STR_OPT_MUL = Union[Iterable[str], None]
T_STR_OPT_SING_MUL = Union[str, Iterable[str], None]

TYPE_COLUMNS = T_STR_OPT_SING_MUL
TYPE_ASCENDING = Union[bool, Iterable[bool], None]
Empty file added tests/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions tests/util/test_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""This module contains tests for the helper module.
"""

from pywrangler.util.helper import get_param_names


def test_get_param_names():

def func():
pass

assert get_param_names(func) == []

def func1(a, b=4, c=6):
pass

assert get_param_names(func1) == ["a", "b", "c"]
assert get_param_names(func1, ["a"]) == ["b", "c"]

0 comments on commit a05738f

Please sign in to comment.