Unobstrusive functional programming in Python.
Project description
Purely 💧
A lightweight elixir for cleaner, safer, and more fluent Python.
Purely is a zero-dependency library designed to bring the best parts of functional programming--safety, pipelines, and immutability--into Python without the academic overhead.
📦 Installation
Purely requires Python 3.12+ (due to modern generic syntax).
pip install purely
✨ Features at a Glance
1. ensure: The Rusty Unwrap
Stop writing multiline if x is None checks. Assert existence in one line.
from purely import ensure
# ❌ Old Way
user_id = get_id()
if user_id is None:
raise ValueError("User ID missing!")
# ✅ The Purely Way
user_id = ensure(get_id(), "User ID missing!")
2. Chain: The Fluent Pipe
Read your code from top-to-bottom, not inside-out. Use the | operator to pipe values through functions.
from purely import Chain
def double(x): return x * 2
def shout(x): return f"{x}!"
# ❌ Old Way (Nested Hell)
result = shout(double(5))
# ✅ The Purely Way
result = Chain(5).map(double).map(shout).value
# 🚀 The "Super Duper" Way (Pipe Operator)
result = Chain(5) | double | shout
# Now result == "10!"
3. Option: Type-Safe Null Handling
Handle optional values gracefully. If a value becomes None mid-stream, the chain handles it safely instead of crashing.
from purely import Option
result = (
Option.of(10)
.map(lambda x: x * 2) # 20
.filter(lambda x: x > 100) # Becomes None (20 is not > 100)
.map(lambda x: x + 5) # Skipped (because it's None)
.unwrap(default=0) # Returns 0 safely
)
📚 API Reference
Core Utilities
| Function | Description |
|---|---|
ensure(val, error) |
Returns val if not None, otherwise raises error. |
tap(val, func) |
Runs func(val) for side effects (logging, etc) and returns val unchanged. |
pipe(val, *funcs) |
Pushes val through a list of functions in order. |
Chain[T]
The "Identity Monad" for fluent programming.
.map(func)/.then(func): Transforms the value.| func: Syntactic sugar for.map(func)..tap(func): Run side effects..ensure(error): Crash if the inner value is None..value: Access the raw value (property).
Option[T]
The "Maybe Monad" for safety.
.map(func): Transforms value only if it exists..filter(predicate): Turns value toNoneif predicate fails..unwrap(default=..., error=...): Extracts value, returns default, or raises error.
🤝 Collaboration
We believe in pure code and open collaboration!
- Fork the repository.
- Create your Feature Branch (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'Add some AmazingFeature'). - Push to the branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
Please ensure you write tests for any new features (we use pytest).
📝 License
Distributed under the MIT License. See LICENSE for more information.
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 purely-0.1.2.tar.gz.
File metadata
- Download URL: purely-0.1.2.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82e2739c97d9c042fe047143555a4b393d3a01ccfdd18c554a9e6db1c744920c
|
|
| MD5 |
ee2b2333e85b5be6154408ec6455e20f
|
|
| BLAKE2b-256 |
9c7ed37d506c97eb7c98dfdf93c453b899d618f558c06d581c50fc7cb4fa0363
|
File details
Details for the file purely-0.1.2-py3-none-any.whl.
File metadata
- Download URL: purely-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b176a2093ccbdcb186277d45f2a38de2faf547bdd249b339df69efce428f212
|
|
| MD5 |
4691729c8a380629638543375408fe07
|
|
| BLAKE2b-256 |
1d17d79ea215f675b8ed635ccfe4014753fcdcae1183ab4c9efc66e1158cca58
|