Sanic and Data Validation using Pydantic - Deepstash
Machine Learning With Google

Learn more about computerscience with this collection

Understanding machine learning models

Improving data analysis and decision-making

How Google uses logic in machine learning

Machine Learning With Google

Discover 95 similar ideas in

It takes just

14 mins to read

Pydantic

  • pydantic class allows custom data types to be defined or we can extend validation with methods on a model decorated with the validator decorator
  • Here, The datatype of name, grade, roll, email, phone, subjects, friends must be as mentioned in pydantic class else it will raise an error (pydantic validation error)

3

54 reads

Serialization

  • In Python “serialization” does nothing else than just convert the given data structure (e.g. a dict ) into its valid JSON object. JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
  • When transmitting a student in a file, the student is required to be byte strings, but Pydntic(here Student) objects are seldom in this format. Serialization can convert these Pydantic objects into byte strings for such use.
  • We need JSON Serializable objects to provide HTTP Response.

3

25 reads

POST METHOD

  • We will request data in JSON format and pass the data to the Student class, Which will Return the Student object and store it in the student variable.
  • before inserting the student object into DB, we converted the Student object to a python dictionary and then inserted it into the database.
  • for the response that we just inserted, we again converted the Student object to a python dictionary.

3

21 reads

PUT METHOD

  • We pass id and mention that id_ is of type integer,
  • We will request data to be updated in JSON format and pass the data to the Student class, Which will Return the Student object and store it in the student variable.
  • before inserting student objects into DB, we check whether the provided id exists or not.
  • if id_ exists, we converted the Student object to python dictionary and then inserted it into the database and provide Success message.
  • else we provide the error message.

3

15 reads

DELETE METHOD

  • We pass id and mention that id_ is of type integer,
  • we check whether or not id_ exists in the database, if exists, delete the data and provide a response that the data is deleted
  • else we provide the error message.

In this way we can validate our data, we can use custom validators in the pydantic model too, like if we require to accept the name that must contain space then you can specify a validator for that,

3

17 reads

CURATED BY

CURATOR'S NOTE

Data validation using Pydantic in python

More like this

stash-superman-illustration

Explore the World’s

Best Ideas

200,000+ ideas on pretty much any topic. Created by the smartest people around & well-organized so you can explore at will.

An Idea for Everything

Explore the biggest library of insights. And we've infused it with powerful filtering tools so you can easily find what you need.

Knowledge Library

Powerful Saving & Organizational Tools

Save ideas for later reading, for personalized stashes, or for remembering it later.

# Personal Growth

Take Your Ideas

Anywhere

Organize your ideas & listen on the go. And with Pro, there are no limits.

Listen on the go

Just press play and we take care of the words.

Never worry about spotty connections

No Internet access? No problem. Within the mobile app, all your ideas are available, even when offline.

Get Organized with Stashes

Ideas for your next work project? Quotes that inspire you? Put them in the right place so you never lose them.

Join

2 Million Stashers

4.8

5,740 Reviews

App Store

4.7

72,690 Reviews

Google Play

Sean Green

Great interesting short snippets of informative articles. Highly recommended to anyone who loves information and lacks patience.

Ashley Anthony

This app is LOADED with RELEVANT, HELPFUL, AND EDUCATIONAL material. It is creatively intellectual, yet minimal enough to not overstimulate and create a learning block. I am exceptionally impressed with this app!

Shankul Varada

Best app ever! You heard it right. This app has helped me get back on my quest to get things done while equipping myself with knowledge everyday.

samz905

Don’t look further if you love learning new things. A refreshing concept that provides quick ideas for busy thought leaders.

Ghazala Begum

Even five minutes a day will improve your thinking. I've come across new ideas and learnt to improve existing ways to become more motivated, confident and happier.

Giovanna Scalzone

Brilliant. It feels fresh and encouraging. So many interesting pieces of information that are just enough to absorb and apply. So happy I found this.

Jamyson Haug

Great for quick bits of information and interesting ideas around whatever topics you are interested in. Visually, it looks great as well.

Laetitia Berton

I have only been using it for a few days now, but I have found answers to questions I had never consciously formulated, or to problems I face everyday at work or at home. I wish I had found this earlier, highly recommended!

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Access to 200,000+ ideas

Access to the mobile app

Unlimited idea saving & library

Unlimited history

Unlimited listening to ideas

Downloading & offline access

Personalized recommendations

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates