diff --git a/README.md b/README.md index c10a571..6444d73 100644 --- a/README.md +++ b/README.md @@ -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/') +@app.get('/orders//') 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':'kefir_mail@notreal.uncom'}} return jsonify(order) +@app.get('/users//') +def user_view(user_id): + user = User.query.get(user_id) #User(id=42, name='Kefir', email='kefir_mail@notreal.uncom') + user_dict = kef.dump(user) # {'id':42,'name':'Kefir','email':'kefir_mail@notreal.uncom','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?