Parsing mixin which converts ``data class instance``, ``dict object``, ``json string`` and ``yaml string`` each other.
Project description
Parsing mixin which converts data class instance, dict object, json string and yaml string each other.
Motivation
Have you ever wanted to write robust code in Python? This library will make such your wishes come true.
Define your data class which is extend OwlMixin, you can use some useful methods which help your codes robust. See following Example and API sections.
Example
You don’t need to use typing necessarily, but I recommend to use this to make your codes more robust. See PEP 484 – Type Hints.
from typing import Text, Optional
from owlmixin import OwlMixin
class Food(OwlMixin):
def __init__(self, id, name, color=None):
self.id = id # type: Text
self.name = name # type: Text
self.color = color # type: Optional[Text]
class Human(OwlMixin):
def __init__(self, id, name, favorite):
self.id = id # type: Text
self.name = name # type: Text
self.favorite = Food.from_dict2list(favorite) # type: TList[Food]
jiro = Human.from_dict({
"id": 10,
"name": "jiro",
"favorite": [
{"id": 1, "name": "apple"},
{"id": 2, "name": "orange", "color": "white"}
]
})
>>> jiro.id
10
>>> jiro.name
'jiro'
>>> print(jiro.favorite[0].to_pretty_json())
{
"color": null,
"id": 1,
"name": "apple"
}
>>> print(jiro.to_dict())
{'favorite': [{'color': None, 'id': 1, 'name': 'apple'}, {'color': 'white', 'id': 2, 'name': 'orange'}], 'id': 10, 'name': 'jiro'}
>>> print(jiro.to_dict(ignore_none=True))
{'favorite': [{'id': 1, 'name': 'apple'}, {'color': 'white', 'id': 2, 'name': 'orange'}], 'id': 10, 'name': 'jiro'}
API
From instance to another
- to_dict
instance => dict
TDict[instance] => dict
- to_dicts
TList[instance] => List[dict]
- to_json
instance => json string
- to_pretty_json
instance => json string (has indent and line break)
- to_yaml
instance => yaml string
From something to instance
Installation
pip install owlmixin
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 Distributions
Built Distribution
Hashes for owlmixin-1.0.0b6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7afa0f2fbdc20d54d4bc156d4e8d1e8a6f970cd1ea6dae39ee9ce9d857887bec |
|
MD5 | 50fc512e2e4a1c24e6ead414d12eb2b2 |
|
BLAKE2b-256 | 211920b9623268349182efea9cf084764e8ea21e74dc7c790ead1e3d55466364 |