Toasted Marshmallow Enum包使基于Toasted Marshmallow提供的超快速序列化来转储和加载枚举值成为可能。
toastedmarshmallow-enum的Python项目详细描述
灵感来自Marshmallow Enum和orm库。
Toasted Marshmallow Enum包使基于超高速的 Toasted Marshmallow提供的序列化。
正在安装toastedmarshmlow enum
pip install toastedmarshmallow-enum
使用烤棉花糖枚举
在现有的{TT1} $中使用烤棉花糖枚举与定义EnUM类属性一样容易 作为EnumField。例如:
fromenumimportEnumimporttoastedmarshmallowfrommarshmallowimportfields,Schemafromtoastedmarshmallow_enumimportEnumFieldclassLevel(Enum):LOW='0'MEDIUM='1'HIGH='2'classUserSchema(Schema):classMeta:jit=toastedmarshmallow.Jitname=fields.String()level=EnumField(Level)
工作原理
Dump methods:
classUser:def__init(name,level):self.name=nameself.level=levelschema=DummyUserSchema()user=User(name='John Doe',level=Level.HIGH)print(schema.dump(user).data)# {'name': 'John Doe', 'level': 2}print(schema.load({'name':'John Doe','level: 2}).data)# {'name': 'John Doe', 'level': 'HIGH'}