Easily define and reuse complex Python objects that serialize into JSON or YAML.
Low maintenance activity. Last release over 2 years ago, commits in 1 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install kadetREADME as published on PyPI with version 0.3.2.
kadet
Easily define and reuse complex Python objects that serialize into JSON or YAML.
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.