[Python] Marshmallow Code

Source: Internet
Author: User

    • Schema.dump and Schema.load

The Schema.dump () method returns an Marshresult object, marshmallow the official API says that the dump and load methods return all Dict objects, but to view the source code, the Marshresult object is a namedtuple.

# # marshmallow::schema.py

# # # line 25 # #

#: Return type of:meth: ' Schema.dump ' including serialized data and ErrorsMarshalresult = Namedtuple ('Marshalresult', ['Data','Errors'])#: Return type of:meth: ' Schema.load ', including deserialized data and errorsUnmarshalresult = Namedtuple ('Unmarshalresult', ['Data','Errors'])

When I was running the official example, there was a problem here.

 try  : Data  = Quote_schema.load (json_data)  except   ValidationError as err:  return  jsonify (err.messages), 422 First, last  = Data[ " author  " ][ " first  " ], Data[ " author " Span style= "COLOR: #800000" > "]["  last    "] # code error here, typeerror:tuple indices must be integers or slices, not str 

Namedtuple cannot access object information by means of result[' data ' [' xxx ']

But through result.data[' xxx '] is OK. The way to ask Namedtuple in Python doc is "." Access. It seems to be understood that name is a attribute of namedtuple,

>>>#Basic Example>>> point = Namedtuple (' Point', ['x','y'])>>> p = Point (one, y=22)#instantiate with positional or keyword arguments>>> x, y = P#unpack like a regular tuple>>>x, Y (11, 22)>>> p.x + p.y # fields also accessible by name33>>> P#readable __repr__ with a name=value stylePoint (x=11, y=22)

[Python] Marshmallow Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.