Object Relational Mapper
Continuing on from last week’s blog that looked at the API, SQLAlchemy and ORM’s is the addition of marshmallow into the mix of technologies, frameworks being used in this project / application.
What is marshmallow?
It is another type of Object Relational Mapper (ORM) that converts complex datatypes (like objects) to and from native Python datatypes (like boolean, numbers, strings, etc).
Marshmallow schemas can be used to:
- Validate input data and minimize malformed data entering the system (to prevent things like XSS)
- Deserialize input data to app-level objects
- Serialize app-level objects to primitive Python types. The serialized objects can then be rendered to standard formats such as JSON for use in an HTTP API.
Marshmallow acts basically as an object serializer and deserializer library.
Serializing is turning turning an object into bytes for transfer or storage or saving.
Deserializing is turning bytes back into a copy of the original object or retrieving data.
Some benefits of using Marshmallow include:
It is Agnostic
It works with any ORM, ODM (object data model) or even none at all.
This enables freedom in choosing things that fit the application without having to change the data formatting code. For example we will be using Flask-Marshmallow. Flask-Marshmallow is combination of Flask (a Python web framework) and Marshmallow (the object saving and retrieving stuff remember?).
It is Concise, Familiar and enables Code Reuse and Configuration
It is touted as having familiar syntax to Django or WTForms which are other Python Web Frameworks. In fact marshmallow is inspired by the Django REST Framework, Flask-RESTful and colander. It can be configured via the schema constructor and does dump (seralization) and load (deserialization) methods.
Marshmallow is reusable as it uses classes instead of for example Flask-RESTful that uses a dictionary to define output schemas.
It is also a thin integration layer for Flask.
What is Flask?
Flask is a Python web framework. A web framework is a collection of packages or modules which allow developers to write Web applications (see WebApplications) or services without having to handle such low-level details as protocols, sockets or process/thread management.
These are generally managed on the server side (known as server side technology).
This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.
Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons.
Pros would be that the framework is light, there are little dependency to update and watch for security bugs.
Cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins. In the case of Flask, its dependencies are:
- Werkzeug a WSGI utility library (to help Python communicate with web servers like Apache, Nginx).
- jinja2 which is its template engine (a great time saver when making apps as well as helping maintenance and updating of the website).
Before this project gets too deep, I’ll take a step back and look at one of the main code component of the app, python.
Python is an interpreted, object orientated, programming language with high level build in data structures.
A key point that comes out of that statement for me is the fact it is an interpreted language. A compiled language (like python) is one that is translated into native machine instructions which are executed by the hardware on the computer like the computer’s CPU.
The code is translated into machine instructions step-by-step WHILE the program is being executed. Compiled code HAS ALREADY BEEN translated before the program executes (or runs)
The converse of this is a compiled language, where the code is translated BEFORE the program executes (or runs).
This app is being developed using Jetbrain’s program Pycharm.
As a brief refresher, Pycharm is an IDE which can be used to program something in Python. It offers (among many others)
- Coding assistance (code completion, syntax and error highlighting)
- Support for webframeworks (like Flask – this is being used in the project)
- Coding Assistance and Analysis, with code completion, syntax and error highlighting, linter integration, and quick fixes
That ends the blog for this week. I will continue to cover Python and its integration in the project in my next blog