pynamodb与marshmallow(de)序列化库的集成

marshmallow-pynamodb的Python项目详细描述


Latest versionTravis-CI

PynamoDBmarshmallow(反)序列化库集成。

安装

来自PYPI:

$ pip install marshmallow-pynamodb

来自github:

$ pip install git+https://github.com/mathewmarcus/marshmallow-pynamodb#egg=marshmallow_pynamodb

申报您的型号

frompynamodb.modelsimportModelfrompynamodb.attributesimportUnicodeAttributeclassUser(Model):classMeta:table_name="user"email=UnicodeAttribute(null=True)first_name=UnicodeAttribute(range_key=True)last_name=UnicodeAttribute(hash_key=True)

生成棉花糖架构

frommarshmallow_pynamodbimportModelSchemaclassUserSchema(ModelSchema):classMeta:model=Useruser_schema=UserSchema()

(反)序列化数据

user=User(last_name="Smith",first_name="John")user_schema.dump(user).data# {u'first_name': u'John', u'last_name': u'Smith', u'email': None}user_schema.load({"last_name":"Smith","first_name":"John"}).data# user<Smith>

嵌套模型?没问题

frommarshmallow_pynamodb.schemaimportModelSchemafrompynamodb.modelsimportModelfrompynamodb.attributesimportListAttribute,MapAttribute,NumberAttribute,UnicodeAttributeclassLocation(MapAttribute):latitude=NumberAttribute()longitude=NumberAttribute()name=UnicodeAttribute()classPerson(MapAttribute):firstName=UnicodeAttribute()lastName=UnicodeAttribute()age=NumberAttribute()classOfficeEmployeeMap(MapAttribute):office_employee_id=NumberAttribute()person=Person()office_location=Location()classOffice(Model):classMeta:table_name='OfficeModel'office_id=NumberAttribute(hash_key=True)address=Location()employees=ListAttribute(of=OfficeEmployeeMap)classOfficeSchema(ModelSchema):classMeta:model=OfficeOfficeSchema().load({'office_id':789,'address':{'latitude':6.98454,'longitude':172.38832,'name':'some_location'},'employees':[{'office_employee_id':123,'person':{'firstName':'John','lastName':'Smith','age':45},'office_location':{'latitude':-24.0853,'longitude':144.87660,'name':'other_location'}},{'office_employee_id':456,'person':{'firstName':'Jane','lastName':'Doe','age':33},'office_location':{'latitude':-20.57989,'longitude':92.30463,'name':'yal'}}]}).data# Office<789>

许可证

麻省理工学院许可有关详细信息,请参阅捆绑的LICENSE文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Spring数据JPA+Hibernate在不首先找到父实体的情况下保存子实体   php Java:如何从CLI接收命令   spring为java中的导出数据创建访问文件   java在Windows 8.1上安装Play Framework   java Spring启动白标签错误页面(类型=未找到,状态=404)   java如何在单击时从数组中绘制?   java fn:substringAfter()上次出现   java在IFR语句中使用方法返回   java onPause()或onStop()的名称   对关联对象的关联对象具有条件的java HQL查询   java只打印一次总值,无需迭代   java如何使用抽象Uri buildOn()方法?   如何在Java中执行sudo命令并获得错误输出?   java反射:避免对getConstructor(类<?>…)的未经检查的警告调用作为原始类型类的成员   Java:如何从类中创建的对象调用类方法?   java如何在电子邮件中嵌入图像?   java如何在Android上启用详细GC?   java什么是串行版本id?