Skip to main content

kadet

Easily define and reuse complex Python objects that serialize into JSON or YAML.

GitHub Workflow Status

Example

from kadet import BaseObj
from pprint import pprint

ships = BaseObj()
ships.root.type.container = ["panamax", "suezmax", "post-panamax"]
ships.root.type.carrier = ["conventional", "geared", "gearless"]
ships.root.type.tanker = BaseObj.from_yaml("tankers.yml")

pprint(ships.root)

# output
{'type': {'carrier': ['conventional',
                      'geared',
                      'gearless'],
          'container': ['panamax',
                        'suezmax',
                        'post-panamax'],
          'tanker': ['oil', 'liquified-gas', 'chemical']}}

Installation

Install using pip install kadet.

Overview

BaseObj

BaseObj implements the basic object that serializes into JSON or YAML. Setting keys in self.root means they will be serialized. Keys can be set as an hierarchy of attributes.

The self.body() method is reserved for setting self.root on instantiation.

The example below:

class MyApp(BaseObj):
  def body(self):
    self.root.name = "myapp"
    self.root.inner.foo = "bar"
    self.root.list = [1, 2, 3]

yaml.dump(MyApp().dump())

serializes into:

---
name: myapp
inner:
  foo: bar
list:
  - 1
  - 2
  - 3

The self.new() method can be used to define a basic constructor.

self.need() checks if a key is set and errors if it isn't (with an optional custom error message). self.optional() sets a key as optional. Use default keyword to set default value when not set.

Both self.new() and self.body() method accept the istype keyword to validate value type on runtime. Supports typing types.

kwargs that are passed onto a new instance of BaseObj are always accessible via self.kwargs

self.new_with() is an utility method to call super().new() while passing kwargs to the super class.

In this example, MyApp needs name and foo to be passed as kwargs.

class MyApp(BaseObj):
  def new(self):
    self.need("name")
    self.need("foo", msg="please provide a value for foo")
    self.optional("baz")

  def body(self):
    self.root.name = self.kwargs.name
    self.root.inner.foo = self.kwargs.foo
    self.root.list = [1, 2, 3]

obj = MyApp(name="myapp", foo="bar")

Setting a skeleton

Defining a large body with Python can be quite hard and repetitive to read and write.

The self.root_file() method allows importing a YAML/JSON file to set self.root.

MyApp's skeleton can be set instead like this:

#skel.yml
---
name: myapp
inner:
  foo: bar
list:
  - 1
  - 2
  - 3
class MyApp(BaseObj):
  def new(self):
    self.need("name")
    self.need("foo", msg="please provide a value for foo")
    self.root_file("path/to/skel.yml")

Extending a MyApp's skeleton is possible just by implementing self.body():

class MyApp(BaseObj):
  def new(self):
    self.need("name")
    self.need("foo", msg="please provide a value for foo")
    self.root_file("path/to/skel.yml")

  def body(self):
    self.set_replicas()
    self.root.metadata.labels = {"app": "mylabel"}

  def set_replicas(self):
    self.root.spec.replicas = 5

Inheritance

Python inheritance will work as expected:

class MyOtherApp(MyApp):
  def new(self):
    super().new()  # MyApp's new()
    self.need("size")

  def body(self):
    super().body()  #  we want to extend MyApp's body
    self.root.size = self.kwargs.size
    del self.root.list  # get rid of "list"

obj = MyOtherApp(name="otherapp1", foo="bar2", size=3)
yaml.dump(obj.dump())

serializes to:

---
name: otherapp1
inner:
  foo: bar2
replicas: 5
size: 3

BaseModel

BaseModel integrates Kadet semantics with Pydantic's BaseModel together with powerful data validation and type hinting features. Just like in BaseObj, keys in self.root will be serialized, but kwargs is no longer necessary as BaseModel's parameters are set as attributes in self.

The self.body() method is reserved for setting self.root on instantiation.

The example below:

class Boat(BaseModel):
  name: str  # Required
  length: int  # Required
  description: str = "I am a boat"  # Default description

  def body(self):
    self.root.name = self.name
    self.root.details.length = self.length
    self.root.details.description = self.description

print(yaml.dump(Boat(name="Boaty", length=600).dump()))

---
details:
  description: I am a boat
  length: 600
name: Boaty

Metadata

Release files for kadet 0.3.2

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

Source distribution (sdist)

Source distribution for kadet 0.3.2
File Size Uploaded
kadet-0.3.2.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kadet 0.3.2
File Interpreter ABI Platform
kadet-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.3 kB

Release files / kadet-0.3.2.tar.gz

Download URL kadet-0.3.2.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c746e341d7cf8b0c623344cb033e9fd09bbbda57e4cc664e45e6c2897c772860
BLAKE2b-256 checksum
How to use checksums
576c0a257cd62ce85a3360b9b4b191958f4afb0b3fd919b3ac4570b176e78528
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1018-azure

Release files / kadet-0.3.2-py3-none-any.whl

Download URL kadet-0.3.2-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e801fe01d1f9baf169fa176c8ed046e654f437162fe8e88e88281eea9bf94d12
BLAKE2b-256 checksum
How to use checksums
be14134502054b3fd355e1b8c4d36ccddf08b79dcd26fd3d7f6caa46edf88e6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.5.0-1018-azure

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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