Skip to main content

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
    )

install

pip install kungfu-fp

Contributions are welcome

MIT licensed


awesome kungfu projects

Awesome

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:

Metadata

Release files for kungfu-fp 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kungfu-fp 1.1.1
File Size Uploaded
kungfu_fp-1.1.1.tar.gz 36.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kungfu-fp 1.1.1
File Interpreter ABI Platform
kungfu_fp-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 55.1 kB

Release files / kungfu_fp-1.1.1.tar.gz

Download URL kungfu_fp-1.1.1.tar.gz
Size 36.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1735243d9f40ca1c14192fccf74bc025e7656fcd35b5846ecd90c1d9219f2dac
BLAKE2b-256 checksum
How to use checksums
7b054178794dc60a907cf6e9c1bb1190a572ad05c0fb75882a846846c6af6ba0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / kungfu_fp-1.1.1-py3-none-any.whl

Download URL kungfu_fp-1.1.1-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
511f82fa5f63bc16dca79e937682ffd592a61e431d7d10953321dbf0784dd2c2
BLAKE2b-256 checksum
How to use checksums
da8056cc23b0d38b116c7da27d0f24303f9b3184250925db506fe8b0a9fdeeee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page