https://github.com/5monkeys/partialize
Python partial on steroids
https://github.com/5monkeys/partialize
Last synced: about 1 year ago
JSON representation
Python partial on steroids
- Host: GitHub
- URL: https://github.com/5monkeys/partialize
- Owner: 5monkeys
- License: mit
- Created: 2013-05-08T12:46:44.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2013-05-14T11:11:00.000Z (about 13 years ago)
- Last Synced: 2025-04-14T01:15:00.349Z (about 1 year ago)
- Language: Python
- Size: 160 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
Partialize
==========
Python partial on steroids
.. image:: https://travis-ci.org/5monkeys/partialize.png?branch=master
:target: http://travis-ci.org/5monkeys/partialize
Installation
------------
Install partialize in your python environment
.. code:: sh
$ pip install partialize
Usage
-----
Partialize is like python :code:`functools.partial` but with change/extend access to args and kwargs.
Also a great tool even if no partial is needed but the function takes args/kwargs that often depends on logic.
Easiest used as a decorator
.. code:: python
from partialize import partialize
@partialize
def dummy(a, b, c=None):
return 'hello world'
partial_dummy = dummy.partial(1)
partial_dummy.b = 2
partial_dummy(c=3)
or invoked inline...
.. code:: python
partial_dummy = partialize(dummy)
partial_dummy.a = 1
partial_dummy.update(b=2)
partial_dummy()
partial_dummy(c=3)
Use it on functions that needs logic to affect arguments passed, instead of building and passing a `dict` as kwargs
which often gets quite messy and hard to read.
Dict kwargs example:
.. code:: python
from partialize import partialize
@partialize
def search_products(site, query=None, brand=None, tags=None):
pass
kwargs = {}
if logic:
kwargs['query'] = q
if more_logic:
kwargs['brand'] = 'brand name'
products = search_products(site, **kwargs)
Partialize example:
.. code:: python
search = search_products.partial(site)
if logic:
search.query = q
if more_logic:
search.brand = 'brand name'
products = search()
..
**Note:** function argument names are validated when set, unlike dict string keys.