Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wuthefwasthat/tapystry
A small python library for handling effects, inspired by redux-saga
https://github.com/wuthefwasthat/tapystry
Last synced: about 1 month ago
JSON representation
A small python library for handling effects, inspired by redux-saga
- Host: GitHub
- URL: https://github.com/wuthefwasthat/tapystry
- Owner: WuTheFWasThat
- License: mit
- Created: 2020-06-27T18:06:55.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-01T23:35:07.000Z (about 3 years ago)
- Last Synced: 2024-10-11T15:09:15.817Z (2 months ago)
- Language: Python
- Homepage:
- Size: 89.8 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tapystry
This is a small library for handling effects, inspired by [redux-saga](https://www.github.com/redux-saga/redux-saga).
## Installation
`pip install tapystry`
## Usage
It's most easily understood by example!
```python
import tapystry as tapdef broadcaster(value):
yield tap.Broadcast('key', value)
return "success"def incrementer():
value = 0
while True:
winner, received_val = yield tap.Race(dict(
receive=tap.Receive('key'),
exit=tap.Receive('exit'),
))
if winner == 'exit':
break
else:
assert winner == 'receive'
value += received_val
return valuedef fn():
# fork off a strand that increments
recv_strand = yield tap.CallFork(incrementer)
# broadcast a value to add
success = yield tap.Call(broadcaster, (5,))
assert success == "success"
# equivalent syntax using yield from
success = yield from broadcaster(8)
assert success == "success"
# forked process is not yet done
assert not recv_strand.is_done()
yield tap.Broadcast("exit")
# this value won't get received
success = yield tap.Call(broadcaster, (1,))
assert success == "success"
value = yield tap.Join(recv_strand)
return valueassert tap.run(fn) == 13
```### What's the point?
This lets you isolate side effects really easily.
For example, you could fork a `Strand` that repeatedly polls a database for events.
Then you could have a bunch of independent logic that decides what to do with that.
Everything except the logic touching the database is pure and can be unit tested easily.