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.0.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.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kungfu_fp-1.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7a8cc753cfbf88de058267deda34ccd19751088489bdaceacf5d6709a979019e
MD5 6d3dc825cc04ad79b773076169fe9614
BLAKE2b-256 75e1bfed503150a2c081cab7f45f3a14adc000045382754e5f844ae4cb350707

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kungfu_fp-1.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40cffe634271175b70a8175a320e16a576cd2f2417b05e79c474ce0678751acb
MD5 2fbd190a812824995b9bfaff0af69d48
BLAKE2b-256 e4f635151df9f0dfc71cff797bf0700c7af37d4f9f8168a21603aa41b1cd6a76

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