-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10d0ccb
commit f54dc0e
Showing
1 changed file
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,22 +35,82 @@ pprint(some_other_dict) | |
real example | ||
```py | ||
from flask import Flask, jsonify, request | ||
from my_models import db, User, Order #NOTE: today kefir does not support all db relations :p | ||
from my_models import db, User, Order #NOTE: today kefir does not support nested relations and many2may probable too :| | ||
from kefir import Kefir | ||
app = Flask(__name__) | ||
kef = Kefir( | ||
session=db.session, | ||
shorcuts={'users':'user'}, | ||
objects={'users':User} | ||
objects={'users':User}, | ||
rels={'users':['orders']} | ||
) | ||
@app.get('/orders/<int:order_id>') | ||
@app.get('/orders/<int:order_id>/') | ||
def order_view(order_id): | ||
order = Order.query.get(order_id) # Order(id=4,adress='some', bill=123, user_id=42) | ||
order_dict = kef.dump(order) # {'id':4, 'adress':'some','bill':123,'user':{'id':42,'name':'Kefir', 'email':'[email protected]'}} | ||
return jsonify(order) | ||
@app.get('/users/<int:user_id>/') | ||
def user_view(user_id): | ||
user = User.query.get(user_id) #User(id=42, name='Kefir', email='[email protected]') | ||
user_dict = kef.dump(user) # {'id':42,'name':'Kefir','email':'[email protected]','orders'=[{'id':4,'adress':'some','bill':123}, {'id': 101,'adress':'another','bill':321}]} | ||
return jsonify(user_dict) | ||
|
||
if __name__ == '__main__: | ||
app.run() | ||
``` | ||
Also see this example: | ||
```py | ||
from dataclasses import dataclass | ||
from pprint import pprint | ||
from kefir import Kefir | ||
|
||
class A: | ||
def __init__(self, attr, attr2): | ||
self.attr = attr | ||
self.attr2 = attr | ||
class B: | ||
def __init__(self, attr, attr2, a): | ||
self.attr = attr | ||
self.attr2 = attr2 | ||
self.a = a | ||
class AWithSlots: | ||
__slots__ = ['attr', 'attr2', 'b'] | ||
def __init__(self, attr, attr2, b): | ||
self.attr = attr | ||
self.attr2 = attr2 | ||
self.b = b | ||
@dataclass | ||
class BDataClass: | ||
attr:str | ||
attr2:int | ||
c:AWithSlots | ||
|
||
kef = Kefir() | ||
a = A('kefir', 0.2) | ||
b = B('is', 7, a) | ||
c = AWithSlots('the', 42, b) | ||
d = BDataClass('best!', 10101, c) | ||
|
||
pprint(kef.dump(a)) | ||
pprint(kef.dump(b)) | ||
pprint(kef.dump(c)) | ||
pprint(kef.dump(d)) | ||
""" | ||
Output: | ||
{'attr': 'kefir', 'attr2': 'kefir'} | ||
{'a': {'attr': 'kefir', 'attr2': 'kefir'}, 'attr': 'is', 'attr2': 7} | ||
{'attr': 'the', | ||
'attr2': 42, | ||
'b': {'a': {'attr': 'kefir', 'attr2': 'kefir'}, 'attr': 'is', 'attr2': 7}} | ||
{'attr': 'best!', | ||
'attr2': 10101, | ||
'c': {'attr': 'the', | ||
'attr2': 42, | ||
'b': {'a': {'attr': 'kefir', 'attr2': 'kefir'}, | ||
'attr': 'is', | ||
'attr2': 7}}} | ||
""" | ||
``` | ||
## Reqs | ||
this support python 3.6+ | ||
## About | ||
|
@@ -60,7 +120,7 @@ i created this only for fun | |
## FAQ | ||
q: why it is needed, be cause we have marshmallow, pydantic e t.c. | ||
|
||
a: i want to be a part of "e t.c." + this is very cool for small APIs | ||
a: The trick is that you don't need to write Schema or add in your project more deps if you do small app or you need only for `dump` and `load` in future ;) methods | ||
|
||
q: Release on PyPi? | ||
|
||
|