A library for modifying Kubernetes manifests and other json-like objects.
Project description
Lens
A library for modifying Kubernetes manifests and other json-like objects.
Examples
You can use lenses for directly getting, setting, or mapping (modifying) data
# set the namespace of a Kubernetes object
namespace = kord("metadata") / KeyLens("namespace")
namespace.l_set(my_pod, "my-ns")
# modify the contents of a Kubernetes secret
admin_password = kord("data") / KeyLens("admin-password") / B64()
admin_password.l_set(my_secret, "super-secret-password")
# get the number of ready replicas of a Kubernetes deployment
ready_replicas = Lens()["status"]["readyReplicas"]
ready_replicas.l_get(my_deployment)
# get the name and resources of every container in a Kubernetes pod
containers = Lens()["spec"]["containers"]
resources = (containers * KeyLens("name")) % (containers * KeyLens("resources"))
# add one more replica to a deployment
replicas = Lens()["spec"]["replicas"]
replicas.l_map(my_deployment, lambda n: n + 1)
You can bind the mapping function to easily apply the same transformation multiple times:
# note how we can re-use `replicas` from above
add_replica = replicas @ (lambda n: n + 1)
add_replica.map(my_deployment)
You can combine several bound lenses to create pipelines
set_namespace = namespace @ Const("my-ns")
labels = kord("metadata") / kord("labels")
add_team_label = (labels / KeyLens("team", None)) @ Const("my_team")
global_pipeline = set_namespace % add_team_label
Usage
Lenses can be built using either their classes or their helpers. The helpers are the most convenient, and look like standard access:
# These are equivalent
l = Lens()["my-key"][0].my_attribute / B64()
l
KeyLens("my-key").l_compose(IndexLens(0)).l_compose(AttrLens("my_attribute")).l_compose(B64())
Lenses support 3 operations. They're all prefixed with "l_" to avoid conflicting with attributes that your objects might have.
l_get: get the value focused by the lensl_set: set the value focused by the lensl_map: use a function to modify the value focused by the lens
Types of Lenses
There are several lenses for moving through objects:
| Name | Description | equivalence and example |
|---|---|---|
| AttrLens | get an attribute | AttrLens("foo") -> object.foo |
| IndexLens | get an item by its index | IndexLens(1) -> object[1] |
| KeyLens | get an item by its key | KeyLens("foo") -> object["foo"] |
| ForEachLens | apply the sublens to all elements of a collection | a for loop |
| CodecLens | decode the item | CodecLens(json.loads, json.dumps) decodes the payload and re-encodes after modification |
| DataclassCodec | instantiate a dataclass from a dict | DataclassCodec(User) |
| FilterLens | focus if a predicate matches | ForEachLens(FilterLens(lambda x: x.value > 0)) will only operate on items whose value is positive |
There are helper lenses for common JSON/YAML operations:
| Name | Description | equivalence and example |
|---|---|---|
| kord | get the key, defaulting to an empty dictionary | kord("labels") -> KeyLens("labels", {}) |
| korl | get the key, defaulting to an empty list | korl("volumes") -> KeyLens("volumes", []) |
Theoretical underpinnings
Lenses have several formulations.
- Profunctors : These are generalisations of functions (think a fancy way of writing
map). The advantage is that optical composition is just function composition. The disadvantage is that this composition is not introspectable. This means that it isn't really possible to report when an optic fails to find an expected value.
Bibliography
- Don't Fear the Profunctor Optics! : a clear walkthrough of concrete optics, profunctors, and profunctor optics. It includes diagrams which visualise the core principle of "just gluing arrows together". It also uses more practical examples for motivation and illustration.
- :
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 alpacloud_lens-0.1.0.tar.gz.
File metadata
- Download URL: alpacloud_lens-0.1.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
894562b42aa67b09e84097965298e5893fb05f73eaa790798e0ee650598eb5bc
|
|
| MD5 |
2674d69e95297d99837d53155b4883c0
|
|
| BLAKE2b-256 |
c0aa0a18d4a0f6e903fc8dc6dfc7883a59efb35d471b56238477567b220325c2
|
File details
Details for the file alpacloud.lens-0.1.0-py3-none-any.whl.
File metadata
- Download URL: alpacloud.lens-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffe98f2430ee1f891697f7e7edca2eefd0a7a71c0188c60dd7b73849f09578dc
|
|
| MD5 |
c1b22a97742ea0c021e986851b2604db
|
|
| BLAKE2b-256 |
3e1e934b9fa3ea28d7d9ff1a2a5c81530c30a0ac0197e6a7e38bb60eea24e42a
|