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:

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kungfu_fp-1.1.1.tar.gz (36.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kungfu_fp-1.1.1-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file kungfu_fp-1.1.1.tar.gz.

File metadata

  • Download URL: kungfu_fp-1.1.1.tar.gz
  • Upload date:
  • Size: 36.0 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for kungfu_fp-1.1.1.tar.gz
Algorithm Hash digest
SHA256 1735243d9f40ca1c14192fccf74bc025e7656fcd35b5846ecd90c1d9219f2dac
MD5 8241313ef539b4ddc33777daacc24e85
BLAKE2b-256 7b054178794dc60a907cf6e9c1bb1190a572ad05c0fb75882a846846c6af6ba0

See more details on using hashes here.

File details

Details for the file kungfu_fp-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: kungfu_fp-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for kungfu_fp-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 511f82fa5f63bc16dca79e937682ffd592a61e431d7d10953321dbf0784dd2c2
MD5 7960ed4b23253141171e364d00baacb9
BLAKE2b-256 da8056cc23b0d38b116c7da27d0f24303f9b3184250925db506fe8b0a9fdeeee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page