Library for converting yaml structures to Python objects, based on a predefined object hierarchy schema.
Project description
# Wattle
Library for converting yaml structures to Python objects, based on a predefined object hierarchy schema.
Given a schema of class hierarchy
```python
from wattle import load_schema
from wattle.nodes import Nested, Value
class MessagePrinter:
indent = Value(int)
capitalize = Value(bool)
def print(self, message):
if self.indent:
message = (" " * self.indent) + message
if self.capitalize:
message = message.upper()
print(message)
class HelloWorld:
message = Value(str)
how_many_times = Value(int, default=25)
printer = Nested(MessagePrinter)
def print(self):
for _ in range(self.how_many_times):
self.printer.print(self.message)
if __name__ == '__main__':
schema = load_schema(HelloWorld)
root = schema.read('examples/hello_world/input.yml')
root.print()
```
and the input yaml file
```yaml
message: Hello world
how_many_times: 5
printer:
indent: 4
capitalize: yes
```
Will result in an already populated object
```
assert root.message == 'Hello world'
assert isinstance(root.printer, MessagePrinter)
root.print()
```
Library for converting yaml structures to Python objects, based on a predefined object hierarchy schema.
Given a schema of class hierarchy
```python
from wattle import load_schema
from wattle.nodes import Nested, Value
class MessagePrinter:
indent = Value(int)
capitalize = Value(bool)
def print(self, message):
if self.indent:
message = (" " * self.indent) + message
if self.capitalize:
message = message.upper()
print(message)
class HelloWorld:
message = Value(str)
how_many_times = Value(int, default=25)
printer = Nested(MessagePrinter)
def print(self):
for _ in range(self.how_many_times):
self.printer.print(self.message)
if __name__ == '__main__':
schema = load_schema(HelloWorld)
root = schema.read('examples/hello_world/input.yml')
root.print()
```
and the input yaml file
```yaml
message: Hello world
how_many_times: 5
printer:
indent: 4
capitalize: yes
```
Will result in an already populated object
```
assert root.message == 'Hello world'
assert isinstance(root.printer, MessagePrinter)
root.print()
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
woning-wattle-0.3.0.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for woning_wattle-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f67f5b06502f9e6bd7afe3cd59db7ddb41f37791d93f222cde6fa72a574c0acb |
|
MD5 | 617f9aa331d59e830f2c037acb776d20 |
|
BLAKE2b-256 | 3387e12d53000f68285dccf1108a7be04317bd4414e0879c4a5812272d11722e |