Skip to main content

Signature-preserving function delegation

Project description

delegatefn

A Python package for adding the parameters of a delegatee function to a delegator function, while keeping the original parameters of the delegator function.

Installation

To install delegatefn, use pip:

pip install delegatefn

Usage

To use the delegate decorator function, import it from delegatefn:

from delegatefn import delegate

Then, decorate your delegator function with the delegate decorator, passing in the delegatee function as an argument:

@delegate(delegatee)
def delegator(c: int, d: int, e: int, **kwargs):
    ...

The delegator function will now have the parameters of delegatee added to it, while keeping its original parameters.

You can also customize the behavior of the delegate decorator by passing in the following keyword arguments:

  • kwonly: A boolean value indicating whether the parameters of delegatee should be converted to keyword-only arguments. The default value is True.

  • delegate_docstring: A boolean value indicating whether the docstring of delegatee should be used as the docstring of the delegator function. The default value is True.

  • ignore: A set of strings containing the names of the parameters of delegatee that should be ignored. The default value is an empty set.

Here is an example of how to use these keyword arguments:

@delegate(delegatee, kwonly=True, delegate_docstring=True, ignore={"a", "b"})
def delegator(c: int, d: int, e: int, **kwargs):
    ...

Example

Here is an example of how to use the delegate decorator:

from delegatefn import delegate

def delegatee(a: int, b: int, **kwargs):
    ...

@delegate(delegatee)
def delegator(c: int, d: int, e: int, **kwargs):
    ...

The delegator function will now have the parameters a: int, b: int, and kwargs added to it, in addition to its original parameters c: int, d: int, and e: int.

Acknowledgements

This approach was inspired by fast.ai.

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

delegatefn-0.3.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

delegatefn-0.3.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file delegatefn-0.3.1.tar.gz.

File metadata

  • Download URL: delegatefn-0.3.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0

File hashes

Hashes for delegatefn-0.3.1.tar.gz
Algorithm Hash digest
SHA256 353a99545cb42908f00f2dec4dd53c778db93c01f370b68a62f678b2dd3ccd04
MD5 9f8cbf4904d5bcec9ad4eee58e662a7a
BLAKE2b-256 4b32587b0a8a25699775863aa85edb77daad40a43e064990f40508c1c2d4e2d3

See more details on using hashes here.

File details

Details for the file delegatefn-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: delegatefn-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Darwin/22.1.0

File hashes

Hashes for delegatefn-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aaa0f11304a3a8e61b06c4b9be1b60887c7f4b092e509e42b62449d76daa67a1
MD5 1169c5e7ba56f865ef009b979b4f33d1
BLAKE2b-256 8427a0e708f8edcc9abd440d868095cbbb1c3b565083db08cf3c8db989492fd9

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