当前位置: 首页 > news >正文

辽宁省兴城做网站的比较好的软文发布平台

辽宁省兴城做网站的,比较好的软文发布平台,哪个网站卖自己做的手工艺品,windows优化大师的作用前言 在许多场景中#xff0c;我们常常需要执行Python对象的序列化、反序列化操作。例如#xff0c;在开发REST API时#xff0c;或者在进行一些面向对象化的数据加载和保存时#xff0c;这一功能经常派上用场。 经常cv Python代码的臭宝#xff0c;接触最多的应该是通过…前言 在许多场景中我们常常需要执行Python对象的序列化、反序列化操作。例如在开发REST API时或者在进行一些面向对象化的数据加载和保存时这一功能经常派上用场。 经常cv Python代码的臭宝接触最多的应该是通过json、pickle模块进行序列化或反序列化这是一种常见的做法。 import jsondata {name: John, age: 30, city: New York} serialized_data json.dumps(data)往往Python对象的序列化、反序列化同时也要伴随着数据的处理和校验。 而今天要自我介绍的主角Marshmallow则为我们带来更强大的数据序列化和反序列化更优雅的参数校验、数据处理能力。 Github地址https://github.com/marshmallow-code/marshmallow 它可以帮助您将复杂的数据类型例如对象或数据库记录转换为Python数据类型例如字典反之亦然。 它被大量用于Flask和Django等Web开发框架中以处理数据输入和输出。 楔子 为了执行序列化或反序列化操作首先需要一个操作对象。在这里我们先定义一个类 class Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pages# 实例化一个小说对象 interesting_novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages300)现在的需求是将这个小说对象转成字典。你会怎么来实现呢 笨方法 手动创建一个字典将小说对象的属性映射到字典的键和值。 novel_dict {title: interesting_novel.title,author: interesting_novel.author,genre: interesting_novel.genre,pages: interesting_novel.pages }使用vars函数 Python 中的 vars 函数可以返回对象的 dict 属性该属性包含了对象的所有属性和对应的值。这样你可以直接使用 vars 将对象转换为字典 novel_dict vars(interesting_novel)使用__dict__属性 对于具有__dict__属性的对象可以直接访问该属性获取对象的属性和值。 novel_dict interesting_novel.__dict__使用json.loads(json.dumps(obj)) 利用JSON库通过先将对象转换为JSON字符串然后再将其解析为字典。 import jsonnovel_json json.dumps(interesting_novel, defaultlambda o: o.__dict__) novel_dict json.loads(novel_json)数据类使用dataclass/attrs的内置方法 dataclass版本 from dataclasses import dataclass, asdict from loguru import loggerdataclass class Novel:title: strauthor: strgenre: strpages: int# 实例化一个小说对象 interesting_novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages300)# 将对象序列化为字典 novel_dict asdict(interesting_novel) logger.info(novel_dict)# 将字典反序列化为对象 new_novel Novel(**novel_dict) logger.info(new_novel)attrs版本 import attr import cattr from loguru import loggerattr.define class Novel:title attr.ib()author attr.ib()genre attr.ib()pages attr.ib()# 实例化一个小说对象 interesting_novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages300)# 将对象序列化为字典 novel_dict cattr.unstructure(interesting_novel) logger.info(novel_dict)# 将字典反序列化为对象 new_novel_dict {title: AI之旅, author: HaiGe, genre: Fantasy, pages: 668} new_novel cattr.structure(new_novel_dict, Novel) logger.info(new_novel)更优雅的方案marshmallow 上面介绍的几种序列化与反序列化方法看起来已经相当炸裂但是为什么还要选择 marshmallow 呢 其实尽管这些方法能够完成基本的序列化和反序列化任务但在处理更加复杂的数据结构、数据验证、预处理逻辑以及与其他库的集成等方面它们可能显得不够灵活和方便。而 marshmallow 库正是为了解决这些问题而诞生的。 marshmallow 提供了强大而灵活的schema模式定义可以精确地控制数据的序列化和反序列化过程。它支持复杂的数据结构、自定义验证器、预处理逻辑等高级功能同时与许多其他常见的Python库和框架无缝集成。 无论是构建RESTful API、数据持久化、数据迁移或者简单的数据处理、数据验证等领域marshmallow都能发挥出色的作用特别适合于需要处理复杂数据结构、进行数据交换的场景。 此外marshmallow还提供了丰富的文档和社区支持使得学习和使用起来更加容易。因此尽管已经有了许多其他方法但选择marshmallow依然是一个明智的选择特别是在处理复杂的数据结构和场景下。 marshmallow库的基本用法 安装 要使用marshmallow这个库需要先安装下 # 3.20.2 pip3 install marshmallow A. 序列化与反序列化 marshmallow提供了灵活且强大的数据序列化与反序列化功能可以将复杂的Python数据类型转换为JSON、XML等格式也能反向将外部数据解析为Python对象。 from marshmallow import Schema, fields, post_loadclass Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str()author fields.Str()genre fields.Str()pages fields.Int()post_loaddef make(self, data, **kwargs):return Novel(**data)# 创建一个 Novel 对象 novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages300)# 序列化 Novel 对象 novel_schema NovelSchema() serialized_data novel_schema.dump(novel) print(serialized_data)# 反序列化 deserialized_data novel_schema.load(serialized_data) print(deserialized_data)这里我们需要稍微区分一下schema的dump方法和dumps方法dump()方法返回的是dict格式而dumps()方法返回的是JSON字符串。 同理load方法用来加载字典而loads方法用来加载JSON字符串。 让我们再来看下多个对象序列化与反序列化同样非常简单 from marshmallow import Schema, fields, post_load from loguru import loggerclass Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str()author fields.Str()genre fields.Str()pages fields.Int()post_loaddef make(self, data, **kwargs):return Novel(**data)# 创建一个 Novel 对象 novel1 Novel(title海哥python1, author暴走的海鸽, genreFantasy, pages300) novel2 Novel(title海哥python2, author暴走的海鸽, genreFantasy, pages300) novel3 Novel(title海哥python3, author暴走的海鸽, genreFantasy, pages300)novels [novel1, novel2, novel3]# 序列化 Novel 对象 novel_schema NovelSchema(manyTrue) serialized_data novel_schema.dump(novels) logger.info(serialized_data)# 反序列化 deserialized_data novel_schema.load(serialized_data) logger.info(deserialized_data)此外Schema类具有两个参数用于控制序列化的输出即only和exclude。only 参数返回的输出结果仅包含列表中指定的类属性而exclude则正好相反它会排除列表中指定的类属性。 from marshmallow import Schema, fields, post_load, validates, ValidationError, validate from loguru import loggerclass Novel:def __init__(self, title, author, genreFantasy2, pages300):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str(validatevalidate.Length(min1, max10))author fields.Str()genre fields.Str()pages fields.Int()post_loaddef make(self, data, **kwargs):logger.info(data)return Novel(**data)validates(pages)def validate_pages(self, value):if value 0:raise ValidationError(Pages must be a positive integer.)# Create a Novel object novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages-300)# Serialize the Novel object novel_schema NovelSchema(only(title, author,)) serialized_data novel_schema.dump(novel) logger.info(serialized_data)B. 数据验证 数据验证是marshmallow的另一个重要特性它能够定制对数据进行验证包括类型验证、长度验证、自定义验证等保证数据的完整性和正确性。 内置的常见验证器有 from marshmallow import Schema, fields, post_load, validates, ValidationError, validate from loguru import loggerclass Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str(validatevalidate.Length(min1, max10))author fields.Str()genre fields.Str()pages fields.Int()post_loaddef make(self, data, **kwargs):return Novel(**data)validates(pages)def validate_pages(self, value):if value 0:raise ValidationError(Pages must be a positive integer.)# Create a Novel object novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages-300)# Serialize the Novel object novel_schema NovelSchema() serialized_data novel_schema.dump(novel) logger.info(serialized_data)# Deserialization try:deserialized_data novel_schema.load(serialized_data)logger.info(deserialized_data) except ValidationError as e:logger.error(e.messages)logger.error(e.valid_data)在这个例子中我们对title使用了validate字段验证并且定义了一个validate_pages方法用于验证pages字段。如果pages字段的值小于等于0将会引发一个ValidationError异常。在反序列化时如果遇到校验失败Marshmallow将会捕获异常并将校验错误信息存储在messages属性中。 如果需要对属性进行缺失验证则在schema中规定required参数即表明该参数是必要的不可缺失。 from marshmallow import Schema, fields, post_load, validates, ValidationError, validate from loguru import loggerclass Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str(validatevalidate.Length(min1, max10))author fields.Str(requiredTrue)genre fields.Str()pages fields.Int()post_loaddef make(self, data, **kwargs):return Novel(**data)validates(pages)def validate_pages(self, value):if value 0:raise ValidationError(Pages must be a positive integer.)# Create a Novel object # novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages-300) novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages-300)# Serialize the Novel object novel_schema NovelSchema() serialized_data novel_schema.dump(novel) logger.info(serialized_data)# Deserialization serialized_data.pop(author) # 移除author try:deserialized_data novel_schema.load(serialized_data)logger.info(deserialized_data) except ValidationError as e:logger.error(e.messages)logger.error(e.valid_data)我们给author字段定义了required属性但是反序列化的时候并没有传入具体报错如下 Marshmallow在序列化和反序列化字段方面也提供了默认值并且非常清晰地区分它们例如load_default参数用于在反序列化时自动填充数据而dump_default参数则用于在序列化时自动填充数据。 from marshmallow import Schema, fields, post_load, validates, ValidationError, validate from loguru import loggerclass Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str(validatevalidate.Length(min1, max1000))author fields.Str(requiredTrue)genre fields.Str()pages fields.Int(load_default300, dump_default500) # 设置反序列化默认值为300序列化默认值为500post_loaddef make(self, data, **kwargs):return Novel(**data)validates(pages)def validate_pages(self, value):if value 0:raise ValidationError(Pages must be a positive integer.)# Create a Novel object novel {title: 公众号海哥python, author: 暴走的海鸽, genre: Fantasy}# Serialize the Novel object novel_schema NovelSchema() serialized_data novel_schema.dump(novel) logger.info(f序列化{serialized_data})# Deserialization novel2 {title: 公众号海哥python, author: 暴走的海鸽, genre: Fantasy} try:deserialized_data novel_schema.load(novel2)logger.info(f反序列化{deserialized_data}) except ValidationError as e:logger.error(e.messages)logger.error(f合法的数据{e.valid_data})在序列化过程中Schema对象默认会使用与其自身定义相同的fields属性名但也可以根据需要进行自定义。 如果使用和生成与架构不匹配的数据则可以通过data_key参数指定输出键类似于起了别名。 from marshmallow import Schema, fields, post_load, validates, ValidationError, validate from loguru import loggerclass Novel:def __init__(self, title, author, genre, pages):self.title titleself.author authorself.genre genreself.pages pagesdef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r})class NovelSchema(Schema):title fields.Str(validatevalidate.Length(min1, max1000))author fields.Str(data_keyauthor_name)genre fields.Str()pages fields.Int(missing300, default500) # 设置反序列化默认值为300序列化默认值为500post_loaddef make(self, data, **kwargs):return Novel(**data)validates(pages)def validate_pages(self, value):if value 0:raise ValidationError(Pages must be a positive integer.)# Create a Novel object novel {title: 公众号海哥python, author_name: 暴走的海鸽2, genre: Fantasy}# Serialize the Novel object novel_schema NovelSchema() serialized_data novel_schema.dump(novel) logger.info(f序列化{serialized_data})# Deserialization novel2 {title: 公众号海哥python, author: 暴走的海鸽, genre: Fantasy} try:deserialized_data novel_schema.load(novel2)logger.info(f反序列化{deserialized_data}) except ValidationError as e:logger.error(e.messages)logger.error(f合法的数据{e.valid_data})C. 自定义字段类型 通过marshmallow我们可以轻松定义自定义字段满足各种特殊数据类型的序列化、反序列化需求使得数据处理更加灵活。 from datetime import datetimefrom marshmallow import Schema, fields, post_load, validates, ValidationError from loguru import loggerclass CustomField(fields.Int):def _deserialize(self, value, attr, obj, **kwargs):# 将数字加1return value 1class CustomDateField(fields.Field):def _deserialize(self, value, attr, obj, **kwargs):return value.strftime(%Y-%m-%d)class Novel:def __init__(self, title, author, genre, pages, date):self.title titleself.author authorself.genre genreself.pages pagesself.date datedef __repr__(self):return fNovel(title{self.title!r}, author{self.author!r}, genre{self.genre!r}, pages{self.pages!r}, date{self.date!r})class NovelSchema(Schema):title fields.Str()author fields.Str()genre fields.Str()pages CustomField()date CustomDateField()post_loaddef make(self, data, **kwargs):return Novel(**data)validates(pages)def validate_pages(self, value):if value 0:raise ValidationError(Pages must be a positive integer.)# Create a Novel object novel Novel(titleThe Enchanting Adventure, authorJane Doe, genreFantasy, pages300,datedatetime(2024, 3, 13))# Serialize the Novel object novel_schema NovelSchema() serialized_data novel_schema.dump(novel) logger.info(serialized_data)# Deserialization try:deserialized_data novel_schema.load(serialized_data)logger.info(deserialized_data) except ValidationError as e:logger.error(e.messages)logger.error(e.valid_data)高级应用技巧和场景 部分加载 在多个位置使用同一Schema时您可能只想通过传递partial来跳过required验证。 from marshmallow import Schema, fieldsclass UserSchema(Schema):name fields.String(requiredTrue)age fields.Integer(requiredTrue)result UserSchema().load({age: 42}, partialTrue) # OR UserSchema(partialTrue).load({age: 42}) print(result) # {age: 42}您可以通过设置partialTrue来完全忽略缺少的字段。 class UserSchema(Schema):name fields.String(requiredTrue)age fields.Integer(requiredTrue)result UserSchema().load({age: 42}, partialTrue) # OR UserSchema(partialTrue).load({age: 42}) print(result) # {age: 42}处理未知字段 默认情况下如果遇到Schema中没有匹配Field项的键load将引发marshmallow.exceptions.ValidationError。 from marshmallow import Schema, fields, INCLUDEclass UserSchema(Schema):name fields.String(requiredTrue)age fields.Integer(requiredTrue)# class Meta:# unknown INCLUDEresult UserSchema().load({age: 42, name: 公众号 海哥python, email: 16666qq.com}) # OR UserSchema(partialTrue).load({age: 42}) print(result) # {age: 42}我们可以对未知字段进行处理 可以在Meta类中指定unknown Schema在实例化时: schema UserSchema(unknownINCLUDE)调用load时: UserSchema().load(data, unknownINCLUDE) 该选项接受以下选项之一 RAISE 默认值 ValidationError 如果存在任何未知字段则引发EXCLUDE 排除未知字段INCLUDE 接受并包含未知字段 dump_only“只读”和load_only“只写”字段 from datetime import datetime from marshmallow import Schema, fields, INCLUDEclass UserSchema(Schema):name fields.Str()# password is write-onlypassword fields.Str(load_onlyTrue)# created_at is read-onlycreated_at fields.DateTime(dump_onlyTrue)# 序列化 user_data {name: Alice, password: s3cr3t, created_at: datetime.now()} user_schema UserSchema() serialized_data user_schema.dump(user_data) print(序列化:, serialized_data)# 反序列化 user_input {name: Bob, password: pass123} user_schema UserSchema() try:deserialized_data user_schema.load(user_input)print(反序列化:, deserialized_data) except Exception as e:print(反序列化报错:, e)排序 对于某些用例维护序列化输出的字段顺序可能很有用。要启用排序请将ordered选项设置为true。这将指示marshmallow将数据序列化到collections.OrderedDict。 #!usr/bin/env python # -*- coding:utf-8 _*- # __author__lianhaifeng # __time__2024/3/14 21:40 import datetimefrom marshmallow import Schema, fields, INCLUDEfrom collections import OrderedDictclass User:def __init__(self, name, email):self.name nameself.email emailself.created_time datetime.datetime.now()class UserSchema(Schema):uppername fields.Function(lambda obj: obj.name.upper())class Meta:fields (name, email, created_time, uppername)ordered Trueu User(Charlie, charliestones.com) schema UserSchema() res schema.dump(u) print(isinstance(res, OrderedDict)) # True print(res) # OrderedDict([(name, Charlie), (email, charliestones.com), (created_time, 2019-08-05T20:22:05.78854000:00), (uppername, CHARLIE)]) 嵌套模式 对于嵌套属性marshmallow毫无疑问也能胜任这正是我认为marshmallow非常强大的地方。 一个Blog可能有一个作者由User对象表示。 import datetime as dt from pprint import pprint from marshmallow import Schema, fieldsclass User:def __init__(self, name, email):self.name nameself.email emailself.created_at dt.datetime.now()self.friends []self.employer Noneclass Blog:def __init__(self, title, author):self.title titleself.author author # A User objectclass UserSchema(Schema):name fields.String()email fields.Email()created_at fields.DateTime()class BlogSchema(Schema):title fields.String()author fields.Nested(UserSchema)user User(nameMonty, emailmontypython.org) blog Blog(titleSomething Completely Different, authoruser) result BlogSchema().dump(blog) pprint(result)# {title: uSomething Completely Different, # author: {name: uMonty, # email: umontypython.org, # created_at: 2014-08-17T14:58:57.60062300:00}}更多嵌套玩法详见 https://marshmallow.readthedocs.io/en/stable/nesting.html 扩展 Schema 预处理和后处理方法 可以使用pre_load、post_load、pre_dump和post_dump装饰器注册数据预处理和后处理方法。 from marshmallow import Schema, fields, post_load, pre_load, pre_dump, post_dump from loguru import loggerclass User:def __init__(self, username, email):self.username usernameself.email emaildef __repr__(self):return fUser(username{self.username!r}, email{self.email!r})class UserSchema(Schema):username fields.Str()email fields.Email()pre_loaddef preprocess_data(self, data, **kwargs):# 在反序列化之前对数据进行预处理if username in data:data[username] data[username].lower() # 将用户名转换为小写logger.info(do pre_load...)return datapost_loaddef make_user(self, data, **kwargs):# 在反序列化之后创建用户对象logger.info(do post_load...)return User(**data)pre_dumpdef prepare_data(self, data, **kwargs):# 在序列化之前对数据进行预处理logger.info(type(data))if isinstance(data, User):data.username data.username.upper()elif username in data:data[username] data[username].upper() # 将用户名转换为大写logger.info(do pre_dump...)return datapost_dumpdef clean_data(self, data, **kwargs):# 在序列化之后对序列化结果进行清理logger.info(type(data))if email in data:del data[email] # 删除 email 字段logger.info(do post_dump...)return data# 准备要反序列化的数据 input_data [{username: 公众号海哥Python,email: haigeqq.com }]# 创建 Schema 对象并进行反序列化 user_schema UserSchema() result user_schema.load(input_data, manyTrue)logger.info(fPost Load Result: {result}) # 输出反序列化后的结果# 创建一个 User 对象 user User(username公众号海哥Python, emailhaigeqq.com)# 序列化 User 对象 serialized_data user_schema.dump(user)logger.info(fPost Dump Result: {serialized_data}) # 输出序列化后的结果自定义错误处理 import logging from marshmallow import Schema, fieldsclass AppError(Exception):passclass UserSchema(Schema):email fields.Email()def handle_error(self, exc, data, **kwargs):Log and raise our custom exception when (de)serialization fails.logging.error(exc.messages)raise AppError(An error occurred with input: {0}.format(data))schema UserSchema() schema.load({email: invalid-email}) # raises AppError场景示例 REST API中基于marshmallow做参数校验是一种相对优雅的操作。 安装: # Flask 3.0.2 # Flask-SQLAlchemy 3.1.1 pip install flask flask-sqlalchemy应用代码 # demo.py import datetimefrom flask import Flask, request from flask_sqlalchemy import SQLAlchemy from sqlalchemy.exc import NoResultFoundfrom marshmallow import Schema, ValidationError, fields, pre_loadapp Flask(__name__) app.config[SQLALCHEMY_DATABASE_URI] sqlite:///quotes.dbdb SQLAlchemy(app)##### MODELS #####class Author(db.Model): # type: ignoreid db.Column(db.Integer, primary_keyTrue)first db.Column(db.String(80))last db.Column(db.String(80))class Quote(db.Model): # type: ignoreid db.Column(db.Integer, primary_keyTrue)content db.Column(db.String, nullableFalse)author_id db.Column(db.Integer, db.ForeignKey(author.id))author db.relationship(Author, backrefdb.backref(quotes, lazydynamic))posted_at db.Column(db.DateTime)##### SCHEMAS #####class AuthorSchema(Schema):id fields.Int(dump_onlyTrue)first fields.Str()last fields.Str()formatted_name fields.Method(format_name, dump_onlyTrue)def format_name(self, author):return f{author.last}, {author.first}# Custom validator def must_not_be_blank(data):if not data:raise ValidationError(Data not provided.)class QuoteSchema(Schema):id fields.Int(dump_onlyTrue)author fields.Nested(AuthorSchema, validatemust_not_be_blank)content fields.Str(requiredTrue, validatemust_not_be_blank)posted_at fields.DateTime(dump_onlyTrue)# Allow client to pass authors full name in request body# e.g. {author: Tim Peters} rather than {first: Tim, last: Peters}pre_loaddef process_author(self, data, **kwargs):author_name data.get(author)if author_name:first, last author_name.split( )author_dict dict(firstfirst, lastlast)else:author_dict {}data[author] author_dictreturn dataauthor_schema AuthorSchema() authors_schema AuthorSchema(manyTrue) quote_schema QuoteSchema() quotes_schema QuoteSchema(manyTrue, only(id, content))##### API #####app.route(/authors) def get_authors():authors Author.query.all()# Serialize the querysetresult authors_schema.dump(authors)return {authors: result}app.route(/authors/int:pk) def get_author(pk):try:author Author.query.filter(Author.id pk).one()except NoResultFound:return {message: Author could not be found.}, 400author_result author_schema.dump(author)quotes_result quotes_schema.dump(author.quotes.all())return {author: author_result, quotes: quotes_result}app.route(/quotes/, methods[GET]) def get_quotes():quotes Quote.query.all()result quotes_schema.dump(quotes, manyTrue)return {quotes: result}app.route(/quotes/int:pk) def get_quote(pk):try:quote Quote.query.filter(Quote.id pk).one()except NoResultFound:return {message: Quote could not be found.}, 400result quote_schema.dump(quote)return {quote: result}app.route(/quotes/, methods[POST]) def new_quote():json_data request.get_json()if not json_data:return {message: No input data provided}, 400# Validate and deserialize inputtry:data quote_schema.load(json_data)except ValidationError as err:return err.messages, 422first, last data[author][first], data[author][last]author Author.query.filter_by(firstfirst, lastlast).first()if author is None:# Create a new authorauthor Author(firstfirst, lastlast)db.session.add(author)# Create new quotequote Quote(contentdata[content], authorauthor, posted_atdatetime.datetime.utcnow())db.session.add(quote)db.session.commit()result quote_schema.dump(Quote.query.get(quote.id))return {message: Created new quote., quote: result}if __name__ __main__:with app.app_context():db.create_all()app.run(debugTrue, port5000)启动服务 python .\demo.py安装httpie进行测试 pip install httpie添加有效报价 添加无效报价 查询报价 常与marshmallow搭档的库还有flask-marshmallow、flask-smorest等 小结 总的来说marshmallow库具有强大的序列化、反序列化和数据验证功能能够适用于各种复杂的数据处理场景使得数据处理变得更加便捷和高效。 相比于Python标准库中的json库marshmallow提供了更为灵活且功能强大的数据序列化与验证功能适用于更多复杂数据结构的处理。与Django框架中的序列化器相比marshmallow更加轻量且不依赖于特定的框架能够在各种Python项目中灵活应用。 适用于Web开发、RESTful API构建、数据持久化、数据验证等多种场景尤其适合处理复杂数据结构的情况。 随着数据处理需求的日益复杂marshmallow作为一款强大的数据序列化与验证库必将在Python数据处理领域有着更为广阔的发展前景。 无论您是在开发Web API、进行表单验证、与数据库交互还是进行数据导入和导出Marshmallow都能够高效地处理数据。希望本文能够帮助您更好地理解和使用Marshmallow Marshmallow的强大远不止这些更多使用技巧请查阅官方文档 最后 今天的分享就到这里。如果觉得不错点赞关注安排起来吧。 参考 https://marshmallow.readthedocs.io/en/stable/ https://rest-apis-flask.teclado.com/docs/flask_smorest/validation_with_marshmallow/ https://marshmallow.readthedocs.io/en/stable/nesting.html https://www.cnblogs.com/ChangAn223/p/11305376.html https://blog.51cto.com/u_15703497/6858837 https://www.cnblogs.com/erhuoyuan/p/17450757.html https://github.com/marshmallow-code/marshmallow-sqlalchemy
http://www.zqtcl.cn/news/392999/

相关文章:

  • 境外企业网站推广大冶市建设局网站
  • 户网站建设的不全.阿里巴巴国际站
  • 定制手机壳的网站能在家做的兼职的网站
  • 温州营销型网站建设郴州网络推广公司
  • asp.net 做网站源代码网站怎么做配置文件夹
  • 网站建设云尚网络wordpress首页flash
  • 北京优化网站宁波网络营销策划公司
  • 网站建设项目前分析电商运营一般要学多久
  • 哪个网站可以做卖房网站菜单模板
  • 网站推广渠道特点郑州百度推广外包
  • 合肥高端网站建设设计公司wordpress 多语言主题
  • 北京工程工程建设交易信息网站wordpress 角色 功能
  • 做购物网站有什么要求吗wordpress查看访问量
  • 多城市网站设计阿里云网站访问不了怎么办
  • 南岗哈尔滨网站建设开发小程序多少费用
  • 百度网站入口特效词品牌企业网站建设公司
  • wordpress找回管理员密码网站关键词排名优化工具
  • 望城建设局网站网站建设与维护可行性报告
  • 免费php网站模板下载手机端网站如何优化
  • 自己做的网站 打开了没有图片注册工程公司名称大全
  • 做网站的团队业绩怎么写WordPress 去掉副标题
  • 学校网页网站模板wordpress更换域名还是之前链接
  • 市面上有什么搭建网站工作室石家庄做网站和宣传的
  • 视频图站主题 wordpress快速收录提交入口
  • 外贸视频网站投资理财网站开发
  • 专业建设网站多少钱铜川网站seo
  • 海外网站seo优化wordpress的代码逻辑
  • 怎样帮别人做网站哪有网站给光头强做面
  • 聊城营销网站建设价格网站设计论文框架
  • 成都哪家网站建设做得好介绍自己的家乡遵义网站建设