Skip to main content

Simple callable argument inspection & filtering

Project description

Project Status: Active — The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/argset/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/argset.svg MIT License

GitHub | PyPI | Issues

argset provides a simple interface for determining whether a callable takes an argument with a given name, filtering a dict of potential arguments down to just those that a callable accepts, and determining any required arguments that are missing from a dict of potential arguments.

Installation

argset requires Python 3.6 or higher. Just use pip for Python 3 (You have pip, right?) to install argset and its dependencies:

python3 -m pip install argset

Examples

Inspecting a function’s arguments:

>>> from argset import argset
>>> def my_func(foo, bar):
...     print(f"foo={foo!r}")
...     print(f"bar={bar!r}")
...
>>> a = argset(my_func)
>>> "foo" in a
True
>>> "quux" in a
False

Filtering a set of arguments to just those accepted by the function:

>>> a.select({"foo": 42, "bar": 23, "quux": 17})
{'foo': 42, 'bar': 23}
>>> my_func(**a.select({"foo": 42, "bar": 23, "quux": 17}))
foo=42
bar=23

Same as above, but now the function takes **kwargs:

>>> from argset import argset
>>> def my_func2(foo, **kwargs):
...     print(f"foo={foo!r}")
...     for k, v in kwargs.items():
...          print(f"{k}={v!r}")
...
>>> a2 = argset(my_func2)
>>> "foo" in a2
True
>>> "quux" in a2
True
>>> a2.select({"foo": 42, "bar": 23, "quux": 17})
{'foo': 42, 'bar': 23, 'quux': 17}
>>> my_func2(**a2.select({"foo": 42, "bar": 23, "quux": 17}))
foo=42
bar=23
quux=17

API

argset(func: Callable) -> ArgSet

Inspects a callable and returns a summary of its arguments as an ArgSet

class ArgSet

A representation of the arguments taken by a callable. It has the following attributes & properties:

required_positional_only: int

The number of arguments that are positional-only and do not have default values

optional_positional_only: int

The number of arguments that are positional-only and have a default value

positional_only: int

The total number of positional-only arguments

required_args: frozenset[str]

The names of all positional-or-keyword or keyword-only arguments that do not have default values

optional_args: frozenset[str]

The names of all positional-or-keyword or keyword-only arguments that have default values

argnames: frozenset[str]

The names of all positional-or-keyword or keyword-only arguments

takes_args: bool

Whether the callable has an argument of the form *args

takes_kwargs: bool

Whether the callable has an argument of the form **kwargs

ArgSet objects support the in operator; an expression of the form argname in a returns True iff argname is in a.argnames or a.takes_kwargs is True.

ArgSet objects have the following methods:

ArgSet.select(kwargs: Dict[str, Any]) -> Dict[str, Any]

Returns all items in kwargs where the key is the name of a positional-or-keyword or keyword-only argument accepted by the callable. If takes_kwargs is True, the return value is a copy of kwargs.

ArgSet.missing(kwargs: Dict[str, Any]) -> FrozenSet[str]

Returns all keys in required_args that do not appear in kwargs

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

argset-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

argset-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file argset-0.1.0.tar.gz.

File metadata

  • Download URL: argset-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for argset-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8a41ee4789d37517c259984c11f2aa3639a90dc8fa446ff905ecc5fe6623c12d
MD5 8795036c84ede7599bed722d9445611b
BLAKE2b-256 d0ee1c25e68d029e8daaf3228dababbf3261fa5d9569f6f705867b2ad4df9b6d

See more details on using hashes here.

File details

Details for the file argset-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: argset-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for argset-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 107a632c7112faceb9fd6e93658dd461154713db250f7ffde5bd473e17cf1db5
MD5 5fd57f8b97ffe7b2a98449afd293a372
BLAKE2b-256 b52b7aa284f345e37f955d86e4cd57b1039b573552b0fc29d1a522ec05c1ee41

See more details on using hashes here.

Supported by

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