# bindpy
The bindpy package allows for partial application of arguments to a function, making it easy to create specialized versions of the function with some arguments pre-filled.
It is a better version of the Python's standard `partial` function from the `functools` package inspired by C++'s `std::bind`.
## Install
Expect gracefully crafted support for any version of Python 3+, but confidently tested in version 3.7 and higher.
```
pip install bindpy
```
## Usage
```python
from bindpy import *
```
### `bind` function
The `show` function takes three arguments, `a1`, `a2` and `a3`, and returns a string composed of their values separated by spaces. The `show_10` function is a partially applied version of `show`, with `a2` bound to `_1`, `a1` bound to `_2` and `a3` bound to `10`.
Bind support placeholders : `_1`, `_2`, ... `_10`. The placeholders allow you to partially apply a function and leave certain arguments to be filled in later. This allows you to reuse the partially applied function and pass different values for the placeholder argument.
Overall, `bind` and placeholders make it easier to create reusable and composable functions by allowing you to fix certain arguments and create new functions that take fewer arguments.
```python
def show(a1, a2, a3):
return " ".join(map(str, [a1, a2, a3]))
show_10 = bind(show, _2, _1, a3=10)
print(show_10(20, 30)) # output: 30 20 10
```
***Convenient to use with functional style.***
If you find lambda expressions unappealing, you can use bind for a more convenient and aesthetically pleasing experience with functional programming.
```python
def add(a, b, c):
return a + b * c
numbers = [1, 2, 3, 4]
print(list(map(bind(add, _1, 10, 2), numbers))) # output 21 22 23 24
# same code with lambda
print(list(map(lambda x: add(x, 10, 2), numbers)))
```
```python
import os # used for example
files = ['a.txt', 'b.json']
my_join = bind(os.path.join, '.', 'data')
print(list(map(my_join, files))) # output ['./data/a.txt', './data/b.json']
```
### `bind_front` function
`bind_front` pre-specifies function arguments like `functools.partial`. It takes a function and values, returns new function with values bound to front. When called with remaining args, values passed to `bind_front` are automatically inserted in front.
```python
def add(a, b, c=0):
return a + b + c
add_10 = bind_front(add, 10)
result = add_10(20, c=30)
print(result) # 60
add_20_30 = bind_front(add, 20, 30)
result = add_20_30() # call add(20, 30)
print(result) # 50
```
### `bind_back` function
`bind_back` also pre-specifies function arguments but with values bound to end of arg list after all others. It takes a function and key-value pairs, returns new function with values bound to end. When called with remaining args, values passed to `bind_back` are automatically inserted at end.
```python
add_30 = bind_back(add, c=30)
result = add_30(10, 20)
print(result) # 60
add_40 = bind_back(add, 40)
result = add_40(10, 20) # call add(10, 20, 40)
print(result) # 70
add_10 = bind_back(add, 10)
result = add_10(12) # call add(10, 12), c=0 by default
print(result) # 22
```
### sequential binding
You can combine `bind_front` and `bind_back` to create a function that has arguments pre-specified at both the front and end of the argument list.
For example, the code:
```python
def func(p1, p2, p3, p4, p5):
return " ".join(map(str, [p1, p2, p3, p4, p5]))
b_func = bind_front(bind_back(func, 4, 5), 1, 2)
print(bfunc(3)) # 1 2 3 4 5
```
can be replaced with:
```python
b_func_v2 = bind(1, 2, _1, 4, 5) # using placeholder *_1*
print(bfunc(3)) # 1 2 3 4 5
```
----
We hope this information helps you effectively use the bind functions in your project. If you have any questions or feedback, please reach out to us. Happy coding!
----
## Acknowledgements
We would like to express our sincere gratitude to all the individuals who have made this project a reality. Their contributions, guidance, and support have been invaluable. Thank you to everyone who has played a part in bringing this project to life.
* [Daniil Dudkin](https://github.com/unterumarmung)
* ChatGPT
Raw data
{
"_id": null,
"home_page": "https://github.com/avdosev/bindpy",
"name": "bindpy",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.0",
"maintainer_email": "",
"keywords": "bind,bindpy,bind_front,bind_back,partial,functools",
"author": "Nikita Avdosev",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/e3/6a/7a85098402c39e4d9d490bdd766c8e07811fde620ecd50ca4abe0c3cc0a3/bindpy-0.1.1.tar.gz",
"platform": null,
"description": "# bindpy\n\nThe bindpy package allows for partial application of arguments to a function, making it easy to create specialized versions of the function with some arguments pre-filled.\nIt is a better version of the Python's standard `partial` function from the `functools` package inspired by C++'s `std::bind`.\n\n## Install\n\nExpect gracefully crafted support for any version of Python 3+, but confidently tested in version 3.7 and higher.\n\n```\npip install bindpy\n```\n\n## Usage\n\n\n```python\nfrom bindpy import *\n```\n\n### `bind` function\n\nThe `show` function takes three arguments, `a1`, `a2` and `a3`, and returns a string composed of their values separated by spaces. The `show_10` function is a partially applied version of `show`, with `a2` bound to `_1`, `a1` bound to `_2` and `a3` bound to `10`.\n\nBind support placeholders : `_1`, `_2`, ... `_10`. The placeholders allow you to partially apply a function and leave certain arguments to be filled in later. This allows you to reuse the partially applied function and pass different values for the placeholder argument.\n\nOverall, `bind` and placeholders make it easier to create reusable and composable functions by allowing you to fix certain arguments and create new functions that take fewer arguments.\n\n```python\ndef show(a1, a2, a3):\n return \" \".join(map(str, [a1, a2, a3]))\n \nshow_10 = bind(show, _2, _1, a3=10)\n\nprint(show_10(20, 30)) # output: 30 20 10\n```\n\n***Convenient to use with functional style.*** \n\nIf you find lambda expressions unappealing, you can use bind for a more convenient and aesthetically pleasing experience with functional programming.\n\n```python\ndef add(a, b, c):\n return a + b * c\n \nnumbers = [1, 2, 3, 4]\nprint(list(map(bind(add, _1, 10, 2), numbers))) # output 21 22 23 24\n# same code with lambda\nprint(list(map(lambda x: add(x, 10, 2), numbers)))\n```\n\n```python\nimport os # used for example\n\nfiles = ['a.txt', 'b.json']\nmy_join = bind(os.path.join, '.', 'data')\nprint(list(map(my_join, files))) # output ['./data/a.txt', './data/b.json']\n```\n### `bind_front` function\n\n`bind_front` pre-specifies function arguments like `functools.partial`. It takes a function and values, returns new function with values bound to front. When called with remaining args, values passed to `bind_front` are automatically inserted in front.\n\n```python\ndef add(a, b, c=0):\n return a + b + c\n\n\nadd_10 = bind_front(add, 10)\nresult = add_10(20, c=30)\nprint(result) # 60\n\nadd_20_30 = bind_front(add, 20, 30)\nresult = add_20_30() # call add(20, 30)\nprint(result) # 50\n```\n\n### `bind_back` function\n\n`bind_back` also pre-specifies function arguments but with values bound to end of arg list after all others. It takes a function and key-value pairs, returns new function with values bound to end. When called with remaining args, values passed to `bind_back` are automatically inserted at end.\n\n```python\nadd_30 = bind_back(add, c=30)\nresult = add_30(10, 20)\nprint(result) # 60\n\nadd_40 = bind_back(add, 40)\nresult = add_40(10, 20) # call add(10, 20, 40)\nprint(result) # 70\n\nadd_10 = bind_back(add, 10)\nresult = add_10(12) # call add(10, 12), c=0 by default\nprint(result) # 22\n```\n\n### sequential binding\n\nYou can combine `bind_front` and `bind_back` to create a function that has arguments pre-specified at both the front and end of the argument list. \nFor example, the code:\n```python\ndef func(p1, p2, p3, p4, p5):\n return \" \".join(map(str, [p1, p2, p3, p4, p5]))\n \nb_func = bind_front(bind_back(func, 4, 5), 1, 2)\nprint(bfunc(3)) # 1 2 3 4 5\n```\n\ncan be replaced with:\n\n```python\nb_func_v2 = bind(1, 2, _1, 4, 5) # using placeholder *_1*\nprint(bfunc(3)) # 1 2 3 4 5\n```\n\n----\n\nWe hope this information helps you effectively use the bind functions in your project. If you have any questions or feedback, please reach out to us. Happy coding!\n\n----\n\n## Acknowledgements\n\nWe would like to express our sincere gratitude to all the individuals who have made this project a reality. Their contributions, guidance, and support have been invaluable. Thank you to everyone who has played a part in bringing this project to life.\n\n* [Daniil Dudkin](https://github.com/unterumarmung)\n* ChatGPT\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "The bindpy package allows for partial application of arguments to a function, making it easy to create specialized versions of the function with some arguments pre-filled.",
"version": "0.1.1",
"split_keywords": [
"bind",
"bindpy",
"bind_front",
"bind_back",
"partial",
"functools"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "38369e69c060474bcabdea3fd8c45cdcdfb632d369f5b8ec1f0297bb0d721ecf",
"md5": "7b1deab42b0c8a774c273fb381e5ea30",
"sha256": "bc40f7de5adc773f2de1bc59879cc7933575d38ddf60669ab113df8346fbd668"
},
"downloads": -1,
"filename": "bindpy-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7b1deab42b0c8a774c273fb381e5ea30",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.0",
"size": 5988,
"upload_time": "2023-02-06T21:38:11",
"upload_time_iso_8601": "2023-02-06T21:38:11.845507Z",
"url": "https://files.pythonhosted.org/packages/38/36/9e69c060474bcabdea3fd8c45cdcdfb632d369f5b8ec1f0297bb0d721ecf/bindpy-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e36a7a85098402c39e4d9d490bdd766c8e07811fde620ecd50ca4abe0c3cc0a3",
"md5": "b20539a5110f13df36f8d82c59ec9faa",
"sha256": "0015f018f9e319192b4cd5a6da378662058798940dfd7bd31963e366a2ee4dcf"
},
"downloads": -1,
"filename": "bindpy-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "b20539a5110f13df36f8d82c59ec9faa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.0",
"size": 5341,
"upload_time": "2023-02-06T21:38:14",
"upload_time_iso_8601": "2023-02-06T21:38:14.108675Z",
"url": "https://files.pythonhosted.org/packages/e3/6a/7a85098402c39e4d9d490bdd766c8e07811fde620ecd50ca4abe0c3cc0a3/bindpy-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-06 21:38:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "avdosev",
"github_project": "bindpy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "bindpy"
}