No project description provided
Project description
kungfu
⚙️ Functional typing in Python!
get_user()
.then(get_posts)
.ensure(lambda posts: len(posts) > 0, "User has no posts")
.map(lambda posts: sum(post.views for post in posts) / len(posts))
.unwrap()
why kungfu?
kungfu is based on the belief that raising exceptions should be avoided. So it defines a set of functional types needed to write better code. This type strategy grants you with higher control over your runtime.
Panicking is the last recourse but for some obscure reason spawning exceptions each time you encounter something other than the all-successful behaviour became a norm in python code.
Let's fix this up and instead of panicking do treat error-state as equal to successful-state. kungfu provides you with all you need to migrate to functional typing approach
Improving control flow will definitely result in avoiding logical errors and getting better code readability: you start to see each kind of behaviour you get from the function.
why "kungfu" as a name? writing in kungfu requires some discipline. kung fu literally meaning "hard work" reflects on this concept. through the hard work on gaining control over our types we get clarity and power
(📖) documentation
See documentation
See examples
examples
howto build monad chains:
def get_user(user_id: int) -> Result[User, str]:
...
def get_posts(user: User) -> Result[list[Post], str]:
...
def get_average_views(user_id: int) -> Result[int, str]:
return (
get_user(user_id)
.then(get_posts)
.ensure(len, "User has no posts")
.map(lambda posts: sum(post.views for post in posts) / len(posts))
)
avg_views = get_average_views().unwrap()
howto detalize function result:
@unwrapping
def send_funds(
sender_id: int,
receiver_id: int,
amount: decimal.Decimal,
) -> Result[TransactionID, str]:
sender = get_user(sender_id).expect("Sender is undefined")
receiver = get_user(receiver_id).expect("Receiver is undefined")
if sender.get_balance().expect("Could not get sender balance") < amount:
return Error("Sender has not enough funds to complete transaction")
return Ok(
create_transaction(sender, receiver, amount)
.unwrap()
.transaction_id
)
Contributions are welcome
MIT licensed
awesome kungfu projects
These projects are awesome to extend kungfu environment:
-
nodnod - dependency injection with efficient nodes computation agents and dependency scoping
-
combinators.py - build explicit functional pipelines
These projects implement kungfu environment in their specific utility cases:
- telegrinder - telegram bot framework
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kungfu_fp-1.0.0.tar.gz.
File metadata
- Download URL: kungfu_fp-1.0.0.tar.gz
- Upload date:
- Size: 36.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3e4bbaf096f3ee532e800b6c548c80ea53c321f1205725880fc41750a9c0c49
|
|
| MD5 |
09c8a93c4a1ef644273ec969ed5e1d8d
|
|
| BLAKE2b-256 |
d74deabe910c80edb9972393f9b623b30c0cc433f454905b4ac60f5223f7b628
|
File details
Details for the file kungfu_fp-1.0.0-py3-none-any.whl.
File metadata
- Download URL: kungfu_fp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd61cc0a7bbac979bf914322e384148cb43db9c3645635631c0aad5adc6b925d
|
|
| MD5 |
d2796bab95de66fa76532c5d9cad57e2
|
|
| BLAKE2b-256 |
3d6048d1f811515a4ce3f5430b48a4382f13a6f3f7ff648dd090235b69c77f65
|