https://github.com/jsfehler/shibari
Bind functions to run only once inside a desired scope.
https://github.com/jsfehler/shibari
python python3 python36
Last synced: 6 months ago
JSON representation
Bind functions to run only once inside a desired scope.
- Host: GitHub
- URL: https://github.com/jsfehler/shibari
- Owner: jsfehler
- License: mit
- Created: 2019-08-27T01:18:18.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-30T20:46:12.000Z (almost 7 years ago)
- Last Synced: 2025-03-19T17:21:56.833Z (over 1 year ago)
- Topics: python, python3, python36
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
shibari
=======
.. image:: https://img.shields.io/pypi/v/shibari.svg
:target: https://pypi.org/project/shibari
:alt: PyPI
.. image:: https://img.shields.io/pypi/pyversions/shibari.svg
:alt: PyPI - Python Version
:target: https://github.com/jsfehler/shibari
.. image:: https://img.shields.io/github/license/jsfehler/shibari.svg
:alt: GitHub
:target: https://github.com/jsfehler/shibari/blob/master/LICENSE
.. image:: https://travis-ci.org/jsfehler/stere.svg?branch=master
:target: https://travis-ci.org/jsfehler/stere
Bind functions to only run once inside a desired scope.
Documentation
-------------
The Rig class exposes two methods: `bind` and `free`. Bind takes one argument: A name for a scope to bind the function.
Functions wrapped with `bind` will be called only once until the scope it is inside is freed.
Functions wrapped with `free` will free all the bound functions in a specific scope after the function's execution.
Example:
.. code-block:: python
import shibari
rig = shibari.Rig('ebi')
@rig.bind('ebi')
def timestamp():
return str(time.time())
@rig.free('ebi')
def finish():
pass
>>> t = timestamp()
>>> t2 = timestamp()
>>> assert t == t2
>>> finish()
>>> t3 = timestamp()
>>> t4 == timestamp()
>>> assert t != t3
>>> assert t3 == t4
Functions that take arguments can be bound, but will always return the same result until they are freed.
Example:
.. code-block:: python
import shibari
rig = shibari.Rig('ebi')
@rig.bind('ebi')
def timestamp(a, b):
return f'{a}_{str(time.time())}_{b}'
>>> t = timestamp('goodbye', 'world')
>>> t2 = timestamp('hello', 'space')
>>> assert t == t2