Create a file name serializing.py
from marshmallow import Schema, fields # Install marshmallow latest pre-release version: $ pip install -U marshmallow --pre
class BookSchema(Schema): # Class of type Schema named BookSchema
title = fields.Str() # Schema property
author = fields.Str() # Schema property
class Book: # Class of type Regular named Book
def __init__(self, title, author, description): # Object constructor
self.title = title # Class property
self.author = author # Class property
self.description = description # Class property
book = Book("Clean Code", "Bob Martin", "A book aout writing clean code.") # Creating an object of class Book by passing values required by constructor
book_schema = BookSchema() # Creating an object of class BookSchema
book_dict = book_schema.dump(book) # Calling dump function of object book_schema of class BookSchema and passing the object book of class Book
print(book_dict) # printing value referred by variable book_dict
Comments
Post a Comment